SlideShare a Scribd company logo
1
Queues
2
Queues
“A Queue is a special kind of list, where
items are inserted at one end (the rear)
And deleted at the other end (the front)”
Other Name:
 First In First Out (FIFO)
Difference from Stack:
Insertion go at the end of the list, rather
than the beginning of the list.
3
Common Operations on Queues
(Queue ADT)
1. MAKENULL(Q): Makes Queue Q be an empty list.
2. FRONT(Q): Returns the first element on Queue Q.
3. ENQUEUE(x,Q): Inserts element x at the end of
Queue Q.
4. DEQUEUE(Q): Deletes the first element of Q.
5. EMPTY(Q): Returns true if and only if Q is an empty
queue.
Example:
Line of customers in a bank
4
Applications of Queues
Operating system
multi-user/multitasking environments, where
several users or task may be requesting the
same resource simultaneously.
Communication Software
queues to hold information received over
networks and dial up connections. (Information
can be transmitted faster than it can be
processed, so is placed in a queue waiting to
be processed)
Some other?
5
Implementation
Static
Queue is implemented by an array, and size of
queue remains fix
Dynamic
A queue can be implemented as a linked list,
and expand or shrink with each enqueue or
dequeue operation.
6
7
A pointer Implementation of Queues
Keep two pointers:
 FRONT: A pointer to the first element of the
queue.
 REAR: A pointer to the last element of the
queue.
x y .zFront
Rear
8
A pointer Implementation of Queues
Q.front
Q.Rear
MAKENULL(Q)
Q.front
Q.Rear
ENQUEUE(x,Q)
.x
NULL
9
Q.front
Q.Rear
ENQUEUE(y,Q)
x .y
Q.front
Q.Rear
DEQUEUE(Q)
.y
A pointer Implementation of Queues
10
A class for Dynamic Queue implementation
class DynIntQueue
{
private:
struct QueueNode
{
int value;
QueueNode *next;
};
QueueNode *front;
QueueNode *rear;
int numItems;
public:
DynIntQueue(void);
~DynIntQueue(void);
void enqueue(int);
int dequeue(void);
bool isEmpty(void);
void makeNull(void);
};
11
Implemenaton
//************************
// Constructor *
//************************
DynIntQueue::DynIntQueue(void)
{
front = NULL;
rear = NULL;
numItems = 0;
}
//************************
// Destructor *
//************************
DynIntQueue::~DynIntQueue(void)
{
makeNull();
}
12
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void DynIntQueue::enqueue(int num)
{
QueueNode *newNode;
newNode = new QueueNode;
newNode->value = num;
newNode->next = NULL;
if (isEmpty())
{
front = newNode;
rear = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
numItems++;
}
13
//**********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies it into num. *
//**********************************************
int DynIntQueue::dequeue(void)
{
QueueNode *temp;
int num;
if (isEmpty())
cout << "The queue is empty.n";
else
{
num = front->value;
temp = front->next;
delete front;
front = temp;
numItems--;
}
return num;
}
14
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool DynIntQueue::isEmpty(void)
{
if (numItems)
return false;
else
return true;
}
15
//********************************************
// Function makeNull dequeues all the elements *
// in the queue. *
//********************************************
void DynIntQueue::makeNull(void)
{
while(!isEmpty())
dequeue();
}
16
Program
// This program demonstrates the DynIntQeue class
void main(void)
{
DynIntQueue iQueue;
cout << "Enqueuing 5 items...n";
// Enqueue 5 items.
for (int x = 0; x < 5; x++)
iQueue.enqueue(x);
// Deqeue and retrieve all items in the queue
cout << "The values in the queue were:n";
while (!iQueue.isEmpty())
{
int value;
value =iQueue.dequeue();
cout << value << endl;
}
}
17
Program Ouput
Enqueuing 5 items...
The values in the queue were:
0
1
2
3
4
18
Array Implementation
First Element
Last Element
maxlength
Front
Second
Element.
.
Rear
When queue is empty both front and rear are set to -1
While enqueueing increment rear by 1, and while dequeueing
increment front by 1
When there is only one value in the Queue, both rear and front
have same index
Can we implement Queue by using only one
index variable Front or Rear??
YES, by moving elements of array to neighboring
locations like we did in STACK but this is in-
efficient
Why it is inefficient?
19
Array Implementation
5 4 6 7 8 7 6
0 1 2 3 4 5 6 7 8
Front=0
Rear=6
8 7 6
0 1 2 3 4 5 6 7 8
Front=4
Rear=6
7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5
Rear=8
How can we insert more elements? Rear index can
not move beyond the last element….
20
Solution: Using circular queue
Allow rear to wrap around the array.
if(rear == queueSize-1)
rear = 0;
else
rear++;
Or use module arithmetic
rear = (rear + 1) % queueSize;
21
7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5
Rear=8
Enqueue 39 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0
39 7 6 12 67
0 1 2 3 4 5 6 7 8
Front=5
Rear=0
22
How to determine empty and full
Queues?
It is some tricky
Number of approaches
A counter indicating number of values in the
queue can be used (We will use this approach)
We will see another approach as well at the
end
23
Implementation
class IntQueue
{
private:
int *queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
IntQueue(int);
~IntQueue(void);
void enqueue(int);
int dequeue(void);
bool isEmpty(void);
bool isFull(void);
void clear(void);
};
Note, the member function clear, which clears the queue by resetting the
front and rear indices, and setting the numItems to 0.
24
IntQueue::IntQueue(int s) //constructor
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
IntQueue::~IntQueue(void) //destructor
{
delete [] queueArray;
}
25
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void IntQueue::enqueue(int num)
{
if (isFull())
cout << "The queue is full.n";
else
{
// Calculate the new rear position
rear = (rear + 1) % queueSize;
// Insert new item
queueArray[rear] = num;
// Update item count
numItems++;
}
}
26
//*********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies t into num. *
//*********************************************
int IntQueue::dequeue(void)
{
if (isEmpty())
cout << "The queue is empty.n";
else
{
// Move front
front = (front + 1) % queueSize;
// Retrieve the front item
int num = queueArray[front];
// Update item count
numItems--;
}
return num;
}
27
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool IntQueue::isEmpty(void)
{
if (numItems)
return false;
else
return true;
}
28
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull(void)
{
if (numItems < queueSize)
return false;
else
return true;
}
29
//*******************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//*******************************************
void IntQueue::clear(void)
{
front = - 1;
rear = - 1;
numItems = 0;
}
30
//Program demonstrating the IntQueue class
void main(void)
{
IntQueue iQueue(5);
cout << "Enqueuing 5 items...n";
// Enqueue 5 items.
for (int x = 0; x < 5; x++)
iQueue.enqueue(x);
// Attempt to enqueue a 6th item.
cout << "Now attempting to enqueue again...n";
iQueue.enqueue(5);
// Deqeue and retrieve all items in the queue
cout << "The values in the queue were:n";
while (!iQueue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << endl;
}
}
31
Program Output
Enqueuing 5 items...
Now attempting to enqueue again...
The queue is full.
The values in the queue were:
0
1
2
3
4
32
Another implementation of Queues using
Arrays
class CQueue
{
int Data*,QueueSize,Front,Rear;
public:
CQueue(int size);
~CQueue(int size);
bool IsFull();
bool IsEmpty();
void Enqueue(int num);
int Dequeue();
void MakeNull;
};
33
CQueue::CQueue(int size)
{
Front=Rear=-1;
Data=new int[size];
}
void CQueue ::Enqueue(int num);
{
if (IsFull()) { cout<<“Overflow” return; }
if (IsEmpty() Rear=Front=0;
else Rear=(Rear+1) % QueueSize;
Data[Rear]=num;
}
34
int CQueue ::Dequeue(int num);
{
if (IsEmpty()) { cout<<“Underflow”; return; }
int ReturnValue=Data[Front];
if (Front==Rear) //only one element in the queue
Front=Rear=-1;
else
Front=(Front+1) % QueueSize;
return ReturnValue;
}
35
bool CQueue::IsEmpty()
{
if (Front==-1) return true;
else return false;
}
bool CQueue::IsFull()
{
If (((Rear+1)%QueueSize)==Front)
return true;
else return false;
}

More Related Content

What's hot

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Naveen Gupta
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
Ramish Suleman
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
Intoduction to dynamic memory allocation
Intoduction to dynamic memory allocationIntoduction to dynamic memory allocation
Intoduction to dynamic memory allocation
Utsav276
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queueAmit Vats
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation웅식 전
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Katang Isip
 
Queue
QueueQueue
Queue implementation
Queue implementationQueue implementation
Queue implementation
Rajendran
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
Roman Rodomansky
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
OpenXR 1.0 Reference Guide
OpenXR 1.0 Reference GuideOpenXR 1.0 Reference Guide
OpenXR 1.0 Reference Guide
The Khronos Group Inc.
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
Mekk Mhmd
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
Toseef Hasan
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
Vineeta Garg
 

What's hot (20)

Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
Csharp_Chap13
Csharp_Chap13Csharp_Chap13
Csharp_Chap13
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
week-16x
week-16xweek-16x
week-16x
 
Intoduction to dynamic memory allocation
Intoduction to dynamic memory allocationIntoduction to dynamic memory allocation
Intoduction to dynamic memory allocation
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Queue oop
Queue   oopQueue   oop
Queue oop
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Queue
QueueQueue
Queue
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
OpenXR 1.0 Reference Guide
OpenXR 1.0 Reference GuideOpenXR 1.0 Reference Guide
OpenXR 1.0 Reference Guide
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 

Similar to Algo>Queues

Qprgs
QprgsQprgs
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
AliRaza899305
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
singhprpg
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
RashidFaridChishti
 
2 b queues
2 b queues2 b queues
2 b queues
Nguync91368
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
shericehewat
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
michardsonkhaicarr37
 
Fundamentals of Data Structure and Queues
Fundamentals of Data Structure and QueuesFundamentals of Data Structure and Queues
Fundamentals of Data Structure and Queues
Bogiri Nagaraju
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
Sushil Mishra
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
Russell Childs
 
Stack queue
Stack queueStack queue
Stack queue
James Wong
 
Stack queue
Stack queueStack queue
Stack queue
Fraboni Ec
 
Stack queue
Stack queueStack queue
Stack queue
Hoang Nguyen
 

Similar to Algo>Queues (20)

Qprgs
QprgsQprgs
Qprgs
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Data Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptxData Structures and Agorithm: DS 09 Queue.pptx
Data Structures and Agorithm: DS 09 Queue.pptx
 
2 b queues
2 b queues2 b queues
2 b queues
 
Given the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docxGiven the following ADT definition of a stack to use stack .docx
Given the following ADT definition of a stack to use stack .docx
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Fundamentals of Data Structure and Queues
Fundamentals of Data Structure and QueuesFundamentals of Data Structure and Queues
Fundamentals of Data Structure and Queues
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 

More from Ain-ul-Moiz Khawaja

Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
Ain-ul-Moiz Khawaja
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
Ain-ul-Moiz Khawaja
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 

More from Ain-ul-Moiz Khawaja (17)

Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & Algorithums
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & Algorithums
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
 
Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
 
Turn over
Turn overTurn over
Turn over
 
Attribution Theories
Attribution TheoriesAttribution Theories
Attribution Theories
 
Attribution Theory
Attribution TheoryAttribution Theory
Attribution Theory
 
Absenteeism
AbsenteeismAbsenteeism
Absenteeism
 
HRM Employee Turnover
HRM Employee TurnoverHRM Employee Turnover
HRM Employee Turnover
 

Recently uploaded

2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 

Recently uploaded (20)

2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 

Algo>Queues

  • 2. 2 Queues “A Queue is a special kind of list, where items are inserted at one end (the rear) And deleted at the other end (the front)” Other Name:  First In First Out (FIFO) Difference from Stack: Insertion go at the end of the list, rather than the beginning of the list.
  • 3. 3 Common Operations on Queues (Queue ADT) 1. MAKENULL(Q): Makes Queue Q be an empty list. 2. FRONT(Q): Returns the first element on Queue Q. 3. ENQUEUE(x,Q): Inserts element x at the end of Queue Q. 4. DEQUEUE(Q): Deletes the first element of Q. 5. EMPTY(Q): Returns true if and only if Q is an empty queue. Example: Line of customers in a bank
  • 4. 4 Applications of Queues Operating system multi-user/multitasking environments, where several users or task may be requesting the same resource simultaneously. Communication Software queues to hold information received over networks and dial up connections. (Information can be transmitted faster than it can be processed, so is placed in a queue waiting to be processed) Some other?
  • 5. 5 Implementation Static Queue is implemented by an array, and size of queue remains fix Dynamic A queue can be implemented as a linked list, and expand or shrink with each enqueue or dequeue operation.
  • 6. 6
  • 7. 7 A pointer Implementation of Queues Keep two pointers:  FRONT: A pointer to the first element of the queue.  REAR: A pointer to the last element of the queue. x y .zFront Rear
  • 8. 8 A pointer Implementation of Queues Q.front Q.Rear MAKENULL(Q) Q.front Q.Rear ENQUEUE(x,Q) .x NULL
  • 10. 10 A class for Dynamic Queue implementation class DynIntQueue { private: struct QueueNode { int value; QueueNode *next; }; QueueNode *front; QueueNode *rear; int numItems; public: DynIntQueue(void); ~DynIntQueue(void); void enqueue(int); int dequeue(void); bool isEmpty(void); void makeNull(void); };
  • 11. 11 Implemenaton //************************ // Constructor * //************************ DynIntQueue::DynIntQueue(void) { front = NULL; rear = NULL; numItems = 0; } //************************ // Destructor * //************************ DynIntQueue::~DynIntQueue(void) { makeNull(); }
  • 12. 12 //******************************************** // Function enqueue inserts the value in num * // at the rear of the queue. * //******************************************** void DynIntQueue::enqueue(int num) { QueueNode *newNode; newNode = new QueueNode; newNode->value = num; newNode->next = NULL; if (isEmpty()) { front = newNode; rear = newNode; } else { rear->next = newNode; rear = newNode; } numItems++; }
  • 13. 13 //********************************************** // Function dequeue removes the value at the * // front of the queue, and copies it into num. * //********************************************** int DynIntQueue::dequeue(void) { QueueNode *temp; int num; if (isEmpty()) cout << "The queue is empty.n"; else { num = front->value; temp = front->next; delete front; front = temp; numItems--; } return num; }
  • 14. 14 //********************************************* // Function isEmpty returns true if the queue * // is empty, and false otherwise. * //********************************************* bool DynIntQueue::isEmpty(void) { if (numItems) return false; else return true; }
  • 15. 15 //******************************************** // Function makeNull dequeues all the elements * // in the queue. * //******************************************** void DynIntQueue::makeNull(void) { while(!isEmpty()) dequeue(); }
  • 16. 16 Program // This program demonstrates the DynIntQeue class void main(void) { DynIntQueue iQueue; cout << "Enqueuing 5 items...n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Deqeue and retrieve all items in the queue cout << "The values in the queue were:n"; while (!iQueue.isEmpty()) { int value; value =iQueue.dequeue(); cout << value << endl; } }
  • 17. 17 Program Ouput Enqueuing 5 items... The values in the queue were: 0 1 2 3 4
  • 18. 18 Array Implementation First Element Last Element maxlength Front Second Element. . Rear When queue is empty both front and rear are set to -1 While enqueueing increment rear by 1, and while dequeueing increment front by 1 When there is only one value in the Queue, both rear and front have same index Can we implement Queue by using only one index variable Front or Rear?? YES, by moving elements of array to neighboring locations like we did in STACK but this is in- efficient Why it is inefficient?
  • 19. 19 Array Implementation 5 4 6 7 8 7 6 0 1 2 3 4 5 6 7 8 Front=0 Rear=6 8 7 6 0 1 2 3 4 5 6 7 8 Front=4 Rear=6 7 6 12 67 0 1 2 3 4 5 6 7 8 Front=5 Rear=8 How can we insert more elements? Rear index can not move beyond the last element….
  • 20. 20 Solution: Using circular queue Allow rear to wrap around the array. if(rear == queueSize-1) rear = 0; else rear++; Or use module arithmetic rear = (rear + 1) % queueSize;
  • 21. 21 7 6 12 67 0 1 2 3 4 5 6 7 8 Front=5 Rear=8 Enqueue 39 Rear=(Rear+1) mod Queue Size = (8+1) mod 9 = 0 39 7 6 12 67 0 1 2 3 4 5 6 7 8 Front=5 Rear=0
  • 22. 22 How to determine empty and full Queues? It is some tricky Number of approaches A counter indicating number of values in the queue can be used (We will use this approach) We will see another approach as well at the end
  • 23. 23 Implementation class IntQueue { private: int *queueArray; int queueSize; int front; int rear; int numItems; public: IntQueue(int); ~IntQueue(void); void enqueue(int); int dequeue(void); bool isEmpty(void); bool isFull(void); void clear(void); }; Note, the member function clear, which clears the queue by resetting the front and rear indices, and setting the numItems to 0.
  • 24. 24 IntQueue::IntQueue(int s) //constructor { queueArray = new int[s]; queueSize = s; front = -1; rear = -1; numItems = 0; } IntQueue::~IntQueue(void) //destructor { delete [] queueArray; }
  • 25. 25 //******************************************** // Function enqueue inserts the value in num * // at the rear of the queue. * //******************************************** void IntQueue::enqueue(int num) { if (isFull()) cout << "The queue is full.n"; else { // Calculate the new rear position rear = (rear + 1) % queueSize; // Insert new item queueArray[rear] = num; // Update item count numItems++; } }
  • 26. 26 //********************************************* // Function dequeue removes the value at the * // front of the queue, and copies t into num. * //********************************************* int IntQueue::dequeue(void) { if (isEmpty()) cout << "The queue is empty.n"; else { // Move front front = (front + 1) % queueSize; // Retrieve the front item int num = queueArray[front]; // Update item count numItems--; } return num; }
  • 27. 27 //********************************************* // Function isEmpty returns true if the queue * // is empty, and false otherwise. * //********************************************* bool IntQueue::isEmpty(void) { if (numItems) return false; else return true; }
  • 28. 28 //******************************************** // Function isFull returns true if the queue * // is full, and false otherwise. * //******************************************** bool IntQueue::isFull(void) { if (numItems < queueSize) return false; else return true; }
  • 29. 29 //******************************************* // Function clear resets the front and rear * // indices, and sets numItems to 0. * //******************************************* void IntQueue::clear(void) { front = - 1; rear = - 1; numItems = 0; }
  • 30. 30 //Program demonstrating the IntQueue class void main(void) { IntQueue iQueue(5); cout << "Enqueuing 5 items...n"; // Enqueue 5 items. for (int x = 0; x < 5; x++) iQueue.enqueue(x); // Attempt to enqueue a 6th item. cout << "Now attempting to enqueue again...n"; iQueue.enqueue(5); // Deqeue and retrieve all items in the queue cout << "The values in the queue were:n"; while (!iQueue.isEmpty()) { int value; iQueue.dequeue(value); cout << value << endl; } }
  • 31. 31 Program Output Enqueuing 5 items... Now attempting to enqueue again... The queue is full. The values in the queue were: 0 1 2 3 4
  • 32. 32 Another implementation of Queues using Arrays class CQueue { int Data*,QueueSize,Front,Rear; public: CQueue(int size); ~CQueue(int size); bool IsFull(); bool IsEmpty(); void Enqueue(int num); int Dequeue(); void MakeNull; };
  • 33. 33 CQueue::CQueue(int size) { Front=Rear=-1; Data=new int[size]; } void CQueue ::Enqueue(int num); { if (IsFull()) { cout<<“Overflow” return; } if (IsEmpty() Rear=Front=0; else Rear=(Rear+1) % QueueSize; Data[Rear]=num; }
  • 34. 34 int CQueue ::Dequeue(int num); { if (IsEmpty()) { cout<<“Underflow”; return; } int ReturnValue=Data[Front]; if (Front==Rear) //only one element in the queue Front=Rear=-1; else Front=(Front+1) % QueueSize; return ReturnValue; }
  • 35. 35 bool CQueue::IsEmpty() { if (Front==-1) return true; else return false; } bool CQueue::IsFull() { If (((Rear+1)%QueueSize)==Front) return true; else return false; }