-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
46 lines (39 loc) · 752 Bytes
/
Copy pathstack.js
File metadata and controls
46 lines (39 loc) · 752 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
/**
栈的链表实现
*/
class Node{
constructor(item){
this.item =item
this.next = null
}
}
class Stack{
constructor(item){
this.first = new Node(item)
this.cur = this.first
this.N = 0
}
isEmpty(){
return this.first==null
}
size(){
return this.N
}
push(val){
let oldFirst = new Node()
oldFirst = this.first
this.first = new Node(val)
this.first.item =val
this.first.next = oldFirst
this.N++
}
pop(){
let val = this.first.item
this.first = this.first.next
this.N--
return item
}
}
let stack = new Stack(0)
stack.push(1)
console.log(stack.size())