-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedListStack.java
More file actions
41 lines (34 loc) · 838 Bytes
/
LinkedListStack.java
File metadata and controls
41 lines (34 loc) · 838 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
38
39
40
41
public class LinkedListStack<T> implements StackInterface {
private ListNode head;
private class ListNode {
private T data;
private ListNode link;
public ListNode(T aData, ListNode aLink) {
data = aData;
link = aLink;
}//ListNode
}//ListNode
public void push(Object data) {
ListNode newNode = new ListNode((T)data,head);
head = newNode;
}//push
public T pop() {
if(head == null) return null;
T retVal = head.data;
head = head.link;
return retVal;
}//pop
public T peek() {
if(head == null) return null;
return head.data;
}//peek
public void print() {
ListNode temp = head;
while(temp != null) {
System.out.println(temp.data.toString());
temp = temp.link;
}//while
}//print
public void nothing() {
}
}//LinkedListStack