Nathan Sprague
9/22/17
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();
}
What will be printed when this code executes:
Queue<String> queue = new LQueue<>();
queue.enqueue("A");
queue.enqueue("B");
queue.enqueue("C");
queue.dequeue();
queue.enqueue("D");
while (queue.length() != 0) {
System.out.println(queue.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)\) |
* 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)\) |