-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStackClient.java
More file actions
60 lines (52 loc) · 1.06 KB
/
StackClient.java
File metadata and controls
60 lines (52 loc) · 1.06 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
package lecture9a13;
public class StackClient {
public static void main(String[] args) throws Exception {
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.display();
// s.pop();
// s.pop();
// s.pop();
// s.display();
// System.out.println(s.peek());
// s.pop();
// s.pop();
// System.out.println();
Reversedisplay(s);
Stack temp = new Stack();
s.display();
ReverseStack(s, temp);
//
s.display();
}
/// reverse display
public static void Reversedisplay(Stack s) throws Exception {
if (s.isEmpty())
return;
int a = s.pop();
Reversedisplay(s);
System.out.println(a);
s.push(a);
}
/// reverse actual stack
public static void ReverseStack(Stack s, Stack temp) throws Exception {
if (s.isEmpty()) {
filreverseStack(s, temp);
return;
}
int a = s.pop();
temp.push(a);
ReverseStack(s, temp);
}
public static void filreverseStack(Stack s, Stack temp) throws Exception {
if (temp.isEmpty())
return;
int a = temp.pop();
filreverseStack(s, temp);
s.push(a);
}
}