Download presentation
Presentation is loading. Please wait.
1
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva - SUNY Fredonia
2
2 What is a Queue? l Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). l A queue is a FIFO “first in, first out” structure.
3
Queue ADT Operations l MakeEmpty -- Sets queue to an empty state. l IsEmpty -- Determines whether the queue is currently empty. l IsFull -- Determines whether the queue is currently full. l Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. l Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 3
4
ADT Queue Operations Transformers n MakeEmpty n Enqueue n Dequeue Observers n IsEmpty n IsFull change state observe state 4
5
5 DYNAMIC ARRAY IMPLEMENTATION QueType ~QueType Enqueue Dequeue. class QueType Private Data: front 1 rear 4 maxQue 5 items ‘C’ ‘X’ ‘J’ items [0] [1] [2] [3] [4] RESERVED
6
//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); QueType( int max );// PARAMETERIZED CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR... bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); private: int front; int rear; int maxQue; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION }; 6
7
7 Implementation of Queues There are two basic approaches: A linear list and a variation on a circular list
8
8 LISTS The only method that would be affected is the dequeue method A BCDEF After a dequeue method is applied B CDEF
9
9 Using Linear List Using a linear list the deque method is template void QueType::Dequeue(ItemType& item) { item = item[0]; QueCount--; for (int k=0; k < QueCount; k++) items[k] = items[k+1]; } This is what complexity?
10
10 Circular List The variation on a circular list uses two variables - front and rear front points to the front of the queue rear points to the back of the queue
11
11 Circular Lists Enqueue('A'); front = 0,rear = 0 A.Enqueue('B'); front = 0,rear = 1 AB.Enqueue('C'); front = 0,rear = 2 ABC.Enqueue('D'); front = 0,rear = 3 ABCD.Dequeue(item); front = 1,rear = 3 BCD What is the complexity of this approach?
12
12 Circular Lists We want queue elements to wrap around We will use the modulo operation to do this –rear = (rear + 1) % maxQue; –front = (front + 1) % maxQue;
13
13 Circular Lists Queue Constructors template QueType ::QueType() // Default { maxQue = 501; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; // dynamically allocates }
14
14 Circular Lists Queue Constructors template QueType ::QueType( int max ) // PARAMETERIZED { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; // dynamically allocates }
15
15 Circular Lists Destructor template QueType ::~QueType( ) { delete [ ] items; // deallocates array }
16
16 Circular Lists template bool QueType ::IsEmpty( ) { return (rear == front) }
17
17 Circular Lists template bool QueType ::IsFull( ) {// WRAP AROUND return ( (rear + 1) % maxQue == front ) }
18
18 Circular Lists Enqueue and Dequeue operations template void QueType ::Enqueue( ItemType item ) { rear = (rear + 1) % maxQue; items[rear] = newItem; }
19
19 Circular Lists Enqueue and Dequeue operations template void QueType ::Dequeue( ItemType& item ) { front = (front + 1) % maxQue; item = items[front]; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.