-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-stack.js
More file actions
51 lines (41 loc) · 840 Bytes
/
Copy path05-stack.js
File metadata and controls
51 lines (41 loc) · 840 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
class stack{
constructor(){
this.s = []
}
length(){
return this.s.length
}
push(value){
this.s.push(value)
}
peek(){
if(this.s.length === 0){
throw new Error("Stack is empty");
}else{
return this.s[this.s.length - 1]
}
}
pop(){
return this.s.pop()
}
search(value){
for(let i = this.s.length-1; i>=0; i--){
if(this.s[i] === value){
return this.s.length - i
}
}
return -1
}
}
let stk = new stack()
stk.push(10)
stk.push(20)
stk.push(30)
stk.push(40)
stk.push(50)
// console.log(stk.length())
// console.log(stk.peek())
// console.log(stk.pop())
// console.log(stk.length())
// console.log(stk.peek())
console.log(stk.search(30))