-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.js
107 lines (90 loc) · 2.06 KB
/
stack.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Stack Implementation
class Stack {
constructor() {
this.items = [];
}
// Add element to the stack from the top
push(element) {
this.items.push(element);
}
// Remove element from the top of the stack
pop() {
if (this.items.length === 0) return "Underflow";
return this.items.pop();
}
// Return the top element of the stack
peek() {
return this.items[this.items.length - 1];
}
// Check if the stack is empty
isEmpty() {
return this.items.length === 0;
}
// Return the stack as a string
printStack() {
let str = "";
for (let i = 0; i < this.items.length; i++) str += this.items[i] + " ";
return str;
}
// Return the size of the stack
size() {
return this.items.length;
}
// Clear the stack
clear() {
this.items = [];
}
// Return the stack
getStack() {
return this.items;
}
}
Stack.push(10);
Stack.push(20);
Stack.push(30);
console.log(Stack.printStack());
console.log(Stack.pop());
console.log(Stack.printStack());
console.log(Stack.peek());
console.log(Stack.isEmpty());
console.log(Stack.size());
console.log(Stack.getStack());
Stack.clear();
console.log(Stack.printStack());
console.log(Stack.isEmpty());
console.log(Stack.size());
console.log(Stack.getStack());
// Implement Stack using Queues
// Implement a last -in -first - out(LIFO) stack using only two queues.
// The implemented stack should support all the functions of a stack (push, top, pop, and empty).
var MyStack = function () {
this.q1 = [];
this.q2 = [];
};
// q1 - [4,2,3,5]
// q2 - []
MyStack.prototype.push = function (x) {
while (this.q1.length !== 0) {
this.q2.push(this.q1.shift());
}
this.q1.push(x);
while (this.q2.length !== 0) {
this.q1.push(this.q2.shift());
}
};
MyStack.prototype.pop = function () {
return this.q1.shift();
};
MyStack.prototype.top = function () {
return this.q1[0];
};
MyStack.prototype.empty = function () {
return this.q1.length === 0;
};
var stack = new MyStack();
stack.push(3);
stack.push(5);
stack.push(96);
stack.push(24);
stack.pop();
console.log(stack.top());