Queue
Queue
Queue
Queue
• Queue is an abstract data structure, somewhat similar to Stack.
• In contrast to Stack, queue is opened at both end.
• One end is always used to insert data (enqueue)
• and the other is used to remove data (dequeue).
• Queue follows First-In-First-Out methodology, i.e., the data item stored first
will be accessed first.
• A real world example of queue can be a single-lane one-way road,where
the vehicle enters first, exits first.
• More real-world example can be seen as queues at ticket windows &
bus-stops.
Queue Representation
• As we now understand that in queue, we access both ends for
different reasons, a diagram given below tries to explain queue
representation as data structure:
Basic Operations
• The highest priority element comes first and the elements of the same priority are arranged based on FIFO
structure.
• For example, suppose we have some values like 1, 3, 4, 8, 14, 22 inserted in a priority queue with an ordering
imposed on the values is from least to the greatest. Therefore, the 1 number would be having the highest priority
while 22 will be having the lowest priority.
Priority Queue
• All the values are arranged in ascending order. Now, we will observe how the priority queue will look
after performing the following operations:
• poll(): This function will remove the highest priority element from the priority queue. In the above
priority queue, the '1' element has the highest priority, so it will be removed from the priority queue.
• add(2): This function will insert '2' element in a priority queue. As 2 is the smallest element among
all the numbers so it will obtain the highest priority.
• poll(): It will remove '2' element from the priority queue as it has the highest priority queue.
• add(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will obtain the
third highest priority in a priority queue.
Priority Queue
There are two types of priority queue:
• In ascending order priority queue, a lower priority number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in an ascending order like 1,2,3,4,5;
therefore, the smallest number, i.e., 1 is given as the highest priority in a priority queue.
Descending order priority queue
• In descending order priority queue, a higher priority number is given as a higher priority in a priority.
For example, we take the numbers from 1 to 5 arranged in descending order like 5, 4, 3, 2, 1;
therefore, the largest number, i.e., 5 is given as the highest priority in a priority queue.
#define SIZE 5 /* Size of Queue */ PriorityQ PQdelete()
int f=0,r=-1; /* Global declarations */ { /* Function for Delete operation */
typedef struct PRQ PriorityQ p;
{ if(Qempty()){ printf("nnUnderflow!!!!nn");
int ele; p.ele=-1;p.pr=-1;
int pr; return(p); }
}PriorityQ; else
{
PriorityQ PQ[SIZE]; p=PQ[f];
f=f+1;
PQinsert(int elem, int pre) return(p);
{ }
int i; /* Function for Insert operation */ }
if( Qfull()) printf("nn Overflow!!!!nn");
else int Qfull()
{ { /* Function to Check Queue Full */
i=r; if(r==SIZE-1) return 1;
++r; return 0;
while(PQ[i].pr >= pre && i >= 0) /* Find location for new elem */ }
{ int Qempty()
PQ[i+1]=PQ[i]; { /* Function to Check Queue Empty */
i--; } if(f > r) return 1;
PQ[i+1].ele=elem; return 0;
PQ[i+1].pr=pre; }
display()
switch(opn)
{ /* Function to display status of Queue */
{
int i;
if(Qempty()) printf(" n Empty Queuen");
case 1: printf("nnRead the element and its Priority?");
else
scanf("%d%d",&p.ele,&p.pr);
{ PQinsert(p.ele,p.pr); break;
printf("Front->"); case 2: p=PQdelete();
for(i=f;i<=r;i++) if( p.ele != -1)
printf("[%d,%d] ",PQ[i].ele,PQ[i].pr); printf("nnDeleted Element is %d n",p.ele);
printf("<-Rear"); break;
} case 3: printf("nnStatus of Queuenn");
} display(); break;
case 4: printf("nn Terminating nn"); break;
main()
default: printf("nnInvalid Option !!! Try Again !! nn");
{ /* Main Program */
break;
int opn;
}
PriorityQ p;
do
printf("nnnn Press a Key to Continue . . . ");
{
getch();
clrscr(); }while(opn != 4);
printf("n ### Priority Queue Operations(DSC order) ### nn"); }
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
Application of Priority Queue
• It is used in the Dijkstra's shortest path algorithm.
• It is also used in operating system like priority scheduling, load balancing and interrupt handling.
Double Ended Queue (Deque)
•It is also a homogeneous list of elements in which insertion and deletion operations are performed from both the ends.
•That is, we can insert elements from the rear end or from the front ends.
•There are two types of Deque. These two types are due to the restrictions put to perform either the insertions or deletions only at
one end.
•There are:
• Input-restricted Deque.
• Output-restricted Deque.
Double Ended Queue (Deque)
• Below show a figure a empty Deque Q[5] which can accommodate five elements.
•There are:
• Input-restricted Deque: An input restricted Deque restricts the insertion of the elements at
one end only, the deletion of elements can be done at both the end of a queue.
Double Ended Queue (Deque)
• Output-restricted Deque: on the contrary, an Output-restricted Deque,
restricts the deletion of elements at one end only, and allows insertion
to be done at both the ends of a Deque.
Double Ended Queue (Deque)
• Operations on Deque: