SlideShare a Scribd company logo
1 of 16
Chapter 5Chapter 5
queuesqueues
01/31/18 BY MS. SHAISTA QADIR 1
PRESENTED BY
Shaista Qadir
Lecturer king khalid university
CONteNtsCONteNts
 queue
 OperatiONs perfOrmed ON queue
 queue appliCatiONs
 example tO eNqueue
 algOrithm tO enqueue() / add () aN elemeNt
(item )iN the queue
 example tO dequeue
 algOrithm tO dequeue() / remove() aN elemeNt
(item )iN the queue
 priOritY queue
 priOritY queue represeNtatiON
01/31/18 BY MS. SHAISTA QADIR 2
queuequeue
01/31/18 BY MS. SHAISTA QADIR 3
 A queue is simply a waiting line that grows by adding
elements to its end and shrinks by taking elements from its
front.
 Unlike a stack, a queue is a structure in which both ends
are used.
 one for adding new elements and one for removing them.
 Therefore, the last element has to wait until all elements
preceding it on the queue are removed.
 A queue is an FIFO structure: first in/first out.
OperatiONs perfOrmed ONOperatiONs perfOrmed ON
queuequeue
01/31/18 BY MS. SHAISTA QADIR 4
 Queue operations are similar to stack operations. The
following operations are needed to properly manage a
queue:
 clear() : Clear the queue.
 isEmpty() : Check to see if the queue is empty.
 enqueue(el): Put the element el at the end of the queue.
 dequeue() : Take the first element from the queue.
 firstEl() : Return the first element in the queue without
removing it.
operations performed onoperations performed on
QUeUeQUeUe
01/31/18 BY MS. SHAISTA QADIR 5
 A series of enqueue and dequeue operations is shown in Figure .
 This time—unlike for stacks—the changes have to be monitored
both at the beginning of the queue and at the end.
 The elements are enqueued on one end and dequeued from the
other.
 For example, after enqueuing 10 and then 5, the dequeue
operation removes 10 from the queue
QUeUe applicationsQUeUe applications
01/31/18 BY MS. SHAISTA QADIR 6
APPLICATIONS OF QUEUE:
◦ Waiting Line
◦ Access to Shared recourses (Example: Printer)
◦ Multiprogramming
◦ Client-Server system
01/31/18 BY MS. SHAISTA QADIR 7
 Example: FOR enqueue() / add()
 array size is 8,
 therefore, the index of the array will be from 0 to 7.
 Elements inside array are 1, 7, 5 and 2
 When we enqueue(6), 6 has been inserted in the queue.
 Now, the rear index is containing 4 while the front has the
same 0 index.
example to enQUeUeexample to enQUeUe
01/31/18 BY MS. SHAISTA QADIR 8
 Example: FOR enqueue() / add()
 When we enqueue(8) another element 8 is inserted in the
queue.
example to enQUeUeexample to enQUeUe
Algorithm toAlgorithm to enqueue() / add ()enqueue() / add ()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
01/31/18 BY MS. SHAISTA QADIR 9
Algorithm To enqueue() / add () An Element (ITEM )In The
Queue:
1. If (REAR == max) Then
2. Print: Overflow
3. Else
4. If (FRONT and REAR == 0) Then
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else
6. Set REAR = REAR + 1 [End of Step 4 If]
7. QUEUE[REAR] = ITEM
8. Print: ITEM inserted [End of Step 1 If]
9. Exit
01/31/18 BY MS. SHAISTA QADIR 10
PROGRAM LOGIC:
void enqueue(int j)
{
if(rear == maxsize-1)
rear = -1;
qarr[++rear] = j;
cnt++;
}
Algorithm toAlgorithm to enqueue() / add ()enqueue() / add ()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
01/31/18 BY MS. SHAISTA QADIR 11
 Example To dequeue ()/ remove () an element from
queue.
 Element is deleted from front end.
 After a call of dequeue() method:
 Another call of dequeue() method:
ExAmplE to dEQuEuEExAmplE to dEQuEuE
Algorithm toAlgorithm to dequeue() / remove()dequeue() / remove()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
01/31/18 BY MS. SHAISTA QADIR 12
Algorithm to dequeue()/ remove () an element (ITEM )
from the queue:
1. If (FRONT == 0) Then
2. Print: Underflow // Check for underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then // Check if only one element in Queue
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else
7. Set FRONT = FRONT + 1 [End of Step 5 If]
8. Print: ITEM deleted [End of Step 1 If]
9. Exit
Algorithm toAlgorithm to dequeue() / remove()dequeue() / remove()
An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE
QuEuEQuEuE
01/31/18 BY MS. SHAISTA QADIR 13
PROGRAM LOGIC:
int dequeue()
{
int temp = qarr[front];
front++;
if(front == maxsize)
front = 0;
cnt--;
return temp;
}
PrioritY QuEuEPrioritY QuEuE
01/31/18 BY MS. SHAISTA QADIR 14
• In many situations, simple queues are inadequate, as when
first in/first out scheduling has to be overruled using some
priority criteria.
• In a post office example, a handicapped person may have
priority over others.
• Therefore, when a clerk is available, a handicapped person is
served instead of someone from the front of the queue.
• In a sequence of processes, process P2 may need to be
executed before process P1 for the proper functioning of a
system, even though P1 was put on the queue of waiting
processes before P2.
• In situations like these, a modified queue, or priority queue,
is needed. In priority queues, elements are dequeued
according to their priority and their current queue position.
PrioritY QuEuE rEPrEsEntAtionPrioritY QuEuE rEPrEsEntAtion
01/31/18 BY MS. SHAISTA QADIR 15
• Priority queues can be represented by two variations of
linked lists.
• In one type of linked list, all elements are entry ordered,
• And in another, order is maintained by putting a new
element in its proper position according to its priority.
• In both cases, the total operational times are O(n)
• because, for an unordered list, adding an element is
immediate but searching is O(n),
• And in a sorted list, taking an element is immediate but
adding an element is O(n).
01/31/18 BY MS. SHAISTA QADIR 16
THANK YOUTHANK YOU

More Related Content

What's hot

Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 

What's hot (20)

Priority queues
Priority queuesPriority queues
Priority queues
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Priority queues and heap sorting
Priority queues and heap sortingPriority queues and heap sorting
Priority queues and heap sorting
 
Team 6
Team 6Team 6
Team 6
 
Queue
QueueQueue
Queue
 
Queue
QueueQueue
Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Queue by rajanikanth
Queue by rajanikanthQueue by rajanikanth
Queue by rajanikanth
 
Queue
QueueQueue
Queue
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Queue
QueueQueue
Queue
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
Queues
QueuesQueues
Queues
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Priority queues
Priority queuesPriority queues
Priority queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 

Similar to Queue

queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
RAtna29
 

Similar to Queue (20)

2 b queues
2 b queues2 b queues
2 b queues
 
Fundamentals of Data Structure and Queues
Fundamentals of Data Structure and QueuesFundamentals of Data Structure and Queues
Fundamentals of Data Structure and Queues
 
basics of queues
basics of queuesbasics of queues
basics of queues
 
Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
Queue
QueueQueue
Queue
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
 
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbbqueuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
queuesArrays.ppt bbbbbbbbbbbbbbbbbbbbbbbbbb
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
IRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data StructuresIRJET- Comparison of Stack and Queue Data Structures
IRJET- Comparison of Stack and Queue Data Structures
 
Data Structure Lecture 4
Data Structure Lecture 4Data Structure Lecture 4
Data Structure Lecture 4
 
@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx@Chapter 4 DSA Part II.pptx
@Chapter 4 DSA Part II.pptx
 
Unit – iv queue
Unit – iv    queueUnit – iv    queue
Unit – iv queue
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Data Structures - Lecture 2 - Unit 2.pptx
Data Structures - Lecture 2 - Unit 2.pptxData Structures - Lecture 2 - Unit 2.pptx
Data Structures - Lecture 2 - Unit 2.pptx
 
Chapter 7 ds
Chapter 7 dsChapter 7 ds
Chapter 7 ds
 
queue.pptx
queue.pptxqueue.pptx
queue.pptx
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
05 queues
05 queues05 queues
05 queues
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Queue

  • 1. Chapter 5Chapter 5 queuesqueues 01/31/18 BY MS. SHAISTA QADIR 1 PRESENTED BY Shaista Qadir Lecturer king khalid university
  • 2. CONteNtsCONteNts  queue  OperatiONs perfOrmed ON queue  queue appliCatiONs  example tO eNqueue  algOrithm tO enqueue() / add () aN elemeNt (item )iN the queue  example tO dequeue  algOrithm tO dequeue() / remove() aN elemeNt (item )iN the queue  priOritY queue  priOritY queue represeNtatiON 01/31/18 BY MS. SHAISTA QADIR 2
  • 3. queuequeue 01/31/18 BY MS. SHAISTA QADIR 3  A queue is simply a waiting line that grows by adding elements to its end and shrinks by taking elements from its front.  Unlike a stack, a queue is a structure in which both ends are used.  one for adding new elements and one for removing them.  Therefore, the last element has to wait until all elements preceding it on the queue are removed.  A queue is an FIFO structure: first in/first out.
  • 4. OperatiONs perfOrmed ONOperatiONs perfOrmed ON queuequeue 01/31/18 BY MS. SHAISTA QADIR 4  Queue operations are similar to stack operations. The following operations are needed to properly manage a queue:  clear() : Clear the queue.  isEmpty() : Check to see if the queue is empty.  enqueue(el): Put the element el at the end of the queue.  dequeue() : Take the first element from the queue.  firstEl() : Return the first element in the queue without removing it.
  • 5. operations performed onoperations performed on QUeUeQUeUe 01/31/18 BY MS. SHAISTA QADIR 5  A series of enqueue and dequeue operations is shown in Figure .  This time—unlike for stacks—the changes have to be monitored both at the beginning of the queue and at the end.  The elements are enqueued on one end and dequeued from the other.  For example, after enqueuing 10 and then 5, the dequeue operation removes 10 from the queue
  • 6. QUeUe applicationsQUeUe applications 01/31/18 BY MS. SHAISTA QADIR 6 APPLICATIONS OF QUEUE: ◦ Waiting Line ◦ Access to Shared recourses (Example: Printer) ◦ Multiprogramming ◦ Client-Server system
  • 7. 01/31/18 BY MS. SHAISTA QADIR 7  Example: FOR enqueue() / add()  array size is 8,  therefore, the index of the array will be from 0 to 7.  Elements inside array are 1, 7, 5 and 2  When we enqueue(6), 6 has been inserted in the queue.  Now, the rear index is containing 4 while the front has the same 0 index. example to enQUeUeexample to enQUeUe
  • 8. 01/31/18 BY MS. SHAISTA QADIR 8  Example: FOR enqueue() / add()  When we enqueue(8) another element 8 is inserted in the queue. example to enQUeUeexample to enQUeUe
  • 9. Algorithm toAlgorithm to enqueue() / add ()enqueue() / add () An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE QuEuEQuEuE 01/31/18 BY MS. SHAISTA QADIR 9 Algorithm To enqueue() / add () An Element (ITEM )In The Queue: 1. If (REAR == max) Then 2. Print: Overflow 3. Else 4. If (FRONT and REAR == 0) Then (a) Set FRONT = 1 (b) Set REAR = 1 5. Else 6. Set REAR = REAR + 1 [End of Step 4 If] 7. QUEUE[REAR] = ITEM 8. Print: ITEM inserted [End of Step 1 If] 9. Exit
  • 10. 01/31/18 BY MS. SHAISTA QADIR 10 PROGRAM LOGIC: void enqueue(int j) { if(rear == maxsize-1) rear = -1; qarr[++rear] = j; cnt++; } Algorithm toAlgorithm to enqueue() / add ()enqueue() / add () An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE QuEuEQuEuE
  • 11. 01/31/18 BY MS. SHAISTA QADIR 11  Example To dequeue ()/ remove () an element from queue.  Element is deleted from front end.  After a call of dequeue() method:  Another call of dequeue() method: ExAmplE to dEQuEuEExAmplE to dEQuEuE
  • 12. Algorithm toAlgorithm to dequeue() / remove()dequeue() / remove() An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE QuEuEQuEuE 01/31/18 BY MS. SHAISTA QADIR 12 Algorithm to dequeue()/ remove () an element (ITEM ) from the queue: 1. If (FRONT == 0) Then 2. Print: Underflow // Check for underflow 3. Else 4. ITEM = QUEUE[FRONT] 5. If (FRONT == REAR) Then // Check if only one element in Queue (a) Set FRONT = 0 (b) Set REAR = 0 6. Else 7. Set FRONT = FRONT + 1 [End of Step 5 If] 8. Print: ITEM deleted [End of Step 1 If] 9. Exit
  • 13. Algorithm toAlgorithm to dequeue() / remove()dequeue() / remove() An ElEmEnt (itEm )in thEAn ElEmEnt (itEm )in thE QuEuEQuEuE 01/31/18 BY MS. SHAISTA QADIR 13 PROGRAM LOGIC: int dequeue() { int temp = qarr[front]; front++; if(front == maxsize) front = 0; cnt--; return temp; }
  • 14. PrioritY QuEuEPrioritY QuEuE 01/31/18 BY MS. SHAISTA QADIR 14 • In many situations, simple queues are inadequate, as when first in/first out scheduling has to be overruled using some priority criteria. • In a post office example, a handicapped person may have priority over others. • Therefore, when a clerk is available, a handicapped person is served instead of someone from the front of the queue. • In a sequence of processes, process P2 may need to be executed before process P1 for the proper functioning of a system, even though P1 was put on the queue of waiting processes before P2. • In situations like these, a modified queue, or priority queue, is needed. In priority queues, elements are dequeued according to their priority and their current queue position.
  • 15. PrioritY QuEuE rEPrEsEntAtionPrioritY QuEuE rEPrEsEntAtion 01/31/18 BY MS. SHAISTA QADIR 15 • Priority queues can be represented by two variations of linked lists. • In one type of linked list, all elements are entry ordered, • And in another, order is maintained by putting a new element in its proper position according to its priority. • In both cases, the total operational times are O(n) • because, for an unordered list, adding an element is immediate but searching is O(n), • And in a sorted list, taking an element is immediate but adding an element is O(n).
  • 16. 01/31/18 BY MS. SHAISTA QADIR 16 THANK YOUTHANK YOU