/
P4_2StackTest.java
51 lines (44 loc) · 1.42 KB
/
P4_2StackTest.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
//Program P4.2
import java.util.*;
public class P4_2StackTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Stack S = new Stack();
System.out.printf("Enter some integers ending with 0\n");
int n = in.nextInt();
while (n != 0) {
S.push(n);
n = in.nextInt();
}
System.out.printf("\nNumbers in reverse order\n");
while (!S.empty())
System.out.printf("%d ", S.pop());
System.out.printf("\n");
} //end main
} //end P4_2StackTest
class Node {
int data;
Node next;
public Node(int d) {
data = d;
next = null;
}
} //end class Node
class Stack {
final static int RogueValue = -999999;
Node top = null;
public boolean empty() {
return top == null;
}
public void push(int n) {
Node p = new Node(n);
p.next = top;
top = p;
} //end push
public int pop() {
if (this.empty()) return RogueValue; //a symbolic constant
int hold = top.data;
top = top.next;
return hold;
} //end pop
} //end class Stack