-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
51 lines (41 loc) · 1.25 KB
/
Stack.java
File metadata and controls
51 lines (41 loc) · 1.25 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
/**
* A block based implementation of the Stack data structure.
* @author Samuel Heath 21725083, Bryan Trac 21704976
*/
public class Stack {
private Link first;
public Stack() {
first = new Link(null, null);
}
public void push(Object o) {
first = new Link(o, first);
}
public Object examine() throws Exception {
if (!isEmpty()) {
return first.item;
} else throw new Exception("Not enough elements in the list to examine");
}
public void delete() throws Exception {
if (!isEmpty()) {
first = first.successor;
} else throw new Exception("Not enough elements in the list to delete");
}
public Object pop() throws Exception {
if (!isEmpty()) {
Object item = first.item;
first = first.successor;
return item;
} else throw new Exception("Not enough elements in the list to pop");
}
public boolean isEmpty() {
return first == null;
}
private class Link {
private Object item;
private Link successor;
public Link(Object item, Link successor) {
this.item = item;
this.successor = successor;
}
}
}