-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringStack.java
More file actions
43 lines (37 loc) · 1.06 KB
/
StringStack.java
File metadata and controls
43 lines (37 loc) · 1.06 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
public class StringStack {
private Node<String> topOfStack;
public StringStack(){
// nothing on the stack
this.topOfStack = null;
} // end constructor
public void push(String data){
Node<String> newNode = new Node<String>(data, topOfStack);
this.topOfStack = newNode;
} // end push
public String pop(){
String value;
if(this.isEmpty()){
value = "stack empty";
} else {
value = this.topOfStack.getData();
this.topOfStack = this.topOfStack.getNext();
}
return value;
} // end pop
public boolean isEmpty(){
if(this.topOfStack == null){
return true;
} else {
return false;
}
} // end isEmpty
public static void main(String[] args){
StringStack s = new StringStack();
// should be empty
System.out.println(s.pop());
s.push("V0");
s.push("V1");
System.out.println(s.pop());
System.out.println(s.pop());
} // end main
} // end Stack