-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminStack.js
More file actions
64 lines (60 loc) · 1.18 KB
/
minStack.js
File metadata and controls
64 lines (60 loc) · 1.18 KB
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
class Node{
constructor(data,next = null){
this.data = data
this.next = next
}
}
class Stack {
constructor(){
this.first = null
this.last = null
this.size = 0;
this.min = [null];
}
push(data){
let node = new Node(data)
if (!this.first){
this.first = node
this.last = node
this.min[0]= node.data
return this.size++
}
let temporary = this.first
this.first = node
if (this.first.data < this.min[0]){
this.min[0] = this.first.data
}
this.first.next = temporary
return this.size++
}
minimum(){
if (this.min){
return this.min[0]
} else {
return null;
}
}
pop(){
if (!this.first){
return null
}
if (this.first == this.last){
this.first = null;
this.last = null;
}
var temp = this.first
this.first = this.first.next
this.size--
return temp.data
}
peek(){
return this.first.data
}
}
let stack = new Stack();
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(0)
stack.push(-1)
console.log(stack)