[DA] Stack
LIFO(Last In First out) ๊ตฌ์กฐ, ๋ํ์ ์ผ๋ก ์คํ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ์ฉ
Array, Linked List ๋ก ํํ ๊ฐ๋ฅ
Array
const stack = [];
// Push
stack.push(1);
// Pop
stack.pop();
Linked List
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Stack {
constructor() {
this.top = null;
this.size = 0;
}
push(value) {
const node = new Node(value);
node.next = this.top;
this.top = node;
this.size += 1;
}
pop() {
const value = this.pop.value;
this.top = this.top.next;
this.size -= 1;
return value;
}
size() {
return this.size;
}
}
Leave a comment