Queues
DATA STRUCTURES
• A data structures is a logical model of a
particular organization of data.
• It is basically a group of data elements that
are put together under one name and which
defines a particular way of storing and
organizing data in a computer so that it can be
used efficiently.
Classifications of data structures
• Primitive: This is the type of data structures
which are supported by programming
language. Example-int, real, character,
boolean.
• Non primitive: This is the type of data
structures in which are created using primitive
data structures. Example-linked lists, stacks,
trees, graphs.
classification
Defination of queue
• A queue is an important data structure which
is extensively used in computer application.
• A queue is a data structure that models the
first-come first-serve order.
• That is, the element that is inserted first into
the queue will be the element that will
deleted first, and the element that is inserted
last is deleted last.
• A waiting line is a good real-life example of a
queue.
• 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.
front=0 1 rear= 2 3
2 4 6
Implementation of queues
Queues can be implemented either as an array
or an linked list.
•Array representation of queue:
0 1 3 4 5 6 7 8
12
Front
=0
9 8 2 6
Rear=
5
Operations performed on the queue
1. Insertion:
0 1 2 3 4 5 6 7
After insertion:
• Front=0
• Rear=5
12 9 8 2 6 10
Algorithm for insertion
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
• Queue after deletion:
0 1 2 3 4 5 6 7
• Front=1
• Rear=5
9 8 2 6 10
Algorithm for deletion
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 representation 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
3 4 8 12 null
4 8 null
Types of queue
• Circular queue
• Dequeue
• Priority queue
• Multiple queue
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
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:
• front rear
insertion deletion
0 1 2 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 procesing:
• 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 operationg
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 *
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
• Direct applications
–Waiting lists, bureaucracy
–Access to shared resources (e.g., printer)
–Multiprogramming
–Queues are used in operating system for
handling interrupts.
–queues are used in playlist for jukebox to
add songs to the end, play from the front of
the list.

Queues

  • 1.
  • 2.
    DATA STRUCTURES • Adata structures is a logical model of a particular organization of data. • It is basically a group of data elements that are put together under one name and which defines a particular way of storing and organizing data in a computer so that it can be used efficiently.
  • 3.
    Classifications of datastructures • Primitive: This is the type of data structures which are supported by programming language. Example-int, real, character, boolean. • Non primitive: This is the type of data structures in which are created using primitive data structures. Example-linked lists, stacks, trees, graphs.
  • 4.
  • 5.
    Defination of queue •A queue is an important data structure which is extensively used in computer application. • A queue is a data structure that models the first-come first-serve order. • That is, the element that is inserted first into the queue will be the element that will deleted first, and the element that is inserted last is deleted last. • A waiting line is a good real-life example of a queue.
  • 6.
    • 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. front=0 1 rear= 2 3 2 4 6
  • 7.
    Implementation of queues Queuescan be implemented either as an array or an linked list. •Array representation of queue: 0 1 3 4 5 6 7 8 12 Front =0 9 8 2 6 Rear= 5
  • 8.
    Operations performed onthe queue 1. Insertion: 0 1 2 3 4 5 6 7 After insertion: • Front=0 • Rear=5 12 9 8 2 6 10
  • 9.
    Algorithm for insertion 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.
  • 10.
    Deletion operation • Queueafter deletion: 0 1 2 3 4 5 6 7 • Front=1 • Rear=5 9 8 2 6 10
  • 11.
    Algorithm for deletion 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.
  • 12.
    Linked representation 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.
  • 13.
    Operations on linkedqueue 1. Insertion 2. Deletion Linked queue is: front rear 3 4 8 null
  • 14.
    • Linked queueafter insertion: Front rear • Linked queue after deletion: Front rear 3 4 8 12 null 4 8 null
  • 15.
    Types of queue •Circular queue • Dequeue • Priority queue • Multiple queue
  • 16.
    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”.
  • 17.
    • 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
  • 18.
  • 19.
    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.
  • 20.
    • 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.
  • 21.
    • The dequeueis represented as follows: • front rear insertion deletion 0 1 2 3
  • 22.
    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 procesing: • an element with higher priority is processed before an element with lower priority.
  • 23.
    • 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 operationg system to execute the highest priority process first.
  • 24.
    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 *
  • 25.
    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.
  • 26.
    • 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
  • 27.
    Applications of Queues •Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g., printer) –Multiprogramming –Queues are used in operating system for handling interrupts. –queues are used in playlist for jukebox to add songs to the end, play from the front of the list.