0% found this document useful (0 votes)
2 views26 pages

Unit 2c Queues

A queue is a data structure that operates on a First-In-First-Out (FIFO) basis, where elements are added at the rear and removed from the front. Various types of queues include normal, circular, double-ended, and priority queues, each with specific methods for insertion and deletion. The document also discusses implementations of queues using arrays and provides code examples for operations like insertion, deletion, and display.

Uploaded by

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

Unit 2c Queues

A queue is a data structure that operates on a First-In-First-Out (FIFO) basis, where elements are added at the rear and removed from the front. Various types of queues include normal, circular, double-ended, and priority queues, each with specific methods for insertion and deletion. The document also discusses implementations of queues using arrays and provides code examples for operations like insertion, deletion, and display.

Uploaded by

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

QUEUE

QUEUE
 A Queue is a data structure in which elements are
added at one end (called the rear), and elements
are removed from the other end (called the front).

 You come across a number of examples of a


queue in real life situations.

 For example, consider a line of students at a fee


counter. Whenever a student enters the queue,
he stands at the end of the queue (analogous to
the addition of nodes to the queue)
QUEUE

 Every time the student at the front of the


queue deposits the fee, he leaves the queue
(analogous to deleting nodes from a queue).

 The student who comes first in the queue is


the one who leaves the queue first.

 Therefore, a queue is commonly called a


First-In-First-Out or a FIFO data structure.
Implementation of Queue

 Queue Can be implemented using Array

Front Rear
End End

 Here Front and Rear are manipulators for


Deleting and Inserting from/into the queue
Implementation of Queue

 For Inseting
if(queue_not_full)
Rear++
Queue[Rear] = val 100 200 300 400 500
endif
Front Front
Rear Front
Rear Front
Rear Rear
Front Front
Rear Rear Front

 For Deleting
Note : After Deletion
if (queue_not_Empty)
Front++ If Front > Rear
endif Front = 0; Rear = -1;
endif
Types of QUEUE

 Normal Queue
 Circular Queue
 Double Ended Queue
 Priority Queue
printf("\t\t\t Enter the choice : ");
Ordinary Queue scanf("%d", &choice);
#include <stdio.h> switch (choice)
#include <process.h> {
#include <conio.h> case 1: // push into the queue
#define QUEUE_SIZE 5 printf("Enter the item to be inserted :
scanf("%d", &item);
insert_rear(item, q, &r);
void main()
continue;
{
case 2: // pop from the queue
void insert_rear(int, int *, int *);
delete_front(q, &f, &r);
void delete_front(int *, int *, int *); break;
void display(int *, int, int); case 3: // display queue
int choice, item, f, r, q[10]; display(q, f, r);
/* Queue is empty */ break;
f = 0; /* Front end of queue*/ case 4:
r = -1; /* Rear end of queue*/ exit(0);
for (;;) default:
{ printf("\t\t\tInvalid Input – Try Again");
clrscr(); } // end of switch
printf("\t\t\t Ordinary Queue operation\n\n"); getch();
printf("1. Push 2 Pop 3 View 4 Exit \n"); }// end of for
} // end of main
void insert_rear(int item, int q[], int *r)
{
if (qfull(*r)) /* Is queue full ? */
{
printf("\t\t\tQueue overflow\n");
return;
}
/* Queue is not full */
q[++(*r)] = item; /* Update rear pointer and insert a item */
}
void delete_front(int q[], int *f, int *r)
{
if (qempty(*f, *r))
{
printf("\t\t\tQueue underflow\n");
return;
}
printf(" Pop Successfull, element deleted = %d ",q[(*f)++]);
if(*f> *r)
{
*f=0,*r=-1;
}
}
void display(int q[], int f, int r)
{
int i;
if (qempty(f,r))
{
printf("Queue is empty\n");
return;
}
printf("\t\t\t Queue Container\n\n");
for(i=f;i<=r; i++)
printf("\t\t\t| %5d |\n",q[i]);
}
int qempty(int f, int r)
{
return (f>r)?1:0;
/* returns true if queue is empty otherwise returns false */
}
int qfull( int r)
{
return (f== QUEUE_SIZE)?1:0;
/* returns true if queue is fullotherwise returns false */
}
Circular Queue

50 40
4 3
60
5 2 30
6 1 100
70 20
7 0
80 10
90
Circular Queues
#define SIZE 5
int CQ[SIZE], f=-1, r=-1;

/* Function for Insert operation */

CQinsert(int elem) /* Function to Check Circular Queue Full */


{
if( CQfull()) int CQfull()
printf("\n\n Overflow!!!!\n\n"); {
else if( (f==r+1) || (f == 0 && r== SIZE-1))
{ return 1;
if(f==-1) return 0;
f=0; }
r=(r+1) % SIZE; /* Function: check Circular Queue Empty */
CQ[r]=elem;
} int CQempty()
{
if(f== -1)
return 1;
return 0;
}
/* Function for Delete operation */

int CQdelete()
{
int elem; /* Function to display status of Circular
if(CQempty()) Queue */
{
printf("\n\nUnderflow!!!!\n\n"); display()
return(-1); {
} int i;
else if(CQempty())
{ printf(" \n Empty Queue\n");
elem=CQ[f]; else
if(f==r) {
{ printf("Front[%d]->",f);
f=-1; r=-1; for(i=f; i !=r; i=(i+1)%SIZE)
} /* Q has only one element ? */ printf("%d ",CQ[i]);
else printf("%d ",CQ[i]);
f=(f+1) % SIZE; printf("<-[%d]Rear",r);
return(elem); }
} }
}
main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("\n ### Circular Queue Operations ### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
CQinsert(elem); break;
case 2: elem=CQdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3: printf("\n\nStatus of Circular Queue\n\n");
display(); break;
case 4: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n"); break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}
Double Ended Queue

Front Rear

1. Insert from rear


2. Delete from front
3. Insert from front
4. Delete from rear
Priority Queue

 It’s a special type of queue


 Insertion happens from rear end
 But deletion/service occurs to highest priority
element

10 5 8 2 1 20 10
2 1
8
5 20
 2 types Insertion
• Ascending P. Q.
• Descending P.Q.
Circular Queues
#include<stdio.h>
#define max 3
int q[10],front=0,rear=-1; while(1)
void main() {
{ printf("Enter your choice:");
int ch; scanf("%d",&ch);
void insert(); switch(ch)
void delet(); {
void display(); case 1: insert();
clrscr(); break;
printf("\nCircular Queue operations\n"); case 2: delet();
break;
printf("1.insert\n2.delete\n3.display\n4.exit\n"); case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");
}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element to be insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is underflow\n");
getch();
exit();
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
getch(); else
exit(); {
} for(i=front;i<=rear;i++)
if(front>rear) {
{ printf("\t%d",q[i]);
for(i=0;i<=rear;i++) }
printf("\t%d",q[i]); printf("\nrear is at %d\n",q[rear]);
for(j=front;j<=max-1;j++) printf("\nfront is at %d\n",q[front]);
printf("\t%d",q[j]); }
printf("\nrear is at %d\n",q[rear]); printf("\n");
printf("\nfront is at %d\n",q[front]); }
} getch();
Multiple Stacks
When a stack is created using single array, we can not able to
store large amount of data, thus this problem is rectified using
more than one stack in the same array of sufficient array. This
technique is called as Multiple Stack.

When an array of STACK[n] is used to represent two stacks, say


Stack A and Stack B. Then the value of n is such that the
combined size of both the Stack[A] and Stack[B] will never
exceed n. Stack[A] will grow from left to right, whereas Stack[B]
will grow in opposite direction ie) right to left.

When we need more than two stacks, we can: „


Use a fixed partition for each stack.
Use a variable partition for each stack.
When stack is full, then „
Find a larger, free space. „
Move the related stacks around.
Multiple Stacks:
There are two ways to do two stacks in array:
1.None fixed size of the stacks:
1. Stack 1 expands from the 0th element to the right
2. Stack 2 expands from the 12th element to the left
3. As long as the value of Top1 and Top2 are not next to
each other, it has free elements for input the data in
the array
4. When both Stacks are full, Top1 and Top 2 will be next
to each other
5. There is no fixed boundary between Stack 1 and
Stack 2
6. Elements –1 and –2 are using to store the information
needed to manipulate the stack (subscript for Top 1
and Top 2)
2.Fixed size of the stacks:
1. Stack 1 expands from the 0th element to the right
2. Stack 2 expands from the 6th element to the right
3. As long as the value of Top 1 is less than 6 and greater
than 0, Stack 1 has free elements to input the data in
the array
4. As long as the value of Top 2 is less than 11 and
greater than 5, Stack 2 has free elements to input the
data in the array
5. When the value of Top 1 is 5, Stack 1 is full
6. When the value of Top 2 is 10, stack 2 is full
7. Elements –1 and –2 are using to store the size of Stack
1 and the subscript of the array for Top 1 needed to
manipulate Stack 1
8. Elements –3 and –4 are using to store the size of Stack
2 and the subscript of the array for Top 2 needed to
manipulate Stack 2
Multiple Queues:
There are two ways to do two queues in array:
1.None fixed size of the queues:
• Queue 1 expands from the 0th element to the right and circular
back to the 0th element
• Queue 2 expands from the 8th element to the left and circular
back to the 8th element
• Temporary boundary between the Queue 1 and the Queue 2;
as long as there has free elements in the array and boundary
would be shift
• Free elements could be any where in the Queue such as
before the front, after the rear, and between front and rear in
the Queue
• Queue 1’s and Queue 2 ‘s size could be change if it is
necessary. When the Queue 1 is full and the Queue 2 has
free space; the Queue 1 can increase the size to use that free
space from the Queue 2. Same way for the Queue 2
•Elements –1, –2, and –3 are using to store the size of the
Queue 1, the front of the Queue 1, and the data count for the
Queue 1 needed to manipulate the Queue 1
•Elements –4, –5, and –6 are using to store the size of the
Queue 2, the front of the Queue 2, and the data count for the
Queue 2 needed to manipulate the Queue 2
•Inserts data to the Queue 1, Q1Rear = (Q1Front + Q1count) %
Q1Size
•Inserts data to the Queue 2, Q2Rear = (Q2Front + Q2count) %
Q2Size + Q1Size
•Deletes data from the Queue 1, Q1Front = (Q1Front + 1) %
Q1Size
•Deletes data from the Queue 2, Q2Front = (Q2Front + 1) %
Q2Size + Q1Size
2.Fixed size of the queue:
•Queue 1 expands from the 0th element to the 4th element and
circular back to 0th element
•Queue 2 expands from the 8th element to the 5th element and
circular back to 8th element
•The boundary is fixed between the Queue 1 and the Queue 2
•Free elements could be any where in the Queue such as
before the front, after the rear, and between front and rear in
the Queue
•Elements –1, –2, and –3 are using to store the size of the
Queue 1, the front of the Queue 1, and the data count for the
Queue 1 needed to manipulate the Queue 1
•Elements –4, –5, and –6 are using to store the size of the
Queue 2, the front of the Queue 2, and the data count for the
Queue 2 needed to manipulate the Queue 2
•Inserts data to the Queue 1, Q1Rear = (Q1Front + Q1count)
% Q1Size
•Inserts data to the Queue 2, Q2Rear = (Q2Front + Q2count)
% Q2Size + Q1Size
•Deletes data from the Queue 1, Q1Front = (Q1Front + 1) %
Q1Size
•Deletes data from the Queue 2, Q2Front = (Q2Front + 1) %
Q2Size + Q1Size

You might also like