SlideShare une entreprise Scribd logo
1  sur  35
QUEUE
INTRODUCTION 
• It is linear data structure 
• It is collection of items – List 
• Queue means line of items waiting for their turn 
• Queue has two ends 
• Elements are added at one end. 
• Elements are removed from the other end.
QUEUE 
• Removal of data item is restricted at one end known as FRONT 
• Insertion of data item is restricted at other end known as REAR 
• The FRONT and REAR are used in describing a linear list, 
when queue is implemented 
• First element inserted in list, will be the first to be removed - 
FIFO
QUEUE AS FIFO 
• The element inserted first will be removed first from the Queue 
• Thus, Queue is known as FIFO (First In-First Out) or FCFS (First 
Come First Serve) 
• Examples of Queue 
○ People waiting in Queue to purchase tickets at railway station or cinema 
hall, where the first person in the queue will be served first
REPRESENTATION OF QUEUE 
• It has two pointer variables 
• ○ FRONT : Containing the location of the front element of the 
queue 
• ○ REAR : Containing the location of the rear element of the queue 
• When queue is empty 
• FRONT = -1 and REAR = -1
OPERATIONS OF QUEUE 
• Insertion 
• Adding an element in queue will increased 
• value of REAR by 1 
• REAR = REAR + 1 
• Removal 
• Removing an element from queue will increased value of FRONT by 1 
• FRONT = FRONT + 1
TYPES OF QUEUE 
• Simple queue 
• Circular Queue 
• Priority Queue 
• Dqueue
SIMPLE QUEUE 
• A simple queue is a list of item which are arranged in a line i.e. 
queue. 
• So we can do insertion only at rear end and the deleteion can be 
done at the front end only. 
• This way the insertion and deletion of the simple queue 
functions
SIMPLE QUEUE :-INSERTION 
INSERTION(QUEUE, F, R, N, item) 
Step 1. if (R >= N) 
print “Stack is full” 
return 0; 
Step 2. R = R + 1; /* Increment R*/ 
Step 3. QUEUE [R] =item; /*Insert Element*/ 
Step 4. if (F=-1) 
F=0; 
return f;
SIMPLE QUEUE :-DELETION 
PROCEDURE DELETE(QUEUE, F, R, item) 
[Deletes ‘item’ from the ‘stack’, ‘F’ is the Front end pointer and ‘R’ is the rare end pointer] 
Step 1. if (F == -1) /* Underflow*/ 
print “Stack is empty.” 
end; 
2. item = QUEUE[F]; /* Deleting Element*/ 
3. F = F + 1; /*Incrementing F*/ 
Step 4. if (F > R) 
then F = R = -1; 
end;
CIRCULAR QUEUE 
• To solve this problem, queues implement wrapping around. 
• Such queues are called Circular Queues. 
• Both the front and the rear pointers wrap around to the beginning of the 
array. 
• It is also called as “Ring buffer”. 
• Items can inserted and deleted from a queue in O(1) time.
CIRCULAR QUEUE 
• When a new item is inserted at the rear, the pointer to rear moves 
upwards. 
• Similarly, when an item is deleted from the queue the front arrow moves 
downwards. 
• After a few insert and delete operations the rear might reach the end of 
the queue and no more items can be inserted although the items from the 
front of the queue have been deleted and there is space in the queue.
CIRCULAR QUEUE :- INSERTION 
• Insert CQueue ( ) 
• Step 1. If (FRONT == 0 and REAR == N-1) or (FRONT == REAR + 1) Then 
• Step 2. Print: Overflow 
• Step 3. Else 
• Step 4. If (REAR == -1) Then [Check if QUEUE is empty] 
• (a) Set FRONT = 0 
• (b) Set REAR = 0 
• Step 5. Else If (REAR == N-1) Then [If REAR reaches end if QUEUE] 
• Step 6. Set REAR = 0 
• Step 7. Else 
• Step 8. Set REAR = REAR + 1 [Increment REAR by 1] [End of Step 4 If] 
• Step 9. Set QUEUE[REAR] = ITEM 
• Step 10. Print: ITEM inserted 
• [End of Step 1 If] 
• Step11. Exit
CIRCULAR QUEUE :-DELETION 
• Delete CircularQueue ( ) 
• Step 1. If (FRONT == -1) Then [Check for Underflow] 
• Step 2. Print: Underflow 
• Step 3. Else 
• Step 4. ITEM = QUEUE[FRONT] 
• Step 5. If (FRONT == REAR) Then [If only element is left] 
• (a) Set FRONT = -1 
• (b) Set REAR = -1 
• Step 6. Else If (FRONT == N-1) Then [If FRONT reaches end if QUEUE] 
• Step 7. Set FRONT = 0 
• Step 8. Else 
• Step 9. Set FRONT = FRONT + 1 [Increment FRONT by 1] 
• [End of Step 5 If] 
• Step 10. Print: ITEM deleted 
• [End of Step 1 If] 
• Step 11. Exit
PRIORITY QUEUE 
Suppose you have to work o few assignment then on which 
assignment will you work first ? So here we have decided the priority 
of the work to be done 
You have set priority on basis of days . And this is known as keys.
PRIORITY QUEUE 
• It is collection of elements where elements are stored according to the their 
priority levels 
• Inserting and removing of elements from queue is decided by the priority 
of the elements 
• The two fundamental methods of a priority queue P: 
○ insertItem(k,e): Insert an element e with key k into P. 
○ removeMin(): Return and remove from P an element with the 
smallest key.
PRIORITY QUEUE 
• When you want to determine the priority for your assignments, 
you need a value for each assignment, that you can compare 
with each other. 
• key: An object that is assigned to an element as a specific 
attribute for that element, which can be used to identify , rank , 
or weight that element.
• Example: Student records 
• Any of the attributes, Student Name, Student Number, or Final Score 
can be used as keys. 
• Note: Keys may not be unique (Final Score).
RULES TO MAINTAIN A PRIORITY QUEUE 
• The elements with the higher priority will be processed before any 
element of lower priority 
• If there are elements with the same priority, then the element 
added first in the queue would get processed
PRIORITY QUEUE :- INSERTION 
• Step 1:q insert(q,val,prno) 
• Step 2:if (q->rear[prno]=Max-1 and q->front[prno]=0) or (q->rear[prno]+1=q->front[prno]) 
• Step 3:print “overflow” 
• Step 4:if q->rear[prno-1]=Max-1 
• Step 5:set q->rear[prno-1]=0; 
• Step 6:else 
• Step 7:set q->rear[prno-1]=q->rear[prno-1]+1; 
• Step 8:end if 
• Step 8:set q->cqueue(prno-1)[q->rear[prno-1]]=val 
• Step 9:if q->front[prno-1]=0; 
• Step 10:End if 
• Step 11:end
PRIORITY QUEUE :- DELETION 
• Step 1:Qdelete(q) 
• Step 2:set flag=0,i=0; 
• Step 3:while i<=Max-1 
• Step 4:If not(q->front[prno])=-1 
• Step 5:set flag=1; 
• Step 6:set del_val=q->cqueue[i][q->front[i]] 
• Step 7:if q->front[i]=q->rear[i] 
• Step 8:set q->front[i]=q->rear[i]=-1; 
• Step 9:Else if q->front[i]=Max-1 
• Step 10:set q->front=0
PRIORITY QUEUE :- DELETION 
• Step 11:else 
• Step 12 :set q->front[i]=q->front[i]+1; 
• Step 13:End if 
• Step 14:End if 
• Step 15:Break 
• Step 16:End if 
• Step 17:Set i=i+1 
• Step 18:End while 
• Step 19:If flag=0 
• Step 20:Print “underflow” 
• Step 21:Return 0 and go to step 4 
• Step 22:Else 
• Step 23:Return del_val 
• Step 24:End if 
• Step 25:end
DEQUE 
• Deque stands for double-end queue 
• A data structure in which elements can be added or deleted at either the 
front or rear 
• But no changes can be made in the list 
• Deque is generalization of both stack and queue
DEQUE :- INSERT AND DELETE 
• There are two variations of a deque. 
• These are 
• Input Restricted Deque 
• An input restricted deque restricts the insertion of elements at one 
end only, but the deletion of elements can perform at both the 
ends. 
• Output Restricted Deque 
• An output restricted queue, restricts the deletion of elements at 
one end only, and allows insertion to be done at both the ends of 
deque
POSSIBILITIES 
• The two possibilities that must be considered while inserting or 
deleting elements into the queue are : 
• When an attempt is made to insert a element into a deque which is 
already full, an overflow occurs. 
• When an attempt is made to delete an element from a deque which is 
empty , underflow occurs.
REPRESENTATION OF DEQUE
INSERTING ELEMENT IN DEQUE
REMOVING ELEMENT FROM DEQUE
INPUT RESTRICTED QUEUE : INSERT ELEMENT 
• Step 1: start 
• Step 2:[check condition for overflow] 
• If(rear==N-1 && front==0 or front=rear+1) 
• Print “over flow” 
• else 
• goto step 3 
• Step 3: [check condition] 
• If(front==null) 
• front = 0 and rear=-1
INPUT RESTRICTED QUEUE : INSERT ELEMENT 
• else 
• goto step 4 
• Step 4: [check condition and value] 
• If (rear== N -1) 
• rear=0 
• Else 
• rear=rear+1 
• Step 5: [Add value] 
• dq[rear]=value 
• Step 6:end
INPUT RESTRICTED QUEUE : 
DELETE BEGINNING 
• Step 1: start 
• Step 2: [check condition for underflow] 
• If(rear==null & front==null) 
• Print”underflow” 
• else 
• goto step 3 
• Step 3: [delete] 
• dq[rear]=null 
• Step 4: [check condition] 
• If(rear==front) 
• front=rear=null 
• else 
• rear--; 
• Step 5: end
INPUT RESTRICTED QUEUE : 
• Step 1: start DELETE END 
• Step2 : [check condition for under flow) 
• If(rear==null & front==null) 
• Print “under flow” 
• else 
• goto step 3 
• Step 3: [delete] 
• dq[front]=null 
• Step 4: [check condition] 
• If(rear==front) 
• rear=-1 and front=null 
• else 
• front=front+1 
• Step 5: end
Detalied information of queue

Contenu connexe

Tendances

Tendances (20)

Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Queue-Data Structure
Queue-Data StructureQueue-Data Structure
Queue-Data Structure
 
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)
 
Team 6
Team 6Team 6
Team 6
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Queues
QueuesQueues
Queues
 
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)
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)Algorithm and Programming (Sorting)
Algorithm and Programming (Sorting)
 
Queue
QueueQueue
Queue
 
Dsa circular queue
Dsa circular queueDsa circular queue
Dsa circular queue
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 

Similaire à Detalied information of 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 preparationRAtna29
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertexSadiaSharmin40
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptxDdushb
 
Queues-and-CQueue-Implementation
Queues-and-CQueue-ImplementationQueues-and-CQueue-Implementation
Queues-and-CQueue-Implementationshaik faroq
 
stack and queue array implementation, java.
stack and queue array implementation, java.stack and queue array implementation, java.
stack and queue array implementation, java.CIIT Atd.
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.CIIT Atd.
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxMeghaKulkarni27
 
Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsAakash deep Singhal
 

Similaire à Detalied information of queue (20)

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
 
Queue
QueueQueue
Queue
 
08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex08_Queues.pptx showing how que works given vertex
08_Queues.pptx showing how que works given vertex
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Stack and Queue.pptx
Stack and Queue.pptxStack and Queue.pptx
Stack and Queue.pptx
 
Queue
QueueQueue
Queue
 
Data structures
Data structuresData structures
Data structures
 
QUEUE.pptx
QUEUE.pptxQUEUE.pptx
QUEUE.pptx
 
Queues-and-CQueue-Implementation
Queues-and-CQueue-ImplementationQueues-and-CQueue-Implementation
Queues-and-CQueue-Implementation
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Queues & ITS TYPES
Queues & ITS TYPESQueues & ITS TYPES
Queues & ITS TYPES
 
QUEUES
QUEUESQUEUES
QUEUES
 
stack and queue array implementation, java.
stack and queue array implementation, java.stack and queue array implementation, java.
stack and queue array implementation, java.
 
stack and queue array implementation in java.
stack and queue array implementation in java.stack and queue array implementation in java.
stack and queue array implementation in java.
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
 
Queues
Queues Queues
Queues
 
Lecture 7 data structures and algorithms
Lecture 7 data structures and algorithmsLecture 7 data structures and algorithms
Lecture 7 data structures and algorithms
 
6.queue
6.queue6.queue
6.queue
 

Dernier

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Dernier (20)

Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

Detalied information of queue

  • 2. INTRODUCTION • It is linear data structure • It is collection of items – List • Queue means line of items waiting for their turn • Queue has two ends • Elements are added at one end. • Elements are removed from the other end.
  • 3. QUEUE • Removal of data item is restricted at one end known as FRONT • Insertion of data item is restricted at other end known as REAR • The FRONT and REAR are used in describing a linear list, when queue is implemented • First element inserted in list, will be the first to be removed - FIFO
  • 4. QUEUE AS FIFO • The element inserted first will be removed first from the Queue • Thus, Queue is known as FIFO (First In-First Out) or FCFS (First Come First Serve) • Examples of Queue ○ People waiting in Queue to purchase tickets at railway station or cinema hall, where the first person in the queue will be served first
  • 5. REPRESENTATION OF QUEUE • It has two pointer variables • ○ FRONT : Containing the location of the front element of the queue • ○ REAR : Containing the location of the rear element of the queue • When queue is empty • FRONT = -1 and REAR = -1
  • 6. OPERATIONS OF QUEUE • Insertion • Adding an element in queue will increased • value of REAR by 1 • REAR = REAR + 1 • Removal • Removing an element from queue will increased value of FRONT by 1 • FRONT = FRONT + 1
  • 7.
  • 8. TYPES OF QUEUE • Simple queue • Circular Queue • Priority Queue • Dqueue
  • 9. SIMPLE QUEUE • A simple queue is a list of item which are arranged in a line i.e. queue. • So we can do insertion only at rear end and the deleteion can be done at the front end only. • This way the insertion and deletion of the simple queue functions
  • 10. SIMPLE QUEUE :-INSERTION INSERTION(QUEUE, F, R, N, item) Step 1. if (R >= N) print “Stack is full” return 0; Step 2. R = R + 1; /* Increment R*/ Step 3. QUEUE [R] =item; /*Insert Element*/ Step 4. if (F=-1) F=0; return f;
  • 11. SIMPLE QUEUE :-DELETION PROCEDURE DELETE(QUEUE, F, R, item) [Deletes ‘item’ from the ‘stack’, ‘F’ is the Front end pointer and ‘R’ is the rare end pointer] Step 1. if (F == -1) /* Underflow*/ print “Stack is empty.” end; 2. item = QUEUE[F]; /* Deleting Element*/ 3. F = F + 1; /*Incrementing F*/ Step 4. if (F > R) then F = R = -1; end;
  • 12. CIRCULAR QUEUE • To solve this problem, queues implement wrapping around. • Such queues are called Circular Queues. • Both the front and the rear pointers wrap around to the beginning of the array. • It is also called as “Ring buffer”. • Items can inserted and deleted from a queue in O(1) time.
  • 13. CIRCULAR QUEUE • When a new item is inserted at the rear, the pointer to rear moves upwards. • Similarly, when an item is deleted from the queue the front arrow moves downwards. • After a few insert and delete operations the rear might reach the end of the queue and no more items can be inserted although the items from the front of the queue have been deleted and there is space in the queue.
  • 14. CIRCULAR QUEUE :- INSERTION • Insert CQueue ( ) • Step 1. If (FRONT == 0 and REAR == N-1) or (FRONT == REAR + 1) Then • Step 2. Print: Overflow • Step 3. Else • Step 4. If (REAR == -1) Then [Check if QUEUE is empty] • (a) Set FRONT = 0 • (b) Set REAR = 0 • Step 5. Else If (REAR == N-1) Then [If REAR reaches end if QUEUE] • Step 6. Set REAR = 0 • Step 7. Else • Step 8. Set REAR = REAR + 1 [Increment REAR by 1] [End of Step 4 If] • Step 9. Set QUEUE[REAR] = ITEM • Step 10. Print: ITEM inserted • [End of Step 1 If] • Step11. Exit
  • 15. CIRCULAR QUEUE :-DELETION • Delete CircularQueue ( ) • Step 1. If (FRONT == -1) Then [Check for Underflow] • Step 2. Print: Underflow • Step 3. Else • Step 4. ITEM = QUEUE[FRONT] • Step 5. If (FRONT == REAR) Then [If only element is left] • (a) Set FRONT = -1 • (b) Set REAR = -1 • Step 6. Else If (FRONT == N-1) Then [If FRONT reaches end if QUEUE] • Step 7. Set FRONT = 0 • Step 8. Else • Step 9. Set FRONT = FRONT + 1 [Increment FRONT by 1] • [End of Step 5 If] • Step 10. Print: ITEM deleted • [End of Step 1 If] • Step 11. Exit
  • 16. PRIORITY QUEUE Suppose you have to work o few assignment then on which assignment will you work first ? So here we have decided the priority of the work to be done You have set priority on basis of days . And this is known as keys.
  • 17. PRIORITY QUEUE • It is collection of elements where elements are stored according to the their priority levels • Inserting and removing of elements from queue is decided by the priority of the elements • The two fundamental methods of a priority queue P: ○ insertItem(k,e): Insert an element e with key k into P. ○ removeMin(): Return and remove from P an element with the smallest key.
  • 18. PRIORITY QUEUE • When you want to determine the priority for your assignments, you need a value for each assignment, that you can compare with each other. • key: An object that is assigned to an element as a specific attribute for that element, which can be used to identify , rank , or weight that element.
  • 19. • Example: Student records • Any of the attributes, Student Name, Student Number, or Final Score can be used as keys. • Note: Keys may not be unique (Final Score).
  • 20. RULES TO MAINTAIN A PRIORITY QUEUE • The elements with the higher priority will be processed before any element of lower priority • If there are elements with the same priority, then the element added first in the queue would get processed
  • 21. PRIORITY QUEUE :- INSERTION • Step 1:q insert(q,val,prno) • Step 2:if (q->rear[prno]=Max-1 and q->front[prno]=0) or (q->rear[prno]+1=q->front[prno]) • Step 3:print “overflow” • Step 4:if q->rear[prno-1]=Max-1 • Step 5:set q->rear[prno-1]=0; • Step 6:else • Step 7:set q->rear[prno-1]=q->rear[prno-1]+1; • Step 8:end if • Step 8:set q->cqueue(prno-1)[q->rear[prno-1]]=val • Step 9:if q->front[prno-1]=0; • Step 10:End if • Step 11:end
  • 22. PRIORITY QUEUE :- DELETION • Step 1:Qdelete(q) • Step 2:set flag=0,i=0; • Step 3:while i<=Max-1 • Step 4:If not(q->front[prno])=-1 • Step 5:set flag=1; • Step 6:set del_val=q->cqueue[i][q->front[i]] • Step 7:if q->front[i]=q->rear[i] • Step 8:set q->front[i]=q->rear[i]=-1; • Step 9:Else if q->front[i]=Max-1 • Step 10:set q->front=0
  • 23. PRIORITY QUEUE :- DELETION • Step 11:else • Step 12 :set q->front[i]=q->front[i]+1; • Step 13:End if • Step 14:End if • Step 15:Break • Step 16:End if • Step 17:Set i=i+1 • Step 18:End while • Step 19:If flag=0 • Step 20:Print “underflow” • Step 21:Return 0 and go to step 4 • Step 22:Else • Step 23:Return del_val • Step 24:End if • Step 25:end
  • 24. DEQUE • Deque stands for double-end queue • A data structure in which elements can be added or deleted at either the front or rear • But no changes can be made in the list • Deque is generalization of both stack and queue
  • 25. DEQUE :- INSERT AND DELETE • There are two variations of a deque. • These are • Input Restricted Deque • An input restricted deque restricts the insertion of elements at one end only, but the deletion of elements can perform at both the ends. • Output Restricted Deque • An output restricted queue, restricts the deletion of elements at one end only, and allows insertion to be done at both the ends of deque
  • 26. POSSIBILITIES • The two possibilities that must be considered while inserting or deleting elements into the queue are : • When an attempt is made to insert a element into a deque which is already full, an overflow occurs. • When an attempt is made to delete an element from a deque which is empty , underflow occurs.
  • 29.
  • 31. INPUT RESTRICTED QUEUE : INSERT ELEMENT • Step 1: start • Step 2:[check condition for overflow] • If(rear==N-1 && front==0 or front=rear+1) • Print “over flow” • else • goto step 3 • Step 3: [check condition] • If(front==null) • front = 0 and rear=-1
  • 32. INPUT RESTRICTED QUEUE : INSERT ELEMENT • else • goto step 4 • Step 4: [check condition and value] • If (rear== N -1) • rear=0 • Else • rear=rear+1 • Step 5: [Add value] • dq[rear]=value • Step 6:end
  • 33. INPUT RESTRICTED QUEUE : DELETE BEGINNING • Step 1: start • Step 2: [check condition for underflow] • If(rear==null & front==null) • Print”underflow” • else • goto step 3 • Step 3: [delete] • dq[rear]=null • Step 4: [check condition] • If(rear==front) • front=rear=null • else • rear--; • Step 5: end
  • 34. INPUT RESTRICTED QUEUE : • Step 1: start DELETE END • Step2 : [check condition for under flow) • If(rear==null & front==null) • Print “under flow” • else • goto step 3 • Step 3: [delete] • dq[front]=null • Step 4: [check condition] • If(rear==front) • rear=-1 and front=null • else • front=front+1 • Step 5: end