-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
41 lines (31 loc) · 823 Bytes
/
Stack.java
File metadata and controls
41 lines (31 loc) · 823 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
import java.util.NoSuchElementException;
public class Stack {
private StackElement head = null;
private static class StackElement {
Object value;
StackElement next;
StackElement(Object value)
{
this.value = value;
}
}
public boolean isEmpty(){
return head == null;
}
public void push(Object value){
StackElement newElement = new StackElement(value);
newElement.next = head;
head = newElement;
}
public Object pop(){
if (isEmpty()) {
throw new NoSuchElementException("cannot pop from an empty stack");
}
StackElement returnedValue = head;
head = returnedValue.next;
return returnedValue.value;
}
public void clear(){
head = null;
}
}