Skip to content

Instantly share code, notes, and snippets.

@djo
Last active April 9, 2017 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djo/8433466 to your computer and use it in GitHub Desktop.
Save djo/8433466 to your computer and use it in GitHub Desktop.
Mistake in the Cracking Coding Interview 5th edition book: loitering problem in the queue implementation.
// Enhanced and fixed the dequeue method
if (first == null) return null;
Object item = first.data;
first = first.next;
if (first == null) last = null;
return item;
Book: Cracking Coding Interview 5th edition
Chapter 3: Stacks and Queues
Section: Implementing a Queue
Page: 80

The "last" reference should be nullified if a queue becomes empty in the "dequeue" method:

17: first = first.next;
++: if (first == null) last = null;
18: return item;

Otherwise "last" item will not be cleared by the GC on an empty stack.

@djo
Copy link
Author

djo commented Mar 4, 2014

Received a reply from Gayle via email about accepting the patch: "Great observation. I've updated this for the next printing.".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment