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();
'소프트웨어 > javascript' 카테고리의 다른 글
promise의 엉뚱한 생각 - promise 중간에 빠져나오기 (0) | 2021.07.21 |
---|---|
[javascript] clock 만들기 (0) | 2016.06.07 |