-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackMain.java
64 lines (59 loc) · 1.55 KB
/
StackMain.java
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 {
private Node next;
private int value;
int getValue() {
return this.value;
}
void setValue(int v) {
this.value = v;
}
Node getNext() {
return this.next;
}
void setNext(Node n) {
this.next = n;
}
}
class Stack {
Node start;
void printStack() {
Node n = this.start;
while(n != null) {
int v = n.getValue();
Debug.debug(v);
n = n.getNext();
}
}
void push(int v) {
Node new_node = new Node();
new_node.setValue(v);
new_node.setNext(this.start);
this.start = new_node;
}
int peak() {
return this.start.getValue();
}
int pop() {
int v = this.start.getValue();
this.start = this.start.getNext();
return v;
}
}
class StackMain {
static void main(String[] args) {
Stack s = new Stack();
int i = 0;
while (i < 20) {
s.push(i);
i = i + 1;
}
s.printStack();
while (i > 5) {
s.pop();
i = i - 1;
}
s.printStack();
int v = s.peak();
Debug.debug(v);
}
}