-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
79 lines (69 loc) · 1.36 KB
/
Copy pathStack.java
File metadata and controls
79 lines (69 loc) · 1.36 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package cc150.ds.stack;
class Node {
int data;
int curmin;
Node next;
public Node(int val) {
data = val;
next = null;
curmin = Integer.MAX_VALUE;
}
}
public class Stack {
Node top;
int min;
public Stack next;
int cap;
public Stack() {
cap = 0;
top = null;
next = null;
}
public void push(int e) {
Node n = new Node(e);
if(top == null) {
top = n;
min = e;
} else {
if( e < top.curmin ) {
min = e;
}
n.next = top;
top = n;
}
top.curmin = min;
cap++;
}
public Object pop() {
if(top != null) {
int tv = top.data;
top = top.next;
if(top != null) {
min = top.curmin;
}
cap--;
return tv;
}
return null;
}
public Object min() {
if(top != null) {
return min;
}
return null;
}
public Object top() {
return top.data;
}
public void print() {
Node t = top;
while( t != null ) {
System.out.printf("%d ", t.data);
t = t.next;
}
System.out.println();
}
public int getCapacity() {
return cap;
}
}