SlideShare a Scribd company logo
QUEUES IN C++
INTRODUCTION TO QUEUE
DEFINITION:
 A stack is a data structure that provides temporary storage of data in such
a way that the element stored first will be retrieved first.
 This method is also called FIFO – First In First Out.
EXAMPLE:
In real life we can think of queue as a queue of vehicles waiting at the petrol
pump, people waiting at the bus stop for the bus etc. The first person to
enter the queue is the first one to leave the queue. Similarly last person to
join the queue is the last person to leave the queue.
OPERATIONS ON QUEUE
 A queue is a linear data structure.
 It is controlled by two operations: insertion and deletion.
 Insertion operation takes place from the rear end of the queue and
deletion operation takes place from the front end of the queue.
 Insertion operation adds an element to the queue.
 Deletion operation removes an element from the queue.
 There are two variables FRONT and REAR. FRONT points to the
beginning of the filled queue and takes care of deletion operation. REAR
points to the end of the queue and takes care of insertion operation.
 These two operations implement the FIFO method .
IMPLEMENTATION OF QUEUE
Queue can be implemented in two ways:
 As an Array
 As a Linked List
QUEUE AS AN ARRAY
Array implementation of Queue uses
 an array to store data
 an integer type variable usually called the FRONT,
which points to the beginning of the array and an
integer variable REAR, which points to the end of
the filled array.
30
20
10
2
4
3
2
1
0REAR
0
FRONT
INSERTION OPERATION
 Initially when the queue is empty, FRONT and REAR can have any
integer value other than any valid index number of the array. Let
FRONT = -1; REAR=-1;
 The first element in the empty queue goes to the 0th position of the
array and both FRONT and REAR are initialized to value 0.
 After this every insertion operation increase the REAR by 1 and
inserts new data at that particular position.
 As arrays are fixed in length, elements can not be inserted beyond
the maximum size of the array. Pushing data beyond the maximum
size of the queue (i.e. when REAR = MAX SIZE -1) results in “data
overflow”.
INSERTION OPERATION EXPLAINED
insert 10
front = 0
rear = 0
DELETE OPERATION
The delete operation deletes the very first item from the queue.
Any attempt to delete an element from the empty queue (when
FRONT+REAR=-1) results in “data underflow” condition.
Each time the deletion operation is performed, the FRONT
variable is decremented by 1.
When there is only one element in the queue, deletion operation
of that element makes the queue empty and both FRONT and
REAR will be assigned the value -1.
DELETION OPERATION EXPLAINED
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS AN INTEGER ARRAY
#include<iostream.h>
#include<conio.h>
#define size 4
class queue
{
int data[size];
int front,rear;
public:
queue()
{ front=-1; rear=-1; }
void insert();
void deletion();
};
void queue::insert()
{ if(rear==size-1)
{ cout<<"n queue is full";
return;
}
else if(rear == -1)
{ rear++;
front++; }
else
rear++;
cout<<"Enter Data : ";
cin>>data[rear]; }
void queue::deletion()
{
if(front==-1)
{ cout<<"n Queue is empty";
return;
}
cout<<data[front]<<"
deleted"<<endl;
if(front==rear)
{ front=-1;rear=-1; }
else
front++; }
void display()
{ for (int i=front;i<=rear; i++)
cout<< data[rear]; }
void main()
{
queue q;
int ch;
do
{
cout<<"n1. Insertn2. Deleten3.
Displayn4.QuitnEnterChoice(1-3) ";
cin>>ch;
switch(ch)
{
case 1: q.insert();break;
case 2: q.deletion();break;
case 3. q.display();
}
}while(ch!=3); }
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS AN ARRAY OF OBJECTS
#include<iostream.h>
#include<conio.h>
struct item
{ int ino;
char name[20]; };
class queue
{
item data[4];
int front,rear;
public:
queue()
{ front=-1; rear=-1; }
void insert();
void deletion();
};
void queue::insert()
{ if(rear==size-1)
{ cout<<"n queue is full";
return; }
else if(rear == -1)
{ rear++;
front++; }
else
rear++;
cout<<"Enter Data : ";
cin>>data[rear].ino>>
data[rear].name; }
void queue::deletion()
{
if(front==-1)
{ cout<<"n Queue is empty";
return; }
cout<<data[front].ino<<"
deleted"<<endl;
if(front==rear)
{ front=-1;rear=-1; }
else
front++; }
void display()
{ for (int i=front;i<=rear; i++)
cout<< data[rear]].ino<<
data[rear].name; }
void main()
{
queue q;
int ch;
do
{
cout<<"n1. Insertn2. Deleten3.
Displayn4.QuitnEnterChoice(1-3) ";
cin>>ch;
switch(ch)
{ case 1: q.insert();break;
case 2: q.deletion();break;
case 3. q.display();
}
}while(ch!=3); }
APPLICATIONS OF QUEUE
A queue is an appropriate data structure on which information is
stored and then retrieved in FIFO order.The applications of queue
are as follows:
Queues find their use in CPU scheduling, printer spooling,
message queuing, computer networks etc.
In time sharing system queues help in scheduling of jobs.
DISADVANTAGE OF NORMAL QUEUE
The queue as an array suffers from one major drawback.
As arrays are fixed in size, elements can not be inserted beyond
the maximum size of the array, even though in reality there
might be empty slots in the beginning of the queue.
REAR
30 40 50 60
0 1 2 3 4 5
2 5FRONT
In this example, queue is considered
as full although there are two empty
spaces in the beginning of the queue.
CIRCULAR QUEUE AS AN ARRAY
Array implementation of Circular Queue uses
 an array to store data
 an integer type variable usually called the FRONT, which
points to the beginning of the filled array and an integer
variable REAR, which points to the end of the filled
array.
NOTE: When the number of additions makes REAR equal to the size
of the array, the next element is inserted in the first slot provided it is
free. Circular queue is full when all the slots of the array are
occupied.
50
40
30
60
2
4
3
2
1
0
REAR
0
FRONT
OPERATIONS ON CIRCULAR QUEUE
 A queue is a linear data structure.
 It is controlled by two operations: insertion and deletion.
 Insertion operation takes place from the rear end of the queue and
deletion operation takes place from the front end of the queue.
 There are two variables FRONT and REAR. FRONT points to the
beginning of the queue and takes care of deletion operation. REAR
points to the end of the queue and takes care of insertion operation.
 If REAR reaches the end of the queue then the next insertion
operation makes REAR=0 provided FRONT is not 0.
 If the FRONT reaches the end of the queue then the next deletion
operation makes FRONT=0 provided there are more elements in the
queue.
CIRCULAR QUEUE: INSERTION OPERATION
 Initially when the queue is empty, FRONT and REAR can have any
integer value other than any valid index number of the array. Let
FRONT = -1; REAR=-1;
 The first element in the empty queue goes to the 0th position of
the array and both FRONT and REAR are initialized to value 0.
 After this every insertion operation increases the REAR by 1 and
inserts new data at that particular position.
 If REAR reaches the end of the queue then the next insertion
operation makes REAR=0 provided FRONT is not 0.
 The queue is full when REAR=FRONT +1 or (FRONT=0 and
REAR=MAX-1). Insertion at this point results in “data overflow”.
CIRCULAR QUEUE: INSERTION OPERATION EXPLAINED
front=2
CIRCULAR QUEUE:DELETE OPERATION
The delete operation deletes the very first item from the queue.
Any attempt to delete an element from the empty queue(when
FRONT+REAR=-1) results in “data underflow” condition.
Each time the deletion operation is performed, the FRONT
variable is decremented by 1.
When there is only one element in the queue, deletion operation
of that element makes the queue empty and both FRONT and
REAR will be assigned the value -1.
When FRONT reaches the end of the queue then the next
deletion operation makes FRONT = 0.
CIRCULAR QUEUE: DELETION OPERATION EXPLAINED
PROGRAMTO ILLUSTRATE OPERATIONS ON CIRCULAR QUEUE
#include<iostream.h>
#include<conio.h>
#define size 4
class cqueue
{
int data[size];
int front,rear;
public:
cqueue()
{ front=-1;rear=-1; }
void insert();
void remove(); };
void cqueue::insert(int num)
{ if(rear==size-1&&
front==0 || front==rear+1)
{ cout<<"nCircular
queue is full";
return;
}
else if(rear==-1)
{ rear++;
front++; }
else if(rear==size-1)
rear=0;
else
rear++;
cout<<"Enter Data : ";
data[rear]=num;
}
void cqueue::remove()
{
if(front==-1)
{
cout<<"n Circular
Queue is empty";return;
}
cout<<data[front]<<"
deleted"<<endl;
if(front==rear)
{ front=-1;rear=-1; }
else if(front==size-1)
front=0;
else
front++;
}
void cqueue::disp()
{if(front<rear)
for(int i= front;i<=rear;
i++)
cout<<data[i];
else
{for(int i=front;i<=size-1;
i++)
cout<<data[i];
for(int i= 0;i<=rear; i++)
cout<<data[i]; }}
void main()
{
cqueue cq;
int ch;
do
{
cout<<"n1. Insert n2.
Removen3. Displayn 4.
Quit n Enter Choice(1-4) ";
cin>>ch;
switch(ch)
{ case 1:
cq.insert();break;
case 2:
cq.remove();break;
case 3: cq.disp(); }
}while(ch!=4);}
QUEUE AS A LINKED LIST
Linked list implementation of queue uses:
 A linked list to store data
 A pointer FRONT pointing to the beginning of the queue and a pointer REAR
pointing to the end of the queue.
NOTE: Each node of a queue as a linked list has two parts : data part and link part and
is created with the help of self referential structure.
The data part stores the data and link part stores the address of the next node of the
linked list.
FRONT
NODE
DATA LINK
REAR
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS A LINKED LIST
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class queue
{
node *rear,*front;
public:
queue()
{ rear=NULL;front=NULL;}
void qinsert();
void qdelete();
void qdisplay();
};
void queue::qinsert()
{
node *temp;
temp=new node;
cout<<"Data :";
cin>>temp->data;
temp->next=NULL;
if(rear==NULL)
{
rear=temp;
front=temp;
}
else
{
rear->next=temp;
rear=temp;
}
}
void queue::qdelete()
{
if(front!=NULL)
{ node *temp=front;
cout<<front->data
<<"deleted n";
front=front->next;
delete temp;
if(front==NULL)
rear=NULL; }
else
cout<<“empty Queue “;
}
void queue::qdisplay()
{
node *temp=front;
while(temp!=NULL)
{ cout<<temp->data;
temp=temp->next; } }
void main()
{
queue q1; char ch;
do
{
cout<< "i. insertn
d. Deletens. Display
n q. quit ";
cin>>ch;
switch(ch)
{
case 'i' :
q1.qinsert();break;
case 'd' :
q1.qdelete();break;
case 's' :
q1.qdisplay();
}
}while(ch!='q'); }
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS A LINKED LIST : Explanation
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *next;
};
class queue
{
node *rear,*front; // front point to the beginning of the queue and rear points to the end of the queue
public:
queue()
{ rear=NULL;front=NULL;} // Initializes front and rear to NULL
void qinsert();
void qdelete();
void qdisplay();
};
Self Referential Structure:These are special structures which
contains pointers to themselves.
Here, next is a pointer of type node itself.
Self Referential structures are needed to create Linked Lists.
PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE
AS A LINKED LIST : Explanation
void queue::qinsert()
{
node *temp; // pointer of type node
temp=new node; // new operator will create a new
//node and address of new node
// is stored in temp
cout<<"Data :";
cin>>temp->data; // adding data in the node
temp->next=NULL; // because new node is always
// added at the end
if(rear==NULL) // if queue is empty, new node
{ rear=temp; // becomes the first node and
front=temp; // both front and rear will point to
} //it
else
{ rear->next=temp; // If queue is not empty ,last
rear=temp; // node will contain address
} // of new node and rear will
} // point to new node
void queue::qdelete()
{
if(front!=NULL) // if queue is not empty
{ node *temp=front; // temp is a pointer
//containing address of
// first node
cout<<front->data <<"deleted n";
front=front->next; // front will now contain
// address of second node
delete temp; // delete operator will delete the node
// stored at temp
if(front==NULL) /*if front become NULL after
deletion, it means that there was only one node
in the queue and deletion of that node will
queue empty */
rear=NULL; }
else
cout<<“empty Queue “;}

More Related Content

What's hot

Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Queues
QueuesQueues
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
Stacks
StacksStacks
Stacks
sweta dargad
 
Stack and queue
Stack and queueStack and queue
Stack and queue
CHANDAN KUMAR
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
FarihaHabib123
 
Stacks
StacksStacks
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
Muhazzab Chouhadry
 
Linked list
Linked listLinked list
Linked list
Md. Afif Al Mamun
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Queue
QueueQueue
Queue
Raj Sarode
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
junnubabu
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
Praveen Vishwakarma
 
Insertion sort
Insertion sortInsertion sort
Insertion sortMYER301
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Linked list
Linked listLinked list
Linked list
KalaivaniKS1
 
Stack
StackStack

What's hot (20)

Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Queues
QueuesQueues
Queues
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Stacks
StacksStacks
Stacks
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Stacks
StacksStacks
Stacks
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Linked list
Linked listLinked list
Linked list
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Queue
QueueQueue
Queue
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Data structure Stack
Data structure StackData structure Stack
Data structure Stack
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Linked list
Linked listLinked list
Linked list
 
Stack
StackStack
Stack
 

Viewers also liked

Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
Vineeta Garg
 
Queue
QueueQueue
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
Rojan Pariyar
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
Farhanum Aziera
 
Queues
QueuesQueues
Queues
Hareem Aslam
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
sumitbardhan
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Shakila Mahjabin
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
surya pandian
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
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
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
Buxoo Abdullah
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
eShikshak
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Julie Iskander
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
Jawad Khan
 
Music magazine analysis
Music magazine analysis Music magazine analysis
Music magazine analysis
JoshuaDuke77
 
Double google-shopping-sales-in-1-hour
Double google-shopping-sales-in-1-hourDouble google-shopping-sales-in-1-hour
Double google-shopping-sales-in-1-hour
Waleed Bhutta
 
As unit g321 research and planning workbook joshua duke
As unit g321 research and planning workbook   joshua dukeAs unit g321 research and planning workbook   joshua duke
As unit g321 research and planning workbook joshua duke
JoshuaDuke77
 

Viewers also liked (20)

Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Queue
QueueQueue
Queue
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queues
QueuesQueues
Queues
 
358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9358 33 powerpoint-slides_9-stacks-queues_chapter-9
358 33 powerpoint-slides_9-stacks-queues_chapter-9
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Data Structure -List Stack Queue
Data Structure -List Stack QueueData Structure -List Stack Queue
Data Structure -List Stack Queue
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
 
Music magazine analysis
Music magazine analysis Music magazine analysis
Music magazine analysis
 
Double google-shopping-sales-in-1-hour
Double google-shopping-sales-in-1-hourDouble google-shopping-sales-in-1-hour
Double google-shopping-sales-in-1-hour
 
As unit g321 research and planning workbook joshua duke
As unit g321 research and planning workbook   joshua dukeAs unit g321 research and planning workbook   joshua duke
As unit g321 research and planning workbook joshua duke
 

Similar to Queues in C++

Queues and Stacks
Queues and StacksQueues and Stacks
Queues and Stacks
Prof Ansari
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
NuraMohamed9
 
05 queues
05 queues05 queues
05 queues
Rajan Gautam
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
maneesh boddu
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
SonaPathak4
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
Omprakash Chauhan
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
Toseef Hasan
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
SherinRappai
 
Queue
QueueQueue
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queueSenthil Kumar
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
QUEUES
QUEUESQUEUES
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
Stack and queue
Stack and queueStack and queue
Stack and queue
Anil Kumar Prajapati
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
skilljiolms
 
Queue
QueueQueue

Similar to Queues in C++ (20)

Queue
QueueQueue
Queue
 
Queues and Stacks
Queues and StacksQueues and Stacks
Queues and Stacks
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
05 queues
05 queues05 queues
05 queues
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Queue - Data Structure - Notes
Queue - Data Structure - NotesQueue - Data Structure - Notes
Queue - Data Structure - Notes
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Queue
QueueQueue
Queue
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
QUEUES
QUEUESQUEUES
QUEUES
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
VCE Unit 03vv.pptx
VCE Unit 03vv.pptxVCE Unit 03vv.pptx
VCE Unit 03vv.pptx
 
Queue
QueueQueue
Queue
 

More from Vineeta Garg

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
GUI programming
GUI programmingGUI programming
GUI programming
Vineeta Garg
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
Vineeta Garg
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
Vineeta Garg
 
SQL
SQLSQL
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
Vineeta Garg
 

More from Vineeta Garg (9)

Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
GUI programming
GUI programmingGUI programming
GUI programming
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Structured query language constraints
Structured query language constraintsStructured query language constraints
Structured query language constraints
 
SQL
SQLSQL
SQL
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 

Recently uploaded

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
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
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
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
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
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
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
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
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
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
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
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 ...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 

Queues in C++

  • 2. INTRODUCTION TO QUEUE DEFINITION:  A stack is a data structure that provides temporary storage of data in such a way that the element stored first will be retrieved first.  This method is also called FIFO – First In First Out. EXAMPLE: In real life we can think of queue as a queue of vehicles waiting at the petrol pump, people waiting at the bus stop for the bus etc. The first person to enter the queue is the first one to leave the queue. Similarly last person to join the queue is the last person to leave the queue.
  • 3. OPERATIONS ON QUEUE  A queue is a linear data structure.  It is controlled by two operations: insertion and deletion.  Insertion operation takes place from the rear end of the queue and deletion operation takes place from the front end of the queue.  Insertion operation adds an element to the queue.  Deletion operation removes an element from the queue.  There are two variables FRONT and REAR. FRONT points to the beginning of the filled queue and takes care of deletion operation. REAR points to the end of the queue and takes care of insertion operation.  These two operations implement the FIFO method .
  • 4. IMPLEMENTATION OF QUEUE Queue can be implemented in two ways:  As an Array  As a Linked List
  • 5. QUEUE AS AN ARRAY Array implementation of Queue uses  an array to store data  an integer type variable usually called the FRONT, which points to the beginning of the array and an integer variable REAR, which points to the end of the filled array. 30 20 10 2 4 3 2 1 0REAR 0 FRONT
  • 6. INSERTION OPERATION  Initially when the queue is empty, FRONT and REAR can have any integer value other than any valid index number of the array. Let FRONT = -1; REAR=-1;  The first element in the empty queue goes to the 0th position of the array and both FRONT and REAR are initialized to value 0.  After this every insertion operation increase the REAR by 1 and inserts new data at that particular position.  As arrays are fixed in length, elements can not be inserted beyond the maximum size of the array. Pushing data beyond the maximum size of the queue (i.e. when REAR = MAX SIZE -1) results in “data overflow”.
  • 7. INSERTION OPERATION EXPLAINED insert 10 front = 0 rear = 0
  • 8. DELETE OPERATION The delete operation deletes the very first item from the queue. Any attempt to delete an element from the empty queue (when FRONT+REAR=-1) results in “data underflow” condition. Each time the deletion operation is performed, the FRONT variable is decremented by 1. When there is only one element in the queue, deletion operation of that element makes the queue empty and both FRONT and REAR will be assigned the value -1.
  • 10. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS AN INTEGER ARRAY #include<iostream.h> #include<conio.h> #define size 4 class queue { int data[size]; int front,rear; public: queue() { front=-1; rear=-1; } void insert(); void deletion(); }; void queue::insert() { if(rear==size-1) { cout<<"n queue is full"; return; } else if(rear == -1) { rear++; front++; } else rear++; cout<<"Enter Data : "; cin>>data[rear]; } void queue::deletion() { if(front==-1) { cout<<"n Queue is empty"; return; } cout<<data[front]<<" deleted"<<endl; if(front==rear) { front=-1;rear=-1; } else front++; } void display() { for (int i=front;i<=rear; i++) cout<< data[rear]; } void main() { queue q; int ch; do { cout<<"n1. Insertn2. Deleten3. Displayn4.QuitnEnterChoice(1-3) "; cin>>ch; switch(ch) { case 1: q.insert();break; case 2: q.deletion();break; case 3. q.display(); } }while(ch!=3); }
  • 11. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS AN ARRAY OF OBJECTS #include<iostream.h> #include<conio.h> struct item { int ino; char name[20]; }; class queue { item data[4]; int front,rear; public: queue() { front=-1; rear=-1; } void insert(); void deletion(); }; void queue::insert() { if(rear==size-1) { cout<<"n queue is full"; return; } else if(rear == -1) { rear++; front++; } else rear++; cout<<"Enter Data : "; cin>>data[rear].ino>> data[rear].name; } void queue::deletion() { if(front==-1) { cout<<"n Queue is empty"; return; } cout<<data[front].ino<<" deleted"<<endl; if(front==rear) { front=-1;rear=-1; } else front++; } void display() { for (int i=front;i<=rear; i++) cout<< data[rear]].ino<< data[rear].name; } void main() { queue q; int ch; do { cout<<"n1. Insertn2. Deleten3. Displayn4.QuitnEnterChoice(1-3) "; cin>>ch; switch(ch) { case 1: q.insert();break; case 2: q.deletion();break; case 3. q.display(); } }while(ch!=3); }
  • 12. APPLICATIONS OF QUEUE A queue is an appropriate data structure on which information is stored and then retrieved in FIFO order.The applications of queue are as follows: Queues find their use in CPU scheduling, printer spooling, message queuing, computer networks etc. In time sharing system queues help in scheduling of jobs.
  • 13. DISADVANTAGE OF NORMAL QUEUE The queue as an array suffers from one major drawback. As arrays are fixed in size, elements can not be inserted beyond the maximum size of the array, even though in reality there might be empty slots in the beginning of the queue. REAR 30 40 50 60 0 1 2 3 4 5 2 5FRONT In this example, queue is considered as full although there are two empty spaces in the beginning of the queue.
  • 14. CIRCULAR QUEUE AS AN ARRAY Array implementation of Circular Queue uses  an array to store data  an integer type variable usually called the FRONT, which points to the beginning of the filled array and an integer variable REAR, which points to the end of the filled array. NOTE: When the number of additions makes REAR equal to the size of the array, the next element is inserted in the first slot provided it is free. Circular queue is full when all the slots of the array are occupied. 50 40 30 60 2 4 3 2 1 0 REAR 0 FRONT
  • 15. OPERATIONS ON CIRCULAR QUEUE  A queue is a linear data structure.  It is controlled by two operations: insertion and deletion.  Insertion operation takes place from the rear end of the queue and deletion operation takes place from the front end of the queue.  There are two variables FRONT and REAR. FRONT points to the beginning of the queue and takes care of deletion operation. REAR points to the end of the queue and takes care of insertion operation.  If REAR reaches the end of the queue then the next insertion operation makes REAR=0 provided FRONT is not 0.  If the FRONT reaches the end of the queue then the next deletion operation makes FRONT=0 provided there are more elements in the queue.
  • 16. CIRCULAR QUEUE: INSERTION OPERATION  Initially when the queue is empty, FRONT and REAR can have any integer value other than any valid index number of the array. Let FRONT = -1; REAR=-1;  The first element in the empty queue goes to the 0th position of the array and both FRONT and REAR are initialized to value 0.  After this every insertion operation increases the REAR by 1 and inserts new data at that particular position.  If REAR reaches the end of the queue then the next insertion operation makes REAR=0 provided FRONT is not 0.  The queue is full when REAR=FRONT +1 or (FRONT=0 and REAR=MAX-1). Insertion at this point results in “data overflow”.
  • 17. CIRCULAR QUEUE: INSERTION OPERATION EXPLAINED front=2
  • 18. CIRCULAR QUEUE:DELETE OPERATION The delete operation deletes the very first item from the queue. Any attempt to delete an element from the empty queue(when FRONT+REAR=-1) results in “data underflow” condition. Each time the deletion operation is performed, the FRONT variable is decremented by 1. When there is only one element in the queue, deletion operation of that element makes the queue empty and both FRONT and REAR will be assigned the value -1. When FRONT reaches the end of the queue then the next deletion operation makes FRONT = 0.
  • 19. CIRCULAR QUEUE: DELETION OPERATION EXPLAINED
  • 20. PROGRAMTO ILLUSTRATE OPERATIONS ON CIRCULAR QUEUE #include<iostream.h> #include<conio.h> #define size 4 class cqueue { int data[size]; int front,rear; public: cqueue() { front=-1;rear=-1; } void insert(); void remove(); }; void cqueue::insert(int num) { if(rear==size-1&& front==0 || front==rear+1) { cout<<"nCircular queue is full"; return; } else if(rear==-1) { rear++; front++; } else if(rear==size-1) rear=0; else rear++; cout<<"Enter Data : "; data[rear]=num; } void cqueue::remove() { if(front==-1) { cout<<"n Circular Queue is empty";return; } cout<<data[front]<<" deleted"<<endl; if(front==rear) { front=-1;rear=-1; } else if(front==size-1) front=0; else front++; } void cqueue::disp() {if(front<rear) for(int i= front;i<=rear; i++) cout<<data[i]; else {for(int i=front;i<=size-1; i++) cout<<data[i]; for(int i= 0;i<=rear; i++) cout<<data[i]; }} void main() { cqueue cq; int ch; do { cout<<"n1. Insert n2. Removen3. Displayn 4. Quit n Enter Choice(1-4) "; cin>>ch; switch(ch) { case 1: cq.insert();break; case 2: cq.remove();break; case 3: cq.disp(); } }while(ch!=4);}
  • 21. QUEUE AS A LINKED LIST Linked list implementation of queue uses:  A linked list to store data  A pointer FRONT pointing to the beginning of the queue and a pointer REAR pointing to the end of the queue. NOTE: Each node of a queue as a linked list has two parts : data part and link part and is created with the help of self referential structure. The data part stores the data and link part stores the address of the next node of the linked list. FRONT NODE DATA LINK REAR
  • 22. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS A LINKED LIST #include<iostream.h> #include<conio.h> struct node { int data; node *next; }; class queue { node *rear,*front; public: queue() { rear=NULL;front=NULL;} void qinsert(); void qdelete(); void qdisplay(); }; void queue::qinsert() { node *temp; temp=new node; cout<<"Data :"; cin>>temp->data; temp->next=NULL; if(rear==NULL) { rear=temp; front=temp; } else { rear->next=temp; rear=temp; } } void queue::qdelete() { if(front!=NULL) { node *temp=front; cout<<front->data <<"deleted n"; front=front->next; delete temp; if(front==NULL) rear=NULL; } else cout<<“empty Queue “; } void queue::qdisplay() { node *temp=front; while(temp!=NULL) { cout<<temp->data; temp=temp->next; } } void main() { queue q1; char ch; do { cout<< "i. insertn d. Deletens. Display n q. quit "; cin>>ch; switch(ch) { case 'i' : q1.qinsert();break; case 'd' : q1.qdelete();break; case 's' : q1.qdisplay(); } }while(ch!='q'); }
  • 23. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS A LINKED LIST : Explanation #include<iostream.h> #include<conio.h> struct node { int data; node *next; }; class queue { node *rear,*front; // front point to the beginning of the queue and rear points to the end of the queue public: queue() { rear=NULL;front=NULL;} // Initializes front and rear to NULL void qinsert(); void qdelete(); void qdisplay(); }; Self Referential Structure:These are special structures which contains pointers to themselves. Here, next is a pointer of type node itself. Self Referential structures are needed to create Linked Lists.
  • 24. PROGRAMTO ILLUSTRATE OPERATIONS ON QUEUE AS A LINKED LIST : Explanation void queue::qinsert() { node *temp; // pointer of type node temp=new node; // new operator will create a new //node and address of new node // is stored in temp cout<<"Data :"; cin>>temp->data; // adding data in the node temp->next=NULL; // because new node is always // added at the end if(rear==NULL) // if queue is empty, new node { rear=temp; // becomes the first node and front=temp; // both front and rear will point to } //it else { rear->next=temp; // If queue is not empty ,last rear=temp; // node will contain address } // of new node and rear will } // point to new node void queue::qdelete() { if(front!=NULL) // if queue is not empty { node *temp=front; // temp is a pointer //containing address of // first node cout<<front->data <<"deleted n"; front=front->next; // front will now contain // address of second node delete temp; // delete operator will delete the node // stored at temp if(front==NULL) /*if front become NULL after deletion, it means that there was only one node in the queue and deletion of that node will queue empty */ rear=NULL; } else cout<<“empty Queue “;}