Hint: Check for null before accessing front.prev [OK]
Common Mistakes:
Assuming front is never null after deletion
Ignoring edge case of single element
Confusing exception types
5. You want to implement a dequeue using a linked list that supports these operations efficiently: 1. Insert at front 2. Insert at rear 3. Delete from front 4. Delete from rear 5. Get size of dequeue
Which data members should your dequeue class maintain to achieve all operations in O(1) time?
hard
A. Pointer to rear node only, no size counter
B. Pointer to front node only, and an integer size counter
C. Pointers to front and rear nodes, and an integer size counter
D. Array to store elements and a size counter
Solution
Step 1: Identify pointers needed for O(1) insert/delete at both ends
We need both front and rear pointers to access both ends directly.
Step 2: Identify how to get size in O(1)
Maintain an integer size counter updated on each insert/delete.
Final Answer:
Pointers to front and rear nodes, and an integer size counter -> Option C