This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ce “Queue” with “Stack” and “enqueue” with “push”)
uu y e rceQee)
/ oplieerr a’ idmxieoa nQee
/Cmietm ro:cntfn aSzSFri uu
Takduu y e uu(;
/ oplieerr ttctp sasbls ftednmctp
/Cmietm ro:sai yei ucaso h yai ye
uu y e rceQee)
/ ohTakduu.nuu( n uu.nuu( r ald
For the first part, the most common mistake was getting it completely wrong by saying that
there’s no error. The error is in the second line remember that Java checks the static class
during compiletime, and the dynamic class during runtime. Since myQ is of static type Queue
and Queues don’t have maxSizeSoFar(), there’s a compiletime error.
For the second part, the most common mistake was being confused about runtime error vs
compile time error. Or rather, they detected the runtime error in the second line if the first line
was allowed to compile. However, the first line doesn’t compile because saying that a Queue
“isa” TrackedQueue is false—not all Queues are TrackedQueues. Java will complain about
In the third part, most people didn’t realize that both TrackedQueue’s enqueue() andQueue’s
enqueue() are called. First, TrackedQueue’s enqueue() method is called, which, if you
implemented 1a correctly makes a call to super.enqueue(). Question 2:
Sample Solution (Version A):
A, Enqueue: linear time; need to free up a slot by shifting all elements by 1 slot.
A, Dequeue: constant time; simply call ArrayList.remove(N1).
Sample Solution (Version B):
A, Enqueue: constant time; simply call ArrayList.add() at the back of the queue
A, Dequeue: linear time; remove the first element, then shift all other elements by 1 slot.
Sample Solution (Both Versions):
B, Enqueue: constant time; adding to the front of a linked list takes constant tim...
View Full Document
- Summer '11
- Data Structures