forked from dr-cs/intro-oop-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStack.java
More file actions
40 lines (32 loc) · 878 Bytes
/
LinkedStack.java
File metadata and controls
40 lines (32 loc) · 878 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
public class LinkedStack<E> extends AbstractStack<E> {
private class Node<E> {
E data;
Node<E> next;
Node(E data, Node<E> next) {
this.data = data;
this.next = next;
}
}
private Node<E> head;
public void push(E item) {
head = new Node<E>(item, head);
}
public E removeNext() {
E answer = head.data;
head = head.next;
return answer;
}
public boolean isEmpty() {
return (head == null);
}
public static void main(String[] args) {
LinkedStack<Integer> ints = new LinkedStack<>();
ints.push(1);
ints.push(2);
ints.push(3);
System.out.println(ints.pop());
System.out.println(ints.pop());
System.out.println(ints.pop());
System.out.println(ints.pop());
}
}