소프트웨어/javascript

javascript로 linked list 만들기 (느낌대로)

개발자_이훈규 2019. 3. 7. 11:34




Linked List on Javascript


class Node, class LinkedList 를 만들고 push, pop, printall, size 함수를 만든다

아래는 코드 전문이다.

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.root = null;
        this.curNode = null;
        this.size = 0;
    }

    push(data) {
        const node = new Node(data);

        if (this.root == null) {
            this.root = node;
            this.curNode = node;
        } else {
            this.curNode.next = node;
            this.curNode = node;
        }

        this.size += 1;
    }

    pop() {
        let walker = this.root;
        let res = -1;

        if (walker && walker.next === null ) {
            res = walker.data;
            this.size -= 1;

            this.root = null;
            this.curNode = null;
        } else {
            while(walker) {
                if (walker.next === this.curNode) {
                    res = this.curNode.data;
                    this.size -= 1;

                    this.curNode = walker;
                    walker.next = null;
                    break;
                }
                walker = walker.next;
            }
        }

        return res;
    }

    printall() {
        let walker = this.root;

        console.log("=======printall START");
        while(walker) {
            console.log(walker.data);

            walker = walker.next;
        }
        console.log("=======printall END");
    }

    size() {
        return this.size;
    }
}

let linkedList = new LinkedList();
linkedList.push(1);
linkedList.push(2);
linkedList.push(3);
linkedList.push(4);
linkedList.push(5);
linkedList.push(6);
linkedList.printall();
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
console.log("pop data : ", linkedList.pop(), "last size : ", linkedList.size);
linkedList.printall();