queue adt operations
TRANSCRIPT
07/11/2021
Data Structures and Algorithms- Fall 2021Instructor: Saima Jawad 1
W04-Queues 20
Queue ADT Operations
Initialize -- Sets queue to an empty state.
IsEmpty -- Determines whether the queue is currently empty.
IsFull -- Determines whether the queue is currently full.
Insert (ItemType newItem) -- Adds newItem to the rear of the queue.
Remove (ItemType& item) -- Removes the item at the front of the queue and returns it in item.
20
W04-Queues 21
Queue ADT Operations
Transformers◦ Initialize
◦ Insert
◦ Remove
Observers ◦ IsEmpty
◦ IsFull
change state
observe state
21
07/11/2021
Data Structures and Algorithms- Fall 2021Instructor: Saima Jawad 2
W04-Queues 22
Queue Implementation in C++
Que
~Que
Insert
Remove.
.
.
class Que
Private Data:
front 2
rear 4
maxQue 5
items‘C’ ‘X’ ‘J’
items [0] [1] [2] [3] [4]
QUEUECLASS TEMPLATE DEFINITION
template <class ItemType>
class Que {
public:
Que( );
Que(int max); // PARAMETERIZED CONSTRUCTOR
~Que( ) ; // DESTRUCTOR
bool IsFull( ) const;
bool IsEmpty( ) const;
void Insert(ItemType newItem);
void Remove(ItemType& oldItem);
private:int front;
int rear;
int maxQue;
int count;
ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION
};
07/11/2021
Data Structures and Algorithms- Fall 2021Instructor: Saima Jawad 3
template <class ItemType>Que<ItemType>::Que() // Default Constructor{
maxQue = 501;front = 0;rear = 0;count = 0;items = new ItemType[maxQue]; // Dynamic Array
}
template <class ItemType>Que<ItemType>::Que(int max) // Parameterized Constructor{
maxQue = max + 1;front = 0; rear = 0;
count = 0;items = new ItemType[maxQue];
}
QUEUE CLASS TEMPLATE IMPLEMENTATION
template <class ItemType>
Que<ItemType>::~Que( )
{
delete [ ] items; // deallocates array
}
template <class ItemType>
bool Que<ItemType>::IsEmpty( ) const{
return (count == 0);}
template <class ItemType>
bool Que<ItemType>::IsFull( ) const{
return (count == maxQue);}
QUEUE CLASS TEMPLATE IMPLEMENTATION
07/11/2021
Data Structures and Algorithms- Fall 2021Instructor: Saima Jawad 4
QUEUE CLASS TEMPLATE IMPLEMENTATION
template <class ItemType>
void Que<ItemType>::Insert(ItemType newItem)
{
if (IsFull()) cout << "OverFlow";
else
{ items[rear] = newItem;
rear = (rear + 1) % maxQue;
++count;
}
}
template <class ItemType>
void Que<ItemType>::Remove(ItemType& oldItem)
{
if (IsEmpty()) cout << "UnderFlow";
else
{ oldItem = items[front];
front = (front + 1) % maxQue;
--count;
}
}
W04-Queues 27
Double ended Queue (Deque)
A Deque is a queue in which elements can be added orremoved at either end but not in the middle.
07/11/2021
Data Structures and Algorithms- Fall 2021Instructor: Saima Jawad 5
W04-Queues 28
Stack vs. Queue vs. Deque
W04-Queues 29
Applications of Deque
Undo-Redo operations in Software applications.
07/11/2021
Data Structures and Algorithms- Fall 2021Instructor: Saima Jawad 6
W04-Queues 30
Double ended Queue Variations
There are two variations of a Deque:◦ Input-restricted Deque
allows insertions from one end only.
◦Output-restricted Deque
allows deletions from one end only.
W04-Queues 31
Deque RepresentationA Deque is maintained by a circular array with pointers Left/Front and Right/Rear, which point to the two ends of the Deque.
We assume that the elements extend from Left/Front to Right/Rear.
07/11/2021
Data Structures and Algorithms- Fall 2021Instructor: Saima Jawad 7
W04-Queues 32
There are four basic operations of a Deque:oInsertion at Rear/Right end
oInsertion at Front/Left end
oDeletion at Front/Left end
oDeletion at Rear/Right end
Deque Implementation