-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
37 lines (30 loc) · 740 Bytes
/
Copy pathStack.java
File metadata and controls
37 lines (30 loc) · 740 Bytes
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
public class Stack<T> {
private T data;
private SinglyLinkedList<T> top;
public void push(T data) {
SinglyLinkedList<T> newNode = new SinglyLinkedList<T>(data);
newNode.setNextNode(top);
top = newNode;
}
public T pop() {
if (top == null)
return null;
SinglyLinkedList<T> returnNode = top;
top = top.getNextNode();
returnNode.setNextNode(null); // D : is it necessary to do this step ?
return returnNode.getData();
}
public T peek() {
return top.getData();
}
public void display() {
SinglyLinkedList<T> currentNode = top;
while (currentNode != null) {
System.out.print(currentNode.getData());
currentNode = currentNode.getNextNode();
}
}
public boolean isEmpty() {
return top==null;
}
}