Queues Jsn
Queues Jsn
Queues
• Queue: a collection whose elements are
added at one end (the rear or tail of the
queue) and removed from the other end
(the front or head of the queue)
• A queue is a FIFO (first in, first out) data
structure
• Any waiting line is a queue:
• The check-out line at a grocery store
• The cars at a stop light
• An assembly line
Conceptual View of a Queue
Adding an element
Front of queue
New element is
added to the rear
of the queue
Conceptual View of a Queue
Removing an element
Element is
removed from the
front of the queue
Uses of Queues in Computing
BBB 2
Front Rear
AAA 1
Rear Front
Algorithms for Insert and Delete Operations in Linear Queue
For Insert Operation
Insert-Queue(QUEUE, REAR, FRONT,MAX, NUM)
Here, QUEUE is the place where to store data. REAR represents the location in which the
data element is to be inserted and FRONT represents the location from which the data
element is to be removed. Here MAX is the maximum size of the QUEUE and finally,
NUM is the new item to be added.
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
Drawback of Linear Queue
• Once the queue is full, even though few elements from the front are deleted and
some occupied space is relieved, it is not possible to add anymore new elements,
as the rear has already reached the Queue’s rear most position.
Circular Queue
• This queue is not linear but circular.
"Rear" most element, if and only if the "Front" Figure: Circular Queue having
Rear = 5 and Front = 0
has moved forward. otherwise it will again be
Rear
Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front
Rear
7. Insert 100, Rear = 5, Front = 2. 10. Delete front, Rear = 1, Front = 3.
Front Rear
Front
Rear
Front
Rear
Front
Front
• while inserting an element
rear=(rear+1) mod (size)
• while deleting an element
front=(front+1) mod (size)
• Overflow?
front=(rear+1) mod (size)
• Empty?
front=rear
Double Ended Queue
• A deque (pronounced as ‘deck’ or ‘dequeue’) is a list
in which the elements can be inserted or deleted at
either end.
• It is also known as a head-tail linked list because
elements can be added to or removed from either the
front (head) or the back (tail) end.
• In a deque, two pointers are maintained, LEFT and
RIGHT, which point to either end of the deque.
• It is a circular
• The elements in a deque extend from the LEFT end
to the RIGHT end and since it is circular, Dequeue[N–
1] is followed by Dequeue[0].
Variants of Deque
• Input restricted deque: In this dequeue,
insertions can be done only at one of
the ends, while deletions can be done
from both ends.
• Output restricted deque :In this
dequeue, deletions can be done only at
one of the ends, while insertions can be
done on both ends.
DEQUEUE
INPUT RESTRICTED DEQUEUE
OUTPUT RESTRICTED DEQUEUE
void insert_left()
{
int val;
printf("\n Enter the value to be added:");
scanf("%d", &val);
if((left == 0 && right == MAX–1) || (left == right+1))
{
printf("\n Overflow");
return;
}
if (left == –1)/*If queue is initially empty*/
{
left = 0;
right = 0;
}
else
{
if(left == 0)
left=MAX–1;
else
left=left–1;
}
deque[left] = val;
}
void display()
{
int front = left, rear = right;
if(front == –1)
{
printf("\n QUEUE IS EMPTY"); return;
}
printf("\n The elements of the queue are : ") if(front <= rear )
{
while(front <= rear)
{
printf("%d",deque[front]); front++;
}
}
else
{
while(front <= MAX–1)
{
printf("%d", deque[front]); front++;
}
front = 0;
while(front <= rear)
{
printf("%d",deque[front]);front++;
}
}
printf("\n");
}
Priority Queue
• A priority queue is a data structure in which each
element is assigned a priority. The priority of the
element will be used to determine the order in which
the elements will be processed.
• The general rules of processing the elements of a
priority queue are
• An element with higher priority is processed before an
element with a lower priority.
• Two elements with the same priority are processed on a
first-come-first-served (FCFS) basis.
Array Representation of a Priority
Queue
• When arrays are used to implement a priority queue,
then a separate queue for each priority number is
maintained.
• Each of these queues will be implemented using
circular arrays or circular queues.
• A two-dimensional array for this purpose where each
queue will be allocated the same amount of space.
• Insertion: Insertion To insert a new
element with priority K in the priority
queue, add the element at the rear end
of row K, where K is the row number as
well as the priority number of that
element.
• Deletion: To delete an element, we find
the first nonempty queue and then
process the front element of the first
non-empty queue