-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
64 lines (52 loc) · 1.47 KB
/
stack.js
File metadata and controls
64 lines (52 loc) · 1.47 KB
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
class Stack {
constructor() {
this.items = [];
}
push(item) {
// this.items[this.items.length] = item;
this.items.push(item)
}
pop() {
if (this.isEmpty()) {
return "Stack is empty"
}
// let item = this.items[this.items.length - 1];
// this.items.splice(this.items.length - 1, 1)
// return item
return this.items.pop();
}
isEmpty() {
return this.items.length === 0
}
peek() {
return this.items[this.items.length - 1];
}
search(item) {
// for (let i = 0; i < this.items.length - 1; i++) {
// if (this.items[i] === item) {
// return i;
// }
// }
// return null
let index = this.items.indexOf(item);
return (index !== -1) ? index : null;
}
print() {
return this.items.toString();
}
}
let stack = new Stack();
console.log('Is stack Empty : ', stack.isEmpty());
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
console.log('Is stack Empty : ', stack.isEmpty());
console.log('Print the stack : ', stack.print())
console.log('Lookup for peek value : ', stack.peek())
console.log('Pop out top value : ', stack.pop())
console.log('Lookup for peek value : ', stack.peek())
console.log('Search index for value 3 : ', stack.search(3))
console.log('Search index for value 10 : ', stack.search(10))
console.log('Print the stack : ', stack.print())