Lecture12

Lecture12 - 5/10/10 CMPSC 24: Lecture 12 Abstract Data...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5/10/10 CMPSC 24: Lecture 12 Abstract Data Type: Queues Divyakant Agrawal Department of Computer Science UC Santa Barbara 5/10/10 1 Lecture Plan •  Another common Abstract Data Type: –  Queues •  Queues SpecificaJon •  Queue ImplementaJon: –  Array ImplementaJon –  Linked List ImplementaJon 5/10/10 2 Queues 3 1 5/10/10 Queues 4 Queues • What do these composite objects all have in common? 5 Queues • An abstract data type in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO) structure. • ApplicaJons • Checking for a palindrome • Assigning priority 6 2 5/10/10 Queues • What opera5ons would be appropriate for a queue? 7 Queues •  Transformers –  MakeEmpty –  Enqueue –  Dequeue change state observe state •  Observers –  IsEmpty –  IsFull •  8 Queue ADT OperaJons •  MakeEmpty ‐‐ Sets queue to an empty state. •  IsEmpty ‐‐ Determines whether the queue is currently empty. •  IsFull ‐‐ Determines whether the queue is currently full. •  Enqueue (ItemType newItem) ‐‐ Adds newItem to the rear of the queue. •  Dequeue (ItemType& item) ‐‐ Removes the item at the front of the queue and returns it in item. 9 3 5/10/10 Queues class QueType { public: QueType(int max); QueType(); ~QueType(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); void Dequeue(ItemType& item); Logical Level 10 Array‐Based ImplementaJon One data structure: An array with the front of the queue fixed in the first posiJon Enqueue A, B, C, D Dequeue Move elements down What’s wrong with this design? 11 Array‐Based ImplementaJon Another data structure: An array where the front floats What happens if we add X, Y, and Z ? 12 4 5/10/10 Array‐Based ImplementaJon We can let the queue wrap around in the array; i.e. treat the array as a circular structure 13 Array‐Based ImplementaJon front = 2 rear = 2 A 0 1 2 3 4 Queue.Dequeue(item) C D A 0 1 2 3 4 Empty Queue B front = 3 rear = 1 Full Queue Queue.Enqueue(‘E’) How can we tell the difference? 14 Array‐Based ImplementaJon A third data structure: front indicates the slot preceding the first item; it is reserved and not used Empty Queue Full Queue 15 5 5/10/10 Array‐Based ImplementaJon private: int front; int rear; int maxQue; Complete implementa5on level ItemType* items; } To what do we ini5alize front and rear ? 16 Array‐Based ImplementaJon QueType::QueType(int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; } Why is the array declared max + 1 ? bool QueType::IsEmpty( ) { return ( rear == front );} bool QueType::IsFull( ) { return ( (rear + 1) % maxQue == front )} 17 Array‐Based ImplementaJon void QueType::Enqueue(ItemType Item) { if (IsFull()) throw FullQueue(); else { rear = (rear + 1) % maxQue; items[rear] = newItem; } } 18 6 5/10/10 Array‐Based ImplementaJon void QueType::Dequeue(ItemType& item) { if (IsEmpty()) throw EmptyQueue(); else { front = (front + 1) % maxQue; item = items[front]; } } 19 Linked ImplementaJon Data structure for linked queue 20 Linked ImplementaJon A circular linked queue uses only one external pointer: rear How do you access front? 21 7 ...
View Full Document

This note was uploaded on 12/27/2011 for the course CMPSC 24 taught by Professor Agrawal during the Fall '09 term at UCSB.

Ask a homework question - tutors are online