15 - CMPSCI 187: Programming With Data Structures Lecture...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #15: Implementing Queues 12 October 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementation of Queues • Linked List Implementation • Circular Array Implementation • Modular Arithmetic • The Java Queue Classes
Background image of page 2
• We want to be able to add elements to the queue (with enqueue ), remove and return the element of the queue that has been there longest (with dequeue ), look at that longest-present element without removing it (with first ), test whether the queue is empty (with isEmpty ), and return how many elements are in the queue (with size ). • The only kind of exception we want to throw from these methods is an EmptyCollectionException , in the case where we try to dequeue or look at the Frst element and the queue is empty. • L&C’s QueueADT interface also includes the toString method. This isn’t fundamental to the operation of the queue, but it’s a useful reminder that wherever we put the data in our implementation, we must be able to Fnd it. • Naturally toString is going to take O(n) time on a queue of n elements, but we we would like the other operations to take only O(1) time.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Linked List Implementation • As with the stack, we can reasonably hope for O(1) implementations of all Fve basic methods, counting on the ±exibility of linked lists to avoid the need for time-consuming resizing. • Our basic linear linked list of LinearNode objects has a start, which we’ll now call the front , from which we can reach all the nodes in the list by following next pointers. But we need to take actions at both ends of the list, so we will also keep a pointer to the
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

15 - CMPSCI 187: Programming With Data Structures Lecture...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online