스택 자료구조 특징
위 자료구조는 언제 사용될까
스택 자료구조의 빅 오 표기법
1) 값을 3으로 갖는 노드를 만들어준다.
2) stack의 top을 추가되는 노드를 가리키게 한다.
1) 값을 4로 갖는 노드를 만들어준다
2) 추가되는 노드의 next에 스택 top이 가리키는 node를 부여한다.
3) stack의 top을 추가되는 노드를 가리키게 한다.
class Node {
constructor(value) {
this.값 = value;
this.next = null;
}
}
class Stack {
constructor() {
this.top = null;
}
push(value) {
//1
const 추가되는노드 = new Node(value);
//2
추가되는노드.next = this.top;
//3
this.top = 추가되는노드;
}
pop() {
if (this.isEmpty()) throw new Error("this is empty");
const temp = this.top;
this.top = temp.next;
return temp.value;
}
peek() {
if (this.isEmpty()) throw new Error("this is empty");
return this.top.value;
}
isEmpty() {
return this.top === null;
}
}
변수에는 변수를 부여하는 게 아니라 "그 변수의 값"을 부여한다.