0% found this document useful (0 votes)
4 views15 pages

Module - 2 QUEUE

The document covers the concept of queues, including definitions, types (ordinary, circular, double-ended, and priority queues), and their operations such as insertion, deletion, and display using C programming examples. It explains the FIFO principle in ordinary queues, the circular structure of circular queues, and the priority-based removal in priority queues. Additionally, it discusses double-ended queues (deques) that allow insertion and deletion from both ends.

Uploaded by

ADITHYA KENI
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)
4 views15 pages

Module - 2 QUEUE

The document covers the concept of queues, including definitions, types (ordinary, circular, double-ended, and priority queues), and their operations such as insertion, deletion, and display using C programming examples. It explains the FIFO principle in ordinary queues, the circular structure of circular queues, and the priority-based removal in priority queues. Additionally, it discusses double-ended queues (deques) that allow insertion and deletion from both ends.

Uploaded by

ADITHYA KENI
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/ 15

DAYANANDA SAGAR COLLEGE OF ENGINEERING

(An Autonomous Institute Affiliated to VTU, Belagavi)

Unit – II

QUEUE
Syllabus:
Queues: Definition, Operations, Implementation using Arrays.
Types of queues: Circular queue, Deque and priority queue.
Applications of queues: Message queue using circular queue

Queue is a linear non primitive data structure in which insertion are made
through one end and deletion are made through other end, according to First In
First Out (FIFO) principles.

Types of queue
1. Ordinary queue
2. Circular queue
3. Double ended queue
4. Priority queue

Ordinary queue or linear queue

In ordinary queue the insertion of elements through rear end and deletion of
elements from the front end.
1. Initial assume that rear =-1 and front =0 indicates the queue is empty

-1 0 1 2 3

Rear Front
Figure1 represents the queue is empty

2. To insert an element into a queue increment the value of rear by 1 in that


index store the element.
-1 0 1 2 3
10

Rear/Front

3. To insert an element into a queue increment the value of rear by 1 in that


index store the element.
-1 0 1 2 3
10 20

Front Rear
4. We can insert maximum 4 elements in the queueand then the queue is full.
0 1 2 3
10 20 30 40

Front Rear

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 1


Bangalore
Function to insert an element into a linear queue.

int qinsert( )
{
if(rear == size-1)
Printf(“Queue is Full”); else
{
rear = rear +1 ;
q[rear]= item;
}
}

In ordinary queue the deletion of elements through front end


.
1. Initial assume that rear =-1 and front =0 indicates the queue is empty,
whenever front value is greater than rear the queue is empty.

-1 0 1 2 3

Rear Front

Figure1 represents the queue is empty

2. To delete an element from a queue increment the value of front by 1.


-1 0 1 2 3
10

Rear/Front

3. To delete an element from a queue increment the value of front by 1, there


we can delete elementsfrom a queue until front value is larger than rear.
0 1 2 3
10 20 30 40

Front Rear

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 2


Bangalore
Function to delete an element from a linear queue.

int qdelete( )
{
if(front > rear )
printf (“Queue is Empty”);
else
front = front +1 ;
}

Function to display the element of a linear queue.

int qdisplay( )
{
if(front > rear ) printf(“Queue
is Empty”); else
{
printf(“The status of Queue \n”);
for(i= front ; i<=rear ;i++)
printf(“%d”, q[i]);
}
}

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 3


Bangalore
Write a C program to perform the operation of queue such as insert, delete &
display the status of queue.

#include<stdio.h>
#define SIZE 5
int q[SIZE], rear= -1,front =0, item ,ch;
void qinsert(int );
void qdelete();
void display ();
void main ( )
{
clrscr ( );
while (1)
{
printf (“\n 1: INSERT2:DELETE 3:DISPLAY Otherwise EXIT\n”);
scanf (“%d”, &ch);
switch (ch)
{
case 1: printf (“Enter the element \n”);
scanf (“%d”, &item);
qinsert(item);
break;
case 2: qdelete ( ); break ;
case 3: qdisplay ( ); break default: printf
(“Invalid operation”);
getch ( );
exit (0);
}
}// end of while
} // end of main program

int qinsert( int item)


{
if(rear == SIZE-1)
Printf(“Queue is Full”);
else
{
rear = rear +1;
q[rear]= item;
}
}
int qdelete( )
{
if(front >rear )
printf (“Queue is Empty”);
else
{
Printf(“The deleted element is %d”, q[front]);
front = front +1;
}
}
Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 4
Bangalore
int qdisplay( )
{
if(front > rear )
printf (“Queue is Empty”);
else
{
printf (“The status of Queue \n”);
for(i= front ; i<=rear ;i++)
printf (“Queue[%d]=%d\n”,i, q[i]);
}
}

Circular Queue

What is a Circular Queue?

A Circular Queue is a special version of queue where the last element of the
queue is connected to the first element of the queue forming a circle.
The operations are performed based on FIFO (First In First Out) principle. It is
also called ‘Ring Buffer’.

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 5


Bangalore
Write a C program to implement circular queue.
#include<stdio.h>
#define MAX 5
int q[MAX],i,ch,item,f=0,r=-1,c=0;
void cqinsert(int);
void cqdelete( );
void cqdisplay( );
void main()
{
clrscr();
while(1)
{
printf("\n1:Insert 2:Delete 3:Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the number \n");
scanf("%d",&item);
cqinsert(item); break;
case 2: cqdelete( ); break;
case 3: cqdisplay( );break;
default: printf("Invalid choice \n");
getch();
} exit(0);
}// end of while
} // end of main program
void cqinsert(int item)
{
if(c==MAX)
printf("Queue is full");
else
{
c=c+1;
r=(r+1)%MAX;
q[r]=item;
}
}

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 6


Bangalore
int cqdelete( )
{
if(c==0)
printf("Queue is Empty");
else
{
c=c-1;
printf("The deleted element is %d",q[f]);
f=(f+1)%MAX;
}
return 0;
}

void cqdisplay( )
{
int i,X;
if(c==0)
printf("Queue is Emplty");
else
{
X=f;
printf("The status of queue\n");
for(i=1;i<=c;i++)
{
printf("%d ",q[X]);
X=(X+1)%MAX;
}
}
}
Priority Queue

A priority queue is a special type of queue. Each queue’s item has an additional piece of
information, namely priority. Unlike a regular queue, the values in the priority queue are
removed based on priority instead of the first-in-first-out (FIFO) rule.

Here, A,B, etc. denotes the value of items while 1,2 etc. denotes the priority of
items. So the item with the highest priority in this example is C (with the priority
of 1) that is removed first. And the lowest priority item, Q (with the priority of
19), will be removed at the end of the process.

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 7


Bangalore
Types of Priority Queue
There are two types of priority queues:
 Min-priority queue: in a min-priority queue, a lower priority number is
given as a higher priority
 Max-priority queue: in a max-priority queue, a higher priority number is
given as a higher priority

C CODE FOR IMPLEMENTING MAX PRIORITY


#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
int pq[SIZE],f=0,r=-1,item;
void pqinsert()
{ int n,i,j,temp;
if(r==SIZE-1)
printf("Queue is Full\n");
else
{
printf("Enter the item\n");
scanf("%d",&item);
pq[++r]=item;
n=r-f+1;
for(i=0;i<n-1;i++)
{
for(j=f;j<r;j++)
{
if(pq[j]<pq[j+1])
{
temp=pq[j];
pq[j]=pq[j+1];
pq[j+1]=temp;
}
}
}
}
Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 8
Bangalore
}
void pqdel()
{
if(f>r)
printf("queue is empty\n");
else
printf("item deleted=%d\n",pq[f++]);
}
void display()
{ int i;
if(f>r)
printf("queue is empty\n");
else
{
printf("Contents of queue\n");
for(i=f;i<=r;i++)
printf("%d\t",pq[i]);
}
}

void main()
{
int ch;
clrscr();
for(;;)
{
printf("1. insert\n 2. delete\n 3. display\n 4. exit\n");
printf("enter your ch\n");
scanf("%d",&ch);
switch(ch)
{
case 1:pqinsert(); break;
case 2: pqdel();break;
case 3: display(); break;
default: exit(0);break;
//getch();
}
}
}

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page 9


Bangalore
Double Ended Queue

Deque or Double Ended Queue is a type of queue in which insertion and removal of
elements can either be performed from the front or the rear. Thus, it does not follow FIFO
rule (First In First Out).

Operations on a Deque
Below is the circular array implementation of deque. In a circular array, if the array is full, we start
from the beginning.

But in a linear array implementation, if the array is full, no more elements can be inserted. In each of
the operations below, if the array is full, "overflow message" is thrown.

Before performing the following operations, these steps are followed.

Insertion at the front end

In this operation, the element is inserted from the front end of the queue. Before
implementing the operation, we first have to check whether the queue is full or not. If the
queue is not full, then the element can be inserted from the front end by using the below
conditions -

o If the queue is empty, both rear and front are initialized with 0. Now, both will
point to the first element.
o Otherwise, check the position of the front if the front is less than 1 (front < 1), then
reinitialize it by front = n - 1, i.e., the last index of the array.

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page


Bangalore 10
Insertion at the rear end

In this operation, the element is inserted from the rear end of the queue. Before
implementing the operation, we first have to check again whether the queue is full or not.
If the queue is not full, then the element can be inserted from the rear end by using the
below conditions -

o If the queue is empty, both rear and front are initialized with 0. Now, both will
point to the first element.
o Otherwise, increment the rear by 1. If the rear is at last index (or size - 1), then
instead of increasing it by 1, we have to make it equal to 0.

Deletion at the front end

In this operation, the element is deleted from the front end of the queue. Before
implementing the operation, we first have to check whether the queue is empty or not.

If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform
the deletion. If the queue is not full, then the element can be inserted from the front end
by using the below conditions

If the deque has only one element, set rear = -1 and front = -1.

Else if front is at end (that means front = size - 1), set front = 0.

Else increment the front by 1, (i.e., front = front + 1).

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page


Bangalore 11
Deletion at the rear end

In this operation, the element is deleted from the rear end of the queue. Before
implementing the operation, we first have to check whether the queue is empty or not.

If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform
the deletion.

If the deque has only one element, set rear = -1 and front = -1.

If rear = 0 (rear is at front), then set rear = n - 1.

Applications of deque
1. Deque can be used as both stack and queue, as it supports both operations.
2. Deque can be used as a palindrome checker means that if we read the string from both
ends, the string would be the same.

Video Link:
https://www.youtube.com/watch?v=kLBuJ1Hle8g&t=87s

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page


Bangalore 12
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#define SIZE 10
int q[10], f = -1, r = -1, i;

void f_insert()
{
if(f == 0)
printf("Space not available.\n");
else if(f > 0)
{
printf("Enter the element to be inserted: ");
scanf("%d",&q[--f]);
}
Else
{
printf("Enter the element to be inserted: ");
scanf("%d",&q[++f]);
++r;
}
}

void r_insert()
{
if(r == SIZE -1)
printf("Space not available.\n");
else
{
printf("Enter the element to be inserted: ");
scanf("%d",&q[++r]);
}
if(f == -1)
f++;
}

void f_delete()
{
if(f == -1)
printf("Queue empty\n");
else if(f == r)
{
printf("The deleted element is: %d.\n",q[f]);
f = -1;
r = -1;
}
else
{
printf("The deleted element is: %d.\n",q[f]);
f++;
}

void r_delete()
{
if(r == -1)
printf("Queue is empty.\n");

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page


Bangalore 13
else if(f == r)
{
printf("The deleted element is: %d",q[r]);
f = -1;
r = -1;
}
else
{
printf("The deleted element is: %d",q[r]);
r--;
}

void display()
{
if(r == -1)
printf("Queue is empty\n");
else
{
printf("Elements of the queue are: \n");
for(i = f; i <= r; i++)
printf("%d\t",q[i]);
}
}

void main(){
char ch;
system("CLS");
while(1){
printf("Double Ended Queue Operations: \n");
printf("1. Insert Elements to the front end.\n");
printf("2. Delete Elements from the front end.\n");
printf("3. Insert Elements to the rear end.\n");
printf("4. Delete Elements from the rear end.\n");
printf("5. Display the Elements of the Queue.\n");
printf("6. Exit.\n");
ch = getchar();
switch(ch)
{
case '1': f_insert();
break;
case '2': f_delete();
break;
case '3': r_insert();
break;
case '4': r_delete();
break;
case '5': display();
break;
case '6': exit(0);
break;
default: printf("Wrong input. Please try again.\n");
}
printf("\nPress any key to continue...\n");
getch();
system("CLS");
}
}

Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page


Bangalore 14
Dr. Harish Kumar N, A s s t . Professor, Dept of CSE, DSCE, Page
Bangalore 15

You might also like