-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
61 lines (46 loc) · 1.01 KB
/
Stack.java
File metadata and controls
61 lines (46 loc) · 1.01 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
package calculator;
public class Stack {
//fields
listNode top;
public void push(String data) {
// making new node
listNode input = new listNode(data);
// if the Stack is empty
if (top == null) {
top = input;
}
// when the Stack is not empty
else {
// link the input listNode to the current Stack
input.next = top;
// move the top
top = input;
}
}
public String pop() {
// string variable to hold the returned value
String returnedValue;
// if the stack only has one element left
if (top.next == null) {
// stored the returned value
returnedValue = top.data;
// set the top to become null
top = null;
return returnedValue;
}
// when there are more than 1 elements left in the stack
else {
// store the returned value
returnedValue = top.data;
// move the top pointer
top = top.next;
return returnedValue;
}
}
public String getTop() {
if (top != null) {
return top.data;
}
return null;
}
}