-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
62 lines (53 loc) · 916 Bytes
/
stack.js
File metadata and controls
62 lines (53 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Node {
constructor(data, next) {
this.data = data;
this.next = next;
}
getData() {
return this.data;
}
}
class Stack {
constructor() {
this.top = null;
this.size = 0;
}
push(element) {
if (this.top == null) {
this.top = new Node(element);
} else {
let newNode = new Node(element);
newNode.next = this.top;
this.top = newNode;
}
this.size++
}
pop() {
if (this.top == null) {
return null;
}
let p = this.top;
this.top = this.top.next;
p.next = null;
this.size--
return p
}
size() {
return this.size;
}
}
function print(stack) {
console.log('TOP')
let node = null;
while ((node = stack.pop()) != null) {
console.log(node.getData())
}
console.log("END")
}
let stack = new Stack()
stack.push("A")
stack.push("B")
stack.push("C")
stack.push("D")
stack.pop()
print(stack)