-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringStack.java
More file actions
112 lines (98 loc) · 3.27 KB
/
StringStack.java
File metadata and controls
112 lines (98 loc) · 3.27 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
/*
* Copyright (C) 2019 Daniel McGuinness
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package stack;
import java.util.ArrayList;
/**
* An example class that makes use of a Stack data structure built atop an
* ArrayList which is used to store Strings.
*
* A Stack makes use of the LIFO (Last In, First Out) principle, whereby data
* that was last pushed to the Stack is the data that is removed first from the
* Stack.
*
* @author Daniel McGuinness
*/
public class StringStack {
ArrayList<String> stk;
int curpos;
public StringStack(){
stk = new ArrayList();
curpos = -1;
}
/**
* Push a String value to the top of the Stack.
*
* @param str String to be added to the top of the Stack.
* @return Was the value pushed successfully?
*/
public boolean push(String str){
//This implementation disallows nulls and empty strings.
if(str.equals("") | str.equals(null)){
return false;
}
stk.add(str);
curpos++;
return true;
}
/**
* Pops a String value; returning it and then removing it from the Stack.
*
* @return Top value of the Stack which has been popped.
*/
public String pop(){
//Simple error prevention, return null instead of causing runtime error.
if(curpos == -1){
return null;
}
String str = stk.get(curpos);
stk.remove(curpos--); //postfix deincrement, original value is used.
return str;
}
/**
* Peeks at the value on the top of the Stack but doesn't remove it from the
* structure.
*
* @return Top value of the stack. Returns null if empty stack.
*/
public String peek(){
//Return null if no values on stack.
if(curpos == -1){
return null;
}
return stk.get(curpos);
}
/**
* Removes every String value currently held within the Stack. Also resets
* the current position variable, 'curpos'.
*/
public void clear(){
stk.clear();
curpos = -1;
}
/**
* Retrieve the total number of Strings currently stored on the Stack.
*
* An alternative solution to this is to use a running total every time a
* value is popped or pushed, similar to the 'curpos' value (or just the
* value itself).
*
* @return Number of values stored in the Stack.
*/
public int size(){
return stk.size();
}
}