-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
57 lines (49 loc) · 1.13 KB
/
Copy pathStack.java
File metadata and controls
57 lines (49 loc) · 1.13 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
import java.util.*;
public class Stack
{
/*******************************************
* STACK DATA STRUCTURE
*******************************************/
private ArrayList <String> val;
// Creates a stack arraylist
public Stack()
{
val = new ArrayList <String> ();
}
// Removes the topmost element from the stack
public String pop()
{
String s = null;
if (!val.isEmpty())
{
s = val.get(val.size() - 1);
val.remove(val.size() - 1);
}
return s;
}
// Adds an element onto the top of the stack
public void push(String s)
{
val.add(s);
}
// Peeks or returns the token on the top of the stack
public String peek()
{
return val.get(val.size() - 1);
}
// Empties the stack
public boolean isEmpty()
{
return 0 == val.size();
}
// Returns the size of the stack
public int getSize()
{
return val.size();
}
// Retrieves the token of a specific index on the stack
public String getValue (int index)
{
return val.get(index);
}
}