Queues - Lecture 14: Queues PIC 10B Todd Wittman The Queue...

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

View Full Document Right Arrow Icon
1 Lecture 14: Queues PIC 10B Todd Wittman The Queue Concept r A queue is a list of items with a defined front and end. r Entering : A data element can only be inserted at the end of the queue ( enqueue ). r Leaving : A data element only be removed from the front of the queue ( dequeue ). r Ex A line of people (queue is British for line) r A queue is a FIFO data structure: F irst I n, F irst O ut end front
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 The QueueNode Class template <typename T> class QueueNode { public: QueueNode(T value); template <typename T> friend class Queue; private: T data; QueueNode<T>* prev; }; template <typename T> QueueNode<T>::QueueNode(T value) { data = value; prev = NULL; } data prev The Queue Class template <typename T> class Queue { public: Queue(); ~Queue(); bool isEmpty() const; void enter (T value); T leave(); T peek() const; //Look but don't touch. private: QueueNode<T>* front; QueueNode<T>* end; }; Han prev Yoda Luke prev prev NULL end front
Background image of page 2
3 Queue Constructor-Destructor template <typename T> Queue<T>::Queue() { front = NULL; end = NULL; } template <typename T> bool Queue<T>::isEmpty() const { return (front==NULL); } template <typename T> Queue<T>::~Queue() { while (!isEmpty()) T temp = leave(); } template <typename T> T Queue<T>::peek() const { return front->data; } Leaving A Queue template <typename T> T Queue<T>::leave() {
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-051-20 taught by Professor Wittman during the Winter '10 term at UCLA.

Page1 / 8

Queues - Lecture 14: Queues PIC 10B Todd Wittman The Queue...

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

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