-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackLinkedList2.js
63 lines (46 loc) · 895 Bytes
/
stackLinkedList2.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
class stack{
constructor(max){
this.max=max
this.item=[]
}
empty(){
return this.item.length===0
}
push(element){
if(this.item.length===this.max){
console.log("stack overflow")
}else{
this.item.push(element)
}
}
pop(){
if(this.empty()){
console.log("under flow")
}else{
this.item.pop()
}
}
peek(){
if(this.empty){
return "empty"
}else{
console.log( this.item[ this.item.length-1])
}
}
print(){
let str=""
for(let i=0;i<this.item.length;i++){
str+=this.item[i]+" "
}
console.log(str)
}
}
let st=new stack(3)
st.push(1)
st.push(2)
st.push(3)
st.push(4)
st.pop()
st.push(5)
// st.pop()
st.print()