-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALStack.java
More file actions
121 lines (106 loc) · 3.62 KB
/
ALStack.java
File metadata and controls
121 lines (106 loc) · 3.62 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
Jason Chua
APCS2 pd5
HW22 -- Standardization
2016-03-31
*/
/*****************************************************
* skeleton for class Latkes
* Implements stack of Strings using array as underlying container.
* new in Version 2: typed for generics
*****************************************************/
import java.util.ArrayList;
public class ALStack<T> implements Stack<T> {
private ArrayList<T> _stack;
private int _stackSize;
//constructor
public ALStack() {
_stack = new ArrayList<T>();
_stackSize = 0;
}
//overloaded constructor allows for intial capacity declaration
public ALStack( int size ) {
this();
_stack = new ArrayList<T>(size);
}
public boolean isEmpty() {
return _stackSize == 0;
}
public boolean isFull() {
return _stackSize == _stack.size();
}
public T peek() {
if (isEmpty())
return null;
return _stack.get(_stackSize-1);
}
public T pop() {
if (isEmpty())
return null;
T ret = _stack.remove(_stackSize-1);
_stackSize--;
return ret;
}
public void push(T x) {
_stack.add(x);
_stackSize++;
}
//main method for testing
public static void main( String[] args ) {
Latkes<String> tastyStack = new Latkes<String>(10);
tastyStack.push("aoo");
tastyStack.push("boo");
tastyStack.push("coo");
tastyStack.push("doo");
tastyStack.push("eoo");
tastyStack.push("foo");
tastyStack.push("goo");
tastyStack.push("hoo");
tastyStack.push("ioo");
tastyStack.push("joo");
tastyStack.push("coocoo");
tastyStack.push("cachoo");
//cachoo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//coocoo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//joo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//ioo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//hoo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//goo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//foo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//eoo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//doo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//coo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//boo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//aoo
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
//stack empty by now; SOP(null)
System.out.println( "peek: " + tastyStack.peek() );
System.out.println( "pop: " + tastyStack.pop() );
System.out.println( tastyStack.pop() );
/*v~~~~~~~~~~~~~~MAKE MORE~~~~~~~~~~~~~~v
^~~~~~~~~~~~~~~~AWESOME~~~~~~~~~~~~~~~^*/
}//end main()
}//end class Latkes