-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseStack.java
More file actions
32 lines (28 loc) · 976 Bytes
/
Copy pathReverseStack.java
File metadata and controls
32 lines (28 loc) · 976 Bytes
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
import java.util.Stack;
public class ReverseStack {
//we are making 3 stacks here in first stack all elements are push and then in 2nd stack all get reversed and then in the 3rd stack they all get revesemeans original stack and then after that that elements filled into the 1st stack itself
public static void main(String[] args) {
Stack<Integer>st=new Stack<>();
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
System.out.println(st);
Stack<Integer>rt=new Stack<>();
while(st.size()>0){
rt.push(st.pop());
}
System.out.println(rt);
Stack<Integer>gt=new Stack<>();
while(rt.size()>0){
gt.push(rt.pop());
}
System.out.println(gt);
while(gt.size()>0){
st.push(gt.pop());
}
System.out.println(st);
}
}