public Stack() {
items = new Object[INITSIZE];
numitems = 0;
}
public Object pop() throws EmptyStackException {
if (numItems == 0) throw new EmptyStackException();
else {
numItems--;
return items[numitems];
}
}
|
OPERATION |
WORST-CASE TIME |
AVERAGE-CASE TIME |
|
constructor |
O(1) |
O(1) |
|
isEmpty |
O(1) |
O(1) |
|
size |
O(1) |
O(1) |
|
push |
O(N) |
O(1) |
|
pop |
O(1) |
O(1) |
|
peek |
O(1) |
O(1) |
public void push(Object ob) {
items = new Listnode(ob, items);
numItems++;
}
|
OPERATION |
WORST-CASE TIME |
|
constructor |
O(1) |
|
isEmpty |
O(1) |
|
size |
O(1) |
|
push |
O(1) |
|
pop |
O(1) |
|
peek |
O(1) |
public static void reverseQ(Queue q) {
// precondition: q contains x1 x2 ... xN (with x1 at the front)
// postcondition: q contains xN ... x2 X1 (with xN at the front)
Stack s = new Stack();
while (!q.isEmpty()) s.push(q.dequeue());
while (!s.isEmpty()) q.enqueue(s.pop());
}