-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
executable file
·30 lines (24 loc) · 930 Bytes
/
Stack.java
File metadata and controls
executable file
·30 lines (24 loc) · 930 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
/** A stack is a list that follows the last-in-first-out (LIFO)
* principle. An example is a stack of cafeteria trays. You take
* the top one, which is the last one that the staff put in.*/
public interface Stack<T> {
/** Push e on this stack. */
public void push(T e);
/** Pop the top element of this stack and return it.
* Throw RuntimeException if the stack is empty */
public T pop();
/** Return true iff this stack contains e. */
public boolean contains(T e);
/** Return the number of elements in this stack. */
public int size();
}
/** Stack cell (a helper class; visible to other classes in same package). */
class StackCell<T> {
public T datum; // Data for this cell
public StackCell<T> next; // Next cell in this Stack
/** Constructor: an instance with datum d and next cell next. */
public StackCell(T d, StackCell<T> next) {
datum= d;
this.next= next;
}
}