SlideShare a Scribd company logo
Queues
By Mansoor Ahmad
What is a queue?
• It is an ordered group of homogeneous items of
elements.
• Queues have two ends:
– Elements are added at one end.
– Elements are removed from the other end.
• The element added first is also removed first
(FIFO: First In, First Out).
Queue Specification
• Definitions: (provided by the user)
– MAX_ITEMS: Max number of items that might be on
the queue
– ItemType: Data type of the items on the queue
• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Enqueue (ItemType newItem)
– Dequeue (ItemType& item)
Implementing Queue
Using linked List:
front
2
5
7
1 1 7 5 2
front
rear rear
Implementing Queue
Using linked List:
front
2
5
7
1 1 7 5 2
front
rear rear
front
2
5
7 1 7 5 2
front
rear rear
dequeue()
Implementing Queue
Using linked List:
front
2
5
7
1 1 7 5 2
front
rear rear
front
2
5
7 9
7 5 2
front
rear rear
enqueue(9)
9
Implementing Queue
int dequeue()
{
int x = front->get();
Node* p = front;
front = front->getNext();
delete p;
return x;
}
void enqueue(int x)
{
Node* newNode = new Node();
newNode->set(x);
newNode->setNext(NULL);
rear->setNext(newNode);
rear = newNode;
}
Implementing Queue
int front()
{
return front->get();
}
int isEmpty()
{
return ( front == NULL );
}
Queue using Linked List
Queue using Linked List
Queue using Linked List
Queue using Linked List Ex 2
Queue using Linked List Ex 2
Queue using Array
If we use an array to hold queue elements,
both insertions and removal at the front
(start) of the array are expensive.
This is because we may have to shift up to
“n” elements.
For the stack, we needed only one end; for
queue we need both.
To get around this, we will not shift upon
removal of an element.
Queue using Array
front
2
5
7
1
rear
6
5 7
0
0 1 3
2 4
front
1 7 5 2
3
rear
Queue using Array
front
2
5
7
1
rear
6
5 7
0
0 1 3
2 4
front
1 7 5 2
4
rear
enqueue(6)
6
6
Queue using Array
front
2
5
7
1
rear
6
5 7
0
0 1 3
2 4
front
1 7 5 2
5
rear
enqueue(8)
6
6
8
8
Queue using Array
front
2
5
7
rear
6
5 7
1
0 1 3
2 4
front
7 5 2
5
rear
dequeue()
6
6
8
8
Queue using Array
front
2
5
rear
6
5 7
2
0 1 3
2 4
front
5 2
5
rear
dequeue()
6
6
8
8
Queue using Array
front
2
5
rear
6
5 7
2
0 1 3
2 4
front
5 2
7
rear
enqueue(9)
enqueue(12)
6
6
8
8
9
9
12
12
enqueue(21) ??
Queue using Array
We have inserts and removal running in
constant time but we created a new
problem.
Cannot insert new elements even though
there are two places available at the start
of the array.
Solution: allow the queue to “wrap
around”.
Queue using Array
Basic idea is to picture the array as a
circular array.
front
2
5
rear
2
front
7
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
2
6
8
9
12
Queue using Array
void enqueue(int x)
{
rear = (rear+1)%size;
array[rear] = x;
noElements = noElements+1;
}
front
2
5
rear
2
front
0
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
2
6
8
9
12
enqueue(21)
21
21
8
size
7
noElements
Queue using Array
int isFull()
{
return noElements == size;
}
int isEmpty()
{
return noElements == 0;
}
front
2
5
rear
2
front
1
rear
6 8 9 12
6
5
7
0 1
3
2
4
5
2
6
8
9
12
enqueue(7)
21
21
8
size
8
noElements
7
7
Queue using Array
int dequeue()
{
int x = array[front];
front = (front+1)%size;
noElements = noElements-1;
return x;
}
front rear
4
front
1
rear
6 8 9 12
6
5
7
0 1
3
2
4
6
8
9
12
dequeue()
21
21
8
size
6
noElements
7
7
Code of Queue using Array
Code of Queue using Array
Code of Queue using Array Ex2
Code of Queue using Array Ex2
Implementation issues
• Implement the queue as a circular
structure.
• How do we know if a queue is full or
empty?
• Initialization of front and rear.
• Testing for a full or empty queue.
Queues in C++ detailed explanation and examples .ppt
Queues in C++ detailed explanation and examples .ppt
Make front point to the element preceding the front
element in the queue (one memory location will be
wasted).
Initialize front and rear
Queue is empty
now!!
rear == front
Queue Implementation
template<class ItemType>
class QueueType {
public:
QueueType(int);
QueueType();
~QueueType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);
private:
int front;
int rear;
ItemType* items;
int maxQue;
};
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::QueueType(int
max)
{
maxQue = max + 1;
front = maxQue - 1;
rear = maxQue - 1;
items = new ItemType[maxQue];
}
Queue Implementation (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
delete [] items;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::
MakeEmpty()
{
front = maxQue - 1;
rear = maxQue - 1;
}
Queue Implementation (cont.)
template<class ItemType>
bool QueueType<ItemType>::IsEmpty() const
{
return (rear == front);
}
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
return ( (rear + 1) % maxQue == front);
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Enqueue
(ItemType newItem)
{
rear = (rear + 1) % maxQue;
items[rear] = newItem;
}
Queue Implementation (cont.)
template<class ItemType>
void QueueType<ItemType>::Dequeue
(ItemType& item)
{
front = (front + 1) % maxQue;
item = items[front];
}
Queue overflow
• The condition resulting from trying to add
an element onto a full queue.
if(!q.IsFull())
q.Enqueue(item);
Queue underflow
• The condition resulting from trying to
remove an element from an empty queue.
if(!q.IsEmpty())
q.Dequeue(item);
Example: recognizing palindromes
• A palindrome is a string that reads the same
forward and backward.
Able was I ere I saw Elba
• We will read the line of text into both a stack
and a queue.
• Compare the contents of the stack and the
queue character-by-character to see if they
would produce the same string of characters.
Example: recognizing palindromes
Example: recognizing palindromes
#include <iostream.h>
#include <ctype.h>
#include "stack.h"
#include "queue.h“
int main()
{
StackType<char> s;
QueType<char> q;
char ch;
char sItem, qItem;
int mismatches = 0;
cout << "Enter string: " << endl;
while(cin.peek() != 'n') {
cin >> ch;
if(isalpha(ch)) {
if(!s.IsFull())
s.Push(toupper(ch));
if(!q.IsFull())
q.Enqueue(toupper(ch));
}
}
while( (!q.IsEmpty()) && (!s.IsEmpty()) ) {
s.Pop(sItem);
q.Dequeue(qItem);
if(sItem != qItem)
++mismatches;
}
if (mismatches == 0)
cout << "That is a palindrome" << endl;
else
cout << That is not a palindrome" << endl;
return 0;
}
Example: recognizing palindromes
Case Study: Simulation
• Queuing System: consists of servers and
queues of objects to be served.
• Simulation: a program that determines how
long items must wait in line before being
served.
Case Study: Simulation (cont.)
• Inputs to the simulation:
(1) the length of the simulation
(2) the average transaction time
(3) the number of servers
(4) the average time between job
arrivals
Case Study: Simulation (cont.)
• Parameters the simulation must vary:
(1) number of servers
(2) time between arrivals of items
• Output of simulation: average wait time.
Exercises (Chapt 4)
• 26, 29-34, 39-41, 46, 47.

More Related Content

PPTX
PPTX
Queues operation using data structure in c
PPT
Queues.ppt
PPT
Queues.ppt
PPT
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
PPT
Queues.ppt
PPT
Queues.ppt
PPT
Queues.pptiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Queues operation using data structure in c
Queues.ppt
Queues.ppt
QueuYUGHIJLK;KJHGFCFYUGIHOJUHYGHJIOIHGes.ppt
Queues.ppt
Queues.ppt
Queues.pptiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

Similar to Queues in C++ detailed explanation and examples .ppt (20)

PPT
Queues - Data structure and algorithm.ppt
PPT
Lecture 2d queues
PPTX
stacks and queues for public
PPT
StacksandQueues.pptnajaiananaajaoakanabjana
PPTX
The presention is about the queue data structure
PPT
Lec-07 Queues.ppt queues introduction to queue
PPT
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
PPTX
Queue Data Structure
PDF
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
PPT
basics of queues
PPTX
Unit 4 queue
PDF
LEC4-DS ALGO.pdf
PPTX
PDF
Queue ADT for data structure for computer
PPTX
RPT_02_B_Queue presentation for FE students
PPTX
U3.stack queue
PPT
PPT
Stacks, Queues, Deques
PPTX
Queue data structures and operation on data structures
Queues - Data structure and algorithm.ppt
Lecture 2d queues
stacks and queues for public
StacksandQueues.pptnajaiananaajaoakanabjana
The presention is about the queue data structure
Lec-07 Queues.ppt queues introduction to queue
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
Queue Data Structure
CEN 235 4. Abstract Data Types - Queue and Stack.pdf
basics of queues
Unit 4 queue
LEC4-DS ALGO.pdf
Queue ADT for data structure for computer
RPT_02_B_Queue presentation for FE students
U3.stack queue
Stacks, Queues, Deques
Queue data structures and operation on data structures
Ad

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
L1 - Introduction to python Backend.pptx
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
AI in Product Development-omnex systems
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
System and Network Administraation Chapter 3
PPT
Introduction Database Management System for Course Database
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Right IT Partner for Your Business in Malaysia
ISO 45001 Occupational Health and Safety Management System
L1 - Introduction to python Backend.pptx
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
AI in Product Development-omnex systems
Softaken Excel to vCard Converter Software.pdf
ManageIQ - Sprint 268 Review - Slide Deck
How to Migrate SBCGlobal Email to Yahoo Easily
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
System and Network Administraation Chapter 3
Introduction Database Management System for Course Database
Navsoft: AI-Powered Business Solutions & Custom Software Development
How Creative Agencies Leverage Project Management Software.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 41
2025 Textile ERP Trends: SAP, Odoo & Oracle
Ad

Queues in C++ detailed explanation and examples .ppt