DATA STRUCTURES
QUEUE
27/08/2020
PROF.RAMYA.R.B , ASSISTANT PROFESSOR , COMPUTER ENGINEERING,APST THANE
OBJECTIVE
“
“
To learn the ADT of queue and to implement queue using arrays .
02
03
03
04
Algorithm for Queue Operations
Array Implementation of Queue
Types of Queue
04
01
05
Applications
Queue ADT
05
06 Disadvantages
Queue
A queue is an important data structure which is extensively used in computer
application.
Ordered collection of homogeneous elements
Non-primitive linear data structure.
A new element is added at one end called rear end and the existing elements are
deleted from the other end called front end.
This mechanism is called First-In-First-Out (FIFO).
• The element in the queue are added at one end called “rear” and removed from another
end called front.
• The process of inserting element in the queue is called enqueue and the process of
deleting element from the queue is dequeue.
Total no of elements in queue= rear – front +1
front=0 1 rear= 2 3
2 4 6
Operations performed on the queue
1. Insertion/Enqueue:
0 1 2 3 4 5 6 7
After insertion:
• Front=0
• Rear=5
12 9 8 2 6 10
Algorithm for enque
Step.1: if rear=max-1,then write “overflow”
and exit.
[ end of if loop]
Step.2: if front=-1 and rear=-1 then,
set front=0 and rear=0
Else
rear=rear+1.
[end of if loop]
Step.3:set queue[rear]=num.
Step.4:exit.
Deletion operation/Deque
• Queue after deletion:
0 1 2 3 4 5 6 7
• Front=1
• Rear=5
9 8 2 6 10
Algorithm for deletion/Deque
Step:1. If front=-1 or front>rear then,
Write “underflow”.
else
Set val=queue[front].
Set front=front+1.
[end of if]
Step.2 exit.
Linked implementation of queue
• In linked queue, every element has 2 parts one that stores the data
and other that stores the address of next element.
• The start pointer is used as first and another pointer called rear for
the last element in the queue.
• If front=rear=null, then it indicated that queue is empty.
Operations on linked queue
1. Insertion
2. Deletion
Linked queue is:
front rear
3 4 8 null
• Linked queue after insertion:
Front rear
• Linked queue after deletion:
Front rear
4 8 null
3 4 8 12 null
Implementation of Linear Queue using
array
Types of queue
• Circular queue
• Dequeue
• Priority queue
• Multiple queue
1.Circular queue
In a standard queue data structure re-buffering problem occurs for
each dequeue operation. To solve this problem by joining the front
and rear ends of a queue to make the queue as circular queue.
•Circular queue is a linear data structure. It follows FIFO principle.
•In circular queue the last node is connected back to the first node to
make a circle.
•it is also called as “ring buffers”.
• Elements are added at the rear end and deleted at the front end of
the queue.
• Both the front and rear pointers points to the beginning of the
array.
Circular queue can be created in three ways:
1. Using singly linked list
2. Using doubly linked list
3. Using arrays
Circular queue using arrays
2 3 4
2.Dequeue
• A dequeue is a data structure in which elements can be inserted or
deleted from both the side(rear or front).
• However no elements can be added or deleted from middle.
• In a computer memory, dequeue is implemented using either a
circular array or circular doubly linked list.
• 2 pointers are maintained left and right.
• Since it is circular, dequeue[n-1] is followed by dequeue[0].
There are two varients of double ended queue
1. Input restricted: In this dequeue insertions can be done only at one
of the end while deletions can be done from both ends.
2. Output restricted: Deletion can be done only at one end while
insertions can done on both the ends.
• The dequeue is represented as follows:
rear front
insertion deletion
0 1 2 3
3.Priority queue
• A priority queue is a data structure in which each element is
assigned a priority.
• The priority of each element will be used to determine the order in
which element will be processed.
General rules of processing:
• An element with higher priority is processed before an element
with lower priority.
• Two elements with same priority is processed on first come first
serve basis.
• The priority of the process may be set based on the CPU time it
requires to get executed easily.
Application:
1. Priority queue are widely used in operating system to execute the
highest priority process first.
Implementation of priority queue
• Linked representation of priority queue:
Priority=1 Priority=2 Priority=3
Lower priority means higher number.
Lower priority number means higher priority.
a 1 b 2 c 3 *
4.Multiple queue
• When we implement a queue using array, size of array must be
known in advance.
• If the queue is allocated less space, then overflow conditions will
encountered. So modification will have to be done to reallocate
more space for the array.
• So a better solution to deal with this problem to have multiple
queues or to have more than one queue in the same array of
sufficient size.
• In the given figure an array queue[n] is used to represent two
queues ,queue A and queue B.
• The value of n is such that the combined size of both the queues
will never exceed n.
queue A queue B
0 1 2 3 n-3 n-2 n-1
Multiple queue
Applications Of Queues
Applications related to computer science:
• When data is transferred asynchronously between two processes. eg. IO Buffers.
• When a resource is shared among multiple consumers. Examples include CPU
scheduling, Disk Scheduling.
• In recognizing palindrome.
• In shared resources management.
• Keyboard buffer.
• Round robin scheduling.
• Job scheduling.
• Simulation
• Queues are used in playlist for jukebox to add songs to the end, play from the front
of the list.
Disadvantage of Linear Queue
In a normal Queue Data Structure, we can insert elements until queue becomes full. But once if queue becomes full, we can not
insert the next element until all the elements are deleted from the queue.
For example consider the queue below...
After inserting all the elements into the queue.
Now consider the following situation after deleting three elements
from the queue...
This situation also says that Queue is Full and we can not insert the
new element because, 'rear' is still at last position.In above
situation, even though we have empty positions in the queue we
can not make use of them to insert new element. This is the major
problem in normal queue data structure.
Probable University Questions
Explain Queue ADT
Write a C program for Array implementation of a queue
List the applications of queue
Explain the different types of queues.
Mention the disadvantages of a linear queue
Thank you

DS10-QUEUE0000000000000000000000000000000000000.pptx

  • 1.
    DATA STRUCTURES QUEUE 27/08/2020 PROF.RAMYA.R.B ,ASSISTANT PROFESSOR , COMPUTER ENGINEERING,APST THANE
  • 2.
    OBJECTIVE “ “ To learn theADT of queue and to implement queue using arrays .
  • 3.
    02 03 03 04 Algorithm for QueueOperations Array Implementation of Queue Types of Queue 04 01 05 Applications Queue ADT 05 06 Disadvantages
  • 4.
    Queue A queue isan important data structure which is extensively used in computer application. Ordered collection of homogeneous elements Non-primitive linear data structure. A new element is added at one end called rear end and the existing elements are deleted from the other end called front end. This mechanism is called First-In-First-Out (FIFO).
  • 5.
    • The elementin the queue are added at one end called “rear” and removed from another end called front. • The process of inserting element in the queue is called enqueue and the process of deleting element from the queue is dequeue. Total no of elements in queue= rear – front +1 front=0 1 rear= 2 3 2 4 6
  • 6.
    Operations performed onthe queue 1. Insertion/Enqueue: 0 1 2 3 4 5 6 7 After insertion: • Front=0 • Rear=5 12 9 8 2 6 10
  • 7.
    Algorithm for enque Step.1:if rear=max-1,then write “overflow” and exit. [ end of if loop] Step.2: if front=-1 and rear=-1 then, set front=0 and rear=0 Else rear=rear+1. [end of if loop] Step.3:set queue[rear]=num. Step.4:exit.
  • 8.
    Deletion operation/Deque • Queueafter deletion: 0 1 2 3 4 5 6 7 • Front=1 • Rear=5 9 8 2 6 10
  • 9.
    Algorithm for deletion/Deque Step:1.If front=-1 or front>rear then, Write “underflow”. else Set val=queue[front]. Set front=front+1. [end of if] Step.2 exit.
  • 11.
    Linked implementation ofqueue • In linked queue, every element has 2 parts one that stores the data and other that stores the address of next element. • The start pointer is used as first and another pointer called rear for the last element in the queue. • If front=rear=null, then it indicated that queue is empty.
  • 12.
    Operations on linkedqueue 1. Insertion 2. Deletion Linked queue is: front rear 3 4 8 null
  • 13.
    • Linked queueafter insertion: Front rear • Linked queue after deletion: Front rear 4 8 null 3 4 8 12 null
  • 14.
    Implementation of LinearQueue using array
  • 19.
    Types of queue •Circular queue • Dequeue • Priority queue • Multiple queue
  • 20.
    1.Circular queue In astandard queue data structure re-buffering problem occurs for each dequeue operation. To solve this problem by joining the front and rear ends of a queue to make the queue as circular queue. •Circular queue is a linear data structure. It follows FIFO principle. •In circular queue the last node is connected back to the first node to make a circle. •it is also called as “ring buffers”.
  • 21.
    • Elements areadded at the rear end and deleted at the front end of the queue. • Both the front and rear pointers points to the beginning of the array. Circular queue can be created in three ways: 1. Using singly linked list 2. Using doubly linked list 3. Using arrays
  • 22.
  • 23.
    2.Dequeue • A dequeueis a data structure in which elements can be inserted or deleted from both the side(rear or front). • However no elements can be added or deleted from middle. • In a computer memory, dequeue is implemented using either a circular array or circular doubly linked list. • 2 pointers are maintained left and right.
  • 24.
    • Since itis circular, dequeue[n-1] is followed by dequeue[0]. There are two varients of double ended queue 1. Input restricted: In this dequeue insertions can be done only at one of the end while deletions can be done from both ends. 2. Output restricted: Deletion can be done only at one end while insertions can done on both the ends.
  • 25.
    • The dequeueis represented as follows: rear front insertion deletion 0 1 2 3
  • 26.
    3.Priority queue • Apriority queue is a data structure in which each element is assigned a priority. • The priority of each element will be used to determine the order in which element will be processed. General rules of processing: • An element with higher priority is processed before an element with lower priority.
  • 27.
    • Two elementswith same priority is processed on first come first serve basis. • The priority of the process may be set based on the CPU time it requires to get executed easily. Application: 1. Priority queue are widely used in operating system to execute the highest priority process first.
  • 28.
    Implementation of priorityqueue • Linked representation of priority queue: Priority=1 Priority=2 Priority=3 Lower priority means higher number. Lower priority number means higher priority. a 1 b 2 c 3 *
  • 29.
    4.Multiple queue • Whenwe implement a queue using array, size of array must be known in advance. • If the queue is allocated less space, then overflow conditions will encountered. So modification will have to be done to reallocate more space for the array. • So a better solution to deal with this problem to have multiple queues or to have more than one queue in the same array of sufficient size.
  • 30.
    • In thegiven figure an array queue[n] is used to represent two queues ,queue A and queue B. • The value of n is such that the combined size of both the queues will never exceed n. queue A queue B 0 1 2 3 n-3 n-2 n-1 Multiple queue
  • 31.
  • 32.
    Applications related tocomputer science: • When data is transferred asynchronously between two processes. eg. IO Buffers. • When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. • In recognizing palindrome. • In shared resources management. • Keyboard buffer. • Round robin scheduling. • Job scheduling. • Simulation • Queues are used in playlist for jukebox to add songs to the end, play from the front of the list.
  • 33.
    Disadvantage of LinearQueue In a normal Queue Data Structure, we can insert elements until queue becomes full. But once if queue becomes full, we can not insert the next element until all the elements are deleted from the queue. For example consider the queue below... After inserting all the elements into the queue.
  • 34.
    Now consider thefollowing situation after deleting three elements from the queue... This situation also says that Queue is Full and we can not insert the new element because, 'rear' is still at last position.In above situation, even though we have empty positions in the queue we can not make use of them to insert new element. This is the major problem in normal queue data structure.
  • 35.
    Probable University Questions ExplainQueue ADT Write a C program for Array implementation of a queue List the applications of queue Explain the different types of queues. Mention the disadvantages of a linear queue
  • 36.