0% found this document useful (0 votes)
58 views

Unit 3 Queues

33uuuuuuuuii

Uploaded by

Saradhi Pkm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Unit 3 Queues

33uuuuuuuuii

Uploaded by

Saradhi Pkm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit-III Queues

Introduction:
A queue is a data structure used for storing data. In queue, the order in which the data arrives is
important. In general, a queue is a line of people or things waiting to be served in sequential order
starting at the beginning of the line or sequence.

Definition : A queue is an ordered list in which insertions are done at one end (rear) and deletions are
done at other end (front). The first element to be inserted is the first one to be deleted. Hence, it is
called as First In First Out(FIFO) list.
When an element is inserted in a queue, the concept is called as EnQueue, and when an element
is removed from the queue, the concept is called as DeQueue. Trying to DeQueue an empty queue is
called as underflow and trying to EnQueue an element in a full queue is called as overflow. As an
example, consider the snapshot of the
10 20 3 40 50 60 70
0

front rear
Queue ADT:
The definition of an abstract data type clearly states that for a data structure to be abstract, it
should have the following two characteristics
1. There should be a particular way in which components are related to each other and
2. A statement of the operations that can be performed on elements of the abstract data type
should be specified.
Thus, a queue, as an abstract data type, can be defined as follows:
A queue of elements of type A is a finite sequence of elements of A together with the following
operations :
1. Initialize a queue to be empty.
2. Check if a queue is empty or not
3. Check if a queue is full or not.
4. Insert a new element after the last element in a queue, if it is not full.
5. Retrieve the first element of a queue, if it is not empty.
6. Delete the first element in a queue, if it is not empty.

Applications of Queue:
1. Queue of processes waiting to be processed
2. Operating systems schedule jobs (with equal priority) in the order of arrival(e.g., a print queue).
3. Simulation of real-world queues such as lines at a ticket counter or any other first-come first-
served scenario requires a queue.
4. Multiprogramming.
5. Asynchronous data transfer.
6. Waiting times of customers at call center.
7. Determining number of cashiers to have at a supermarket.
8. Round-robin scheduling : Iterate through a set of processes in a circular manner and service
each element.
Limitations of Queues:
1. The main disadvantages is that queue is not readily searchable and adding or removing elements
from the middle of the queue is very complex.
2. It is not meant to allow elements to be removed from the middle.
3. A queue is simply like a line. first in line, first to get service

Applications of Circular Queue:

1. Traffic light functioning is the best example for circular Queues. The colours in the traffic light
follow a circular pattern.
2. In page replacement algorithms, a circular list of pages is maintained and when a page needs to
be replaced, the page in the front of the queue will be chosen. 6. Define Dequeue ?

Dequeue :
1. A dequeue (double ended queue) is a linear list in which insertion and deletion are performed
from the either end of the structure.
2. There are two variations of Dqueue
i. Input restricted dqueue-allows insertion at only one end
ii. Output restricted dqueue-allows deletion from only one end
3. Such a structure can be represented by following fig.

Priority Queue :

A priority queue is a collection of elements such that each element has been assigned a priority and
such that the order in which elements are deleted and processed comes from the following rules :
1. An element of higher priority is processed before any element of lower priority.
2. Two elements with same priority are processed according to the order in which they were added
to the queue.
An example of priority queue can be a time sharing system that is programs of high priority are
processed first and programs with the same priority form a standard queue
Each node in the list contains three items of information – an information field(INFO), a priority
number(PRNO) and the link (NEXT)
Node A will precede Node B in the list when A has higher priority than B or when both the nodes
have same priority but A was added to the list before B. This means that the order in one-way list
corresponds to the order of priority queue
Operations (or) Implementation of Queue using Arrays:
ARRAY REPRESENATION OF QUEUES :
Queues can be easily represented using linear arrays. As stated earlier, every queue has front
and rear variables that point to the position from where deletions and insertions can be done
respectively. Figure below shows the representation of queue as an

Q[0] Q[1 Q[2] Q[3] Q[4]


]
10 20 30 40 50

Basic Operations on a Queue : Front Rear The following operations that


are usually performed on a queue.

1. Insert or enqueue
2. Delete or dequeue
3. Peek
1. Insert or enqueue operation :
Queues maintain two data variables front and rear. Therefore, its operators are comparatively
difficult to implement than that of stacks.
Before inserting an element in the queue, we must check for overflow conditions. An overflow
will occur when we try to insert an element into a queue that is already full. When Rear MAX - 1,
where MAX is the size of the queue, we have an overflow condition. Note that we have written MAX -
1, because the index starts from 0.

Algorithm to insert an element in the Queue :

Step 1: IF REAR = MAX-1, then;


write OVERFLOW
[END OF IF]
Step 2 : IF FRONT = -1 and REAR = -1, then;
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE [REAR] = NUM
Step 4 : Exit

Example : Consider a queue shown in figure

fron rear
t
12 9 7 18 14 36
0 1 2 3 4 5 6 7 8 9
Queue
Here, front =0 and rear =5. Suppose we want to add another element with the value 45, then the
rear would be incremented by 1 and the value would be stored at the position pointed by the rear. The
queue after addition would be as shown in figure.

fron rear
t
12 9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Queue after insertion of a new element
Here,front 0 and rear = 6. Every time a new element has to be added, we repeat the same
procedure.

2. Delete or Dequeue operation :


Similarly, before deleting an element from the queue, we must check for underflow conditions.
An underflow condition occurs when we try to delete an element from a queue that is already empty. If
front = -1 and rear = -1, it means there is no element in the queue.

Algorithm to delete an element from the Queue :

Step 1 :IF FRONT = -1, then;


Write UNDERFLOW
ELSE
SET FRONT = FRONT +1
SET VAL = QUEUE [FRONT]
[END OF IF]
Step 2: Exit

Example : If we want to delete an element from the queue, then the value of the front will be
incremented. Deletions are done from only this end of the queue after deletion will be as shown in
figure.
front rear

9 7 18 14 36 45
0 1 2 3 4 5 6 7 8 9
Queue after deletion of an element
Here, front =1 and rear = 6
3. Peek Operation :
Peek is an operation that return the data at the front of the queue. The algorithm for peek
operation :

Step 1 : IF FRONT = -1 then;


Write UNDERFLOW
Goto Step 3
Step 2 : RETURN QUEUE [FRONT]
Step3: END.

Example Program on Queue operations using Array :


#include<stdio.h>
#include<conio.h>
#define MAX 5;
int Queue [MAX];
int front=-1;
int rear=-1;
void insertion();
void deletion();
void display();
void main()
{
int ch;
clrscr();
printf("Queue operations");
printf ("\n1. Insertion\n2. Deletion\n3. Display\n4. Exit");
do
{
printf("\nenter your choice:");
scanf ("&d", &ch);
switch(ch)
{
case 1:
insertion (); break;
case 2:
deletion (); break;
case 3:
display ( ); break;
}
}while (ch !=4);
}
void insertion ()
{
int k;
if (rear-MAX-1)
printf ("Queue is Overflow !!");
else
{
printf ("Enter element for insertion:");
scanf ("&d", &k);
if (front==-1)
front=0;
queue [++rear]=k;
}
}
void deletion()
{
if (front==-1)
printf ("Queue is underflow!!\n");
else if (front==rear)
{
printf ("The deleted element is %d\n", queue (front]);
front=rear=-1;
}
else
{
printf ("The deleted element is %d\n", queue (front]);
front++;
}
}
void display
{
inti;
if (front==-1)
printf("Queue is Empty!!\n");
else
{
printf("Elements in Queue are\n");
for (i=front; i<=rear; i++)
printf("%5d", queue [i]);
}
}
Output :
Queue operations
1. Insertion
2. Deletion
3. Display
4. Exit
enter your choice : 1
Enter element for insertion : 11
enter your choice : 1
Enter element for insertion : 22
enter your choice : 1
Enter element for insertion : 33
enter your choice : 3
Elements in Queue are
11 22 33
enter your choice : 2
The deleted element is 11
enter your choice : 4

Queue operations using Linked list:


In a linked queue, every elements has two parts, one that stores the data and another that stores
the address of the next element.
Operations on a queue :
A queue has two basic operations : insert and delete. The insert operation adds an element to the
end of the queue and the delete operation removes the element from the front or the start of the queue.
Apart from this, there is another operation peek which returns the value of the first element of
the queue.
1. Insert operation :The insert operation is used to insert an element into the queue. The new element
is added as the last element of the queue. Consider the linked queue shown in figure.

To insert an element with the value 50, we first check if FRONT=NULL. If the condition holds,
then the queue is empty. So, we allocate memory for a new node, store the value in its data part and
NULL in its next part. If FRONT !=NULL, then we will insert the new node at the end of the linked
queue. Thus, the updated queue becomes as shown in figure.

Linked queue after inserting a new node

Algorithm to insert an element in a linked queue


Step 1 : Allocate memory for the new node and name it as PTR
Step 2 : SET PTR ->DATA = VAL
Step 3: IF FRONT = NULL, then
SET FRONT = REAR PTR;
ELSE
SET REAR->NEXT = PTR
SET REAR = PTR
[END OF IF]
Step 4: END

Delete operation :
The delete operation is used to delete the element that was first inserted in a queue. That is, the
delete operation deletes the element whose address is stored in the FRONT. However before deleting
the value, we must first check it FRONT=NULL, If the condition is true, then the queue is empty and
no more deletions can be done. If an attempt is made to delete a value from a queue that is already
empty, an UNDERFLOW message is printed.
If the condition is false, then we delete the first node pointed by FRONT. The FRONT will now
refers to the second element of the linked queue. Thus, the updated queue becomes as shown in figure.

Algorithm to delete an element from linked Queue:


Step 1 : IF FRONT = NULL, then
write "Underflow"
Go to Step 3
[END OF IF]
Step 2 : SET PTR = FRONT
Step 3 : FRONT = FRONT ->NEXT
Step 4: END

Circular Queue using Arrays:


A circular queue is similar to a linear queue as it is also based on the FIFO (First In First Out)
principle except that the last position is connected to the first position in a circular queue that forms a
circle. It is also known as a Ring Buffer.

Circular queues are the queues implemented in circular form rather than in a straight line.
Circular queues overcome the problem of unutilised space in linear queue implemented as an array. In
the array implementation there is a possibility that the queue is reported full even through slots of the
queue are empty (since rear has reached the end of array).
Suppose an array Q of n elements is used to implement a circular queue. If we go on adding
elements to the queue we may reach Q[n-1]. We cannot add any more elements to the queue since the
end of the array has been reached.
Instead of reporting the queue is full. If some elements in the queue have been deleted then
there might be empty slots at the beginning of the queue. In such case these slots would be filled by
new elements added to the queue. A circular queue is implemented in the same manner as a linear
queue is implemented. The only difference will be in the code that performs insertion and deletion
operations.

Insertion Operation :
For insertion, we now have to check for the following three conditions:
1.If rear !=MAX-1, then the value will be inserted and rear will be incremented illustrated in figure.

2.If front = 0 and rear = MAX-1, then print that the circular queue is full. Look at the queue given
in figure. Which illustrates this point.

3. If front !=0 and rear = MAX-1, then it means that the queue is not full. So, set 0 and insert the new
element there, as shown in figure

Algorithm to insert an element in a circular queue :

Step 1 : IF FRONT = 0 and Rear = MAX - 1, then write "OVERFLOW"


ELSE IF FRONT = -1 and REAR = -1, then;
SET FRONT = REAR = 0
ELSE IF REAR = MAX -1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 2 : SET QUEUE [REAR] = VAL
Step 3: Exit

2. Deletion Operation :
To delete an element again we check for three conditions.
i. Look at figure. If front = -1, then there are no elements in the queue. So, an underflow condition will
be reported.

0 1 2 3 4 5 6 7 8 9
Front=rear=-1
Empty Queue
ii. If the queue is not empty and after returning the value on the front, front=rear, then the queue has
now become empty and so, front and rear are set to -1. This is illustrated in figure.

81
0 1 2 3 4 5 6 7 8 9
Front=rear=9
Delete last element and set rear=front=-1
iii.If the queue is not empty and after returning the value on the front, front=MAX-1, then front is set
to 0. This is shown in figure.

7 63 9 1 27 39 81
2 8
0 1 2 3 4 Rear=5 6 7 8 Front=9

Delete this element and set rear=front=-1

Queue where front = MAX -1 before deletion

Algorithm to delete an element from circular queue


Step 1 : IF FRONT = -1, then write "Underflow"
[End of IF]
Step 2 : SET VAL = QUEUE (FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[End of IF]
[END OF IF]
Step 4: Exit

Program:
#include <stdio.h>
# define max 6
int queue[max]; // array declaration
int front=-1;
int rear=-1;
// function to insert an element in a circular queue
void enqueue(int element)
{
if(front==-1 && rear==-1) // condition to check queue is empty
{
front=0;
rear=0;
queue[rear]=element;
}
else if((rear+1)%max==front) // condition to check queue is full
{
printf("Queue is overflow..");
}
else
{
rear=(rear+1)%max; // rear is incremented
queue[rear]=element; // assigning a value to the queue at the rear position.
}
}

// function to delete the element from the queue


int dequeue()
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
// function to display the elements of a queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x; // variables declaration

while(choice<4 && choice!=0) // while loop


{
printf("\n Press 1: Insert an element");
printf("\nPress 2: Delete an element");
printf("\nPress 3: Display the element");
printf("\nEnter your choice");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter the element which is to be inserted");
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}

Deque (Or) DOUBLE ENDED QUEUES:


The dequeue stands for Double Ended Queue. In the queue, the insertion takes place from one
end while the deletion takes place from another end. The end at which the insertion occurs is known as
the rear end whereas the end at which the deletion occurs is known as front end.
Deque is a linear data structure in which the insertion and deletion operations are performed
from both ends. We can say that deque is a generalized version of the queue.

Properties of deque:
Deque can be used both as stack and queue as it allows the insertion and deletion operations on
both ends.
In deque, the insertion and deletion operation can be performed from one side. The stack
follows the LIFO rule in which both the insertion and deletion can be performed only from one end;
therefore, we conclude that deque can be considered as a stack.

In deque, the insertion can be performed on one end, and the deletion can be done on another
end. The queue follows the FIFO rule in which the element is inserted on one end and deleted from
another end. Therefore, we conclude that the deque can also be considered as the queue.

There are two types of Queues, Input-restricted queue, and output-restricted queue.

Input-restricted queue: The input-restricted queue means that some restrictions are applied to the
insertion. In input-restricted queue, the insertion is applied to one end while the deletion is applied
from both the ends.

Output-restricted queue: The output-restricted queue means that some restrictions are applied to the
deletion operation. In an output-restricted queue, the deletion can be applied only from one end,
whereas the insertion is possible from both ends.

Operations on Deque:
The following are the operations applied on deque:
1. Insert at front
2. Delete from end
3. insert at rear
4. delete from rear
Other than insertion and deletion, we can also perform peek operation in deque. Through peek
operation, we can get the front and the rear element of the dequeue. We can perform two more
operations on dequeue
isFull(): This function returns a true value if the stack is full; otherwise, it returns a false value.
isEmpty(): This function returns a true value if the stack is empty; otherwise it returns a false value.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy