Nathan Sprague
OpenDSA version:
public interface Stack<E> { // Stack class ADT
// Reinitialize the stack.
public void clear();
// Push "it" onto the top of the stack
public boolean push(E it);
// Remove and return the element at the top of the stack
public E pop();
// Return a copy of the top element (Often called peek)
public E topValue();
// Return the number of elements in the stack
public int length();
// Return true if the stack is empty
public boolean isEmpty();
}
OpenDSA version:
public interface Queue<E> { // Queue class ADT
// Reinitialize queue
public void clear();
// Put element on rear
public boolean enqueue(E it);
// Remove and return element from front
public E dequeue();
// Return front element
public E frontValue();
// Return queue size
public int length();
//Tell if the queue is empty or not
public boolean isEmpty();
}
What will be printed when this code executes?
class AStack<E> implements Stack<E> {
private E stackArray[]; // Array holding stack
private static final int defaultSize = 10;
private int maxSize; // Maximum size of stack
private int top; // First free position at top
// Constructors
@SuppressWarnings("unchecked") // Generic array allocation
AStack(int size) {
maxSize = size;
top = 0;
stackArray = (E[])new Object[size]; // Create stackArray
}
AStack() { this(defaultSize); }
public void clear() { top = 0; } // Reinitialize stack
// Push "it" onto stack
public boolean push(E it) {
if (top >= maxSize) return false;
stackArray[top++] = it;
return true;
}
// Remove and return top element
public E pop() {
if (top == 0) return null;
return stackArray[--top];
}
public E topValue() { // Return top element
if (top == 0) { return null; }
return stackArray[top-1];
}
public int length() { return top; } // Return stack size
}
// Check if the stack is empty
public boolean isEmpty() { return top == 0; }
/*
* OpenDSA Project Distributed under the MIT License
*
* Copyright (c) 2011-2016 - Ville Karavirta and Cliff Shaffer
*/
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
|
pop() |
|
topValue() |
|
length() |
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
\(\Theta(1)\)* |
pop() |
\(\Theta(1)\) |
topValue() |
\(\Theta(1)\) |
length() |
\(\Theta(1)\) |
* Amortized
// Linked stack implementation
class LStack<E> implements Stack<E> {
private Link<E> top; // Pointer to first element
private int size; // Number of elements
// Constructors
LStack() { top = null; size = 0; }
LStack(int size) { top = null; size = 0; }
// Reinitialize stack
public void clear() { top = null; size = 0; }
// Put "it" on stack
public boolean push(E it) {
top = new Link<E>(it, top);
size++;
return true;
}
// Remove "it" from stack
public E pop() {
if (top == null) return null;
E it = top.element();
top = top.next();
size--;
return it;
}
public E topValue() { // Return top value
if (top == null) return null;
return top.element();
}
// Return stack length
public int length() { return size; }
// Check if the stack is empty
public boolean isEmpty() { return size == 0; }
}
/*
* OpenDSA Project Distributed under the MIT License
*
* Copyright (c) 2011-2016 - Ville Karavirta and Cliff Shaffer
*/
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
|
pop() |
|
topValue() |
|
length() |
Big-\(\Theta\) cost?
operation | cost |
---|---|
push() |
\(\Theta(1)\) |
pop() |
\(\Theta(1)\) |
topValue() |
\(\Theta(1)\) |
length() |
\(\Theta(1)\) |
Big-\(\Theta\) cost?
operation | cost |
---|---|
enqueue() |
|
dequeue() |
|
frontValue() |
|
length() |
Big-\(\Theta\) cost?
operation | cost |
---|---|
enqueue() |
\(\Theta(1)\)* |
dequeue() |
\(\Theta(1)\) |
frontValue() |
\(\Theta(1)\) |
length() |
\(\Theta(1)\) |
* Amortized
Which of the following describes the most reasonable organization of a linked queue?
enequeue
and dequeue
are performed at the head. There is no need to maintain a tail reference.
enequeue
is performed at the tail. dequeue
is performed at the head.
enequeue
is performed at the head. dequeue
is performed at the tail.
Both a head and tail reference are required, but it doesn’t matter which is used for enequeue
and which is used for dequeue
.
Big-\(\Theta\) cost?
operation | cost |
---|---|
enqueue() |
|
dequeue() |
|
frontValue() |
|
length() |
Big-\(\Theta\) cost?
operation | cost |
---|---|
enqueue() |
\(\Theta(1)\) |
dequeue() |
\(\Theta(1)\) |
frontValue() |
\(\Theta(1)\) |
length() |
\(\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