-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathStackExample.java
63 lines (45 loc) Β· 1.75 KB
/
StackExample.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
package Stack;
public class StackExample {
public static void main(String[] args) {
{
ArrayStack<Integer> stack = new ArrayStack<>();
stack.push(5);
stack.push(3);
System.out.println(stack.size()); // 2
System.out.println(stack.pop()); // 3
System.out.println(stack.isEmpty()); // false
System.out.println(stack.pop()); // 5
System.out.println(stack.isEmpty()); // true
System.out.println(stack.pop()); // null
stack.push(7);
stack.push(9);
System.out.println(stack.peek()); // 9
stack.push(4);
System.out.println(stack.size()); // 3
System.out.println(stack.pop()); // 4
stack.push(6);
stack.push(8);
System.out.println(stack.pop()); // 8
}
{
LinkedStack<Integer> stack = new LinkedStack<>();
stack.push(5);
stack.push(3);
System.out.println(stack.size()); // 2
System.out.println(stack.pop()); // 3
System.out.println(stack.isEmpty()); // false
System.out.println(stack.pop()); // 5
System.out.println(stack.isEmpty()); // true
System.out.println(stack.pop()); // null
stack.push(7);
stack.push(9);
System.out.println(stack.peek()); // 9
stack.push(4);
System.out.println(stack.size()); // 3
System.out.println(stack.pop()); // 4
stack.push(6);
stack.push(8);
System.out.println(stack.pop()); // 8
}
}
}