-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
59 lines (40 loc) · 978 Bytes
/
scripts.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
/**
* Implementation of Stacks In ES6
*/
class Stack {
//initialize an empty stack
constructor(){
this.items = [];
}
// push inside stack
push(value){
this.items.push(value);
}
// pop from stack and return popped value
pop(){
return (this.items.length === 0)? "Underflow" : this.items.pop();
}
// peek stack
peek(){
return this.items[this.items.length - 1];
}
// check if stack is empty
length(){
return this.items.length;
}
// traverse the stack and print all values
traverse(){
for(let i = 0, length = this.items.length; i < length; i++){
console.log(this.items[i]);
}
}
// search a value in stack
search(value){
for(let i = 0, length = this.items.length; i < length; i++){
if(this.items[i] === value) {
return true;
}
}
return false;
}
}