-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringStackRefBased.java
More file actions
211 lines (181 loc) · 5.25 KB
/
Copy pathStringStackRefBased.java
File metadata and controls
211 lines (181 loc) · 5.25 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Student ID: V00816592
* Date: 07/17/15
* Program Name: StringStackRefBased.java
* Program Description: Referenced base stack Implementation of Strings
* @author Isaac Sahle
*/
public class StringStackRefBased implements StringStack{
static private StringStackNode top;
public StringStackRefBased(){
top = null;
}
/**
* Checks if stack is empty
* @return true if empty, false otherwise
*/
public boolean isEmpty(){
return(top == null);
}
/**
* Computes height of stack (i.e the number of items in the stack)
* @return number of items in stack
*/
public int height(){
StringStackNode curr;
int numItems = 0;
for(curr = top; curr != null; curr = curr.next){
numItems++;
}
return numItems;
}
/**
* Removes item at the top of the stack, if stack is empty,
* StringStackException is thrown
* @return String item at top of the list
*/
public String pop() throws StringStackException{
StringStackNode curr;
//if list is empty throw exception
if(isEmpty()){
throw new StringStackException("List is Empty");
}
//if at this point, at least one item to remove
curr = top;
top = top.next;
return curr.word;
}
/**
* Removes all items from the stack
*/
public void popAll(){
top = null;
}
/**
* Places a new item onto the top of the stack
* @param item the string to be inserted into the list
*/
public void push(String item) throws StringStackException{
StringStackNode insert = new StringStackNode(item);
//RefBased therefore there will never be a issue pushing an item on the list
if(false){
throw new StringStackException("List is full");
}
//insert at the top of the list
insert.next = top;
top = insert;
}
/**
* Just return the value currently at the top of the stack, leaving stack unchanged
* @return String item at top of the list
*/
public String peek() throws StringStackException{
if(isEmpty()){
throw new StringStackException("List is empty");
}
return (top.word);
}
public static void main(String[] args) {
StringStackRefBased test = new StringStackRefBased();
String one = "Isaac";
String two = "Emily";
String three = "Steve";
//Test behavior after initializing stack
if(test.isEmpty()){
System.out.println("Test 1 (isEmpty): passed");
}else{
System.out.println("Test 1 (isEmpty): FAILED");
}
//Test behavior of height
if(test.height() == 0){
System.out.println("Test 2 (height): passed");
}else{
System.out.println("Test 2 (height): FAILED");
}
//Test behavior popping from empty list
boolean compare = false;
try{
test.pop();
}
catch(StringStackException e) {
compare = true;
}
if(compare){
System.out.println("Test 3 (pop empty list): passed");
}else{
System.out.println("Test 3 (pop empty list): FAILED");
}
//Test behavior of peek
compare = false;
try{
test.peek();
}
catch(StringStackException e) {
compare = true;
}
if(compare){
System.out.println("Test 4 (peek at empty list): passed");
}else{
System.out.println("Test 4 (peek at empty list): FAILED");
}
//Test behavior after pushing one item on list
try{
test.push(one);
}
catch(StringStackException e) {
System.out.println(e);
}
if(!(test.isEmpty())){
System.out.println("Test 5 (push one item): passed");
}else{
System.out.println("Test 5 (push one item): FAILED");
}
//Test behavior after pushing two more items on list
try{
test.push(two);
test.push(three);
}
catch(StringStackException e) {
System.out.println(e);
}
if(test.height() == 3){
System.out.println("Test 6 (push two more items): passed");
}else{
System.out.println("Test 6 (push two more items): FAILED");
}
//Test behavior of pop
compare = true;
try{
test.pop();
}
catch(StringStackException e) {
System.out.println(e);
compare = false;
}
if(compare && test.height() == 2){
System.out.println("Test 7 (pop item): passed");
}else{
System.out.println("Test 7 (pop item): FAILED");
}
//Test behavior of peek
String temp = null;
try{
temp = test.peek();
}
catch(StringStackException e) {
System.out.println(e);
}
if(temp.equals("Emily")){
System.out.println("Test 8 (peek at item): passed");
}else{
System.out.println("Test 8 (peek at item): FAILED");
}
//Test behavior of popAll
test.popAll();
if(test.isEmpty()){
System.out.println("Test 9 (popAll items from list): passed");
}else{
System.out.println("Test 9 (popAll items from list): FAILED");
}
}
}