-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinkStack.java
71 lines (57 loc) · 1.55 KB
/
LinkStack.java
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
/**
* Created by jkeys on 9/28/2015.
*/
public class LinkStack<T> implements StackInterface<T> {
private SNode<T> top = null;
/**
* Method to push new element to top of stack.
* @param e Element to be pushed. If e is null, return without modifying the stack structure.
*/
public void push(T e) {
if(e == null)
return;
top = new SNode<T>(e, top); //create new node and push to top, while linking up with old top
}
/**
* Method to pop an element off of the top of the stack.
* @return Element popped from the top of the stack.
*/
public T pop() {
if (top == null)
return null;
T result = top.getData(); //Node to return
SNode<T> newTop = top.getNext(); //the new top node
top.setNext(null);
top = newTop;
return result;
}
/**
* Method to peek at the top element of the stack.
* @return The top element of the stack.
*/
public T top() {
if(top == null)
return null;
return top.getData();
}
/**
* Method to get the size of the stack.
* @return The size of the stack.
*/
public int size() {
int count = 0;
SNode<T> rover = top;
while(rover != null) {
count++;
rover = rover.getNext();
}
return count;
}
/**
* Method to return whether the stack is empty.
* @return true if size == 0, else false.
*/
public boolean isEmpty() {
return top == null;
}
}