Lecture12 - CMPSC 24 Lecture 12 Abstract Data Type Queues...

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern