Nathan Sprague
Operation | Description |
---|---|
Push(stack, x) | Inserts x on top of stack |
Pop(stack) | Returns and removes item at top of stack |
Peek(stack) | Returns but does not remove item at top of stack |
IsEmpty(stack) | Returns true if stack has no items |
GetLength(stack) | Returns the number of items in the stack |
Operation | Description |
---|---|
Enqueue(queue, x) | Inserts x at end of the queue |
Dequeue(queue) | Returns and removes item at front of queue |
Peek(queue) | Returns but does not remove item at the front of the queue |
IsEmpty(queue) | Returns true if queue has no items |
GetLength(queue) | Returns the number of items in the queue |
What will be printed when this code executes?
Operation | Description |
---|---|
PushFront(deque, x) | Inserts x at the front of the deque |
PushBack(deque, x) | Inserts x at the back of the deque |
PopFront(deque) | Returns and removes item at front of deque |
PopBack(deque) | Returns and removes item at back of deque |
PeekFront(deque) | Returns but does not remove the item at the front of deque |
PeekBack(deque) | Returns but does not remove the item at the back of deque |
IsEmpty(deque) | Returns true if the deque is empty |
GetLength(deque) | Returns the number of items in the deque |
Stack
interface?
List
ArrayList
or LinkedList
public class ArrayStack<E> implements Stack<E> {
private static final int INITIAL_CAPACITY = 1;
private E[] array;
private int size;
@SuppressWarnings("unchecked")
public ArrayStack() {
array = (E[]) new Object[INITIAL_CAPACITY];
size = 0;
}
public void push(E item) {
if (size == array.length) {
resize();
}
array[size] = item;
size++;
}
public E pop() {
E toReturn = array[size - 1];
size--;
array[size] = null; // WHY????
return toReturn;
// return array[--size]; // could be one line.
}
@Override
public E peek() {
return array[size - 1];
}
@Override
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
private void resize() {
int newSize = array.length * 2;
@SuppressWarnings("unchecked")
E[] newArray = (E[]) new Object[newSize];
for (int i = 0; i < size; i++) {
newArray[i] = array[i];
}
array = newArray;
}
}
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
|
pop() |
|
peek() |
|
size() |
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
\(\Theta(1)\)* |
pop() |
\(\Theta(1)\) |
peek() |
\(\Theta(1)\) |
size() |
\(\Theta(1)\) |
* Amortized
public class LinkedStack<E> implements Stack<E>{
private Node top; // No need for a tail reference!
private int size;
private class Node {
E item;
Node next;
public Node(E item, Node next) {
this.item = item;
this.next = next;
}
}
@Override
public void push(E item) {
top = new Node(item, top);
size++;
}
@Override
public E pop() {
E toReturn = top.item;
top = top.next;
size--;
return toReturn;
}
@Override
public E peek() {
return top.item;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public int size() {
return size;
}
}
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
|
pop() |
|
peek() |
|
size() |
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
\(\Theta(1)\) |
pop() |
\(\Theta(1)\) |
peek() |
\(\Theta(1)\) |
size() |
\(\Theta(1)\) |
for each token:
if the token is an operand:
Push the token on the stack
else: // token is an operator
Pop two items
Perform the operation
Push the result on the stack
return the top of the stack
Which of the following represents one of the stack configurations that is reached during evaluation? (Top of the stack is on the left.)
A) 2 4 8 \(\div\) \(\times\)
B) 3 7 1
C) 2 4 8
D) 4 8 1