-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestStack.java
More file actions
43 lines (37 loc) · 1018 Bytes
/
TestStack.java
File metadata and controls
43 lines (37 loc) · 1018 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
33
34
35
36
37
38
39
40
41
42
43
import java.util.*;
public class TestStack {
public static void main (String[] args) {
Stack<Integer> st = new Stack<Integer>();
st.push(1);
st.push(3);
st.push(5);
st.push(7);
while (!st.empty()) {
System.out.println(st.peek());
st.pop();
}
//Better implentation:
Deque<Integer> st2 = new ArrayDeque<>();
st2.push(1);
st2.push(3);
st2.push(5);
st2.push(7);
System.out.println("Iterating the ArrayDeque:");
// Output should be "7, 5, 3, 1,"
for(Iterator<Integer> iter = st2.iterator(); iter.hasNext();) {
System.out.print(iter.next() + ", ");
}
System.out.println("");
System.out.println("Iterating the ArrayDeque reversely:");
// Output should be "1, 3, 5, 7,"
for(Iterator descItr = st2.descendingIterator();descItr.hasNext();) {
System.out.print(descItr.next() + ", ");
}
System.out.println("");
System.out.println("Popping out all the elements:");
while(!st2.isEmpty()) {
System.out.print(st2.pop() + ", ");
}
System.out.println("");
}
}