-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path栈.js
77 lines (77 loc) · 1.5 KB
/
栈.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
/**
* Created by Administrator on 2017/7/4.
*/
//顺序存储栈
class Stack {
constructor (MAXSIZE) {
this.maxsize = MAXSIZE
this.data = []
this.top = - 1
}
push (ele) {
if (this.top == this.maxsize - 1) {
console.log('栈满不可储存!!!')
return
}
this.data[++this.top] = ele
}
pop () {
if (this.top == -1) {
console.log('已栈空!!')
return
}
return this.data.splice(this.top--,1)
}
getTop () {
return this.data[this.top]
}
clear () {
this.top = -1
this.data = []
}
leng () {
return this.top + 1
}
}
//链式栈
function Node(data) {
this.data = data
this.next = null
}
class Stack2 {
constructor () {
this.top = - 1
this.data = null
}
push (ele) {
let node = new Node(ele)
if (this.top == -1) {
this.data = node
} else {
node.next = this.data
this.data = node
}
this.top ++
}
pop () {
if (this.top == -1) {
console.log('栈空!!')
return
} else {
let temp = this.data.data
this.data = this.data.next
this.top--
return temp
}
}
getTop () {
return this.data.data
}
clear () {
this.data = null
this.top = -1
}
leng () {
return this.top + 1
}
}