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

queues data structures

The document provides comprehensive notes on queues, circular queues, and deques, detailing their definitions, graphical representations, applications, and implementations. It explains basic operations such as enqueue and dequeue, along with algorithms for these operations and sample programs in C for implementing queues and deques using arrays. Additionally, it highlights the differences between linear queues and circular queues, as well as between linear queues and deques.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

queues data structures

The document provides comprehensive notes on queues, circular queues, and deques, detailing their definitions, graphical representations, applications, and implementations. It explains basic operations such as enqueue and dequeue, along with algorithms for these operations and sample programs in C for implementing queues and deques using arrays. Additionally, it highlights the differences between linear queues and circular queues, as well as between linear queues and deques.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

2nd UNIT Notes

Queue
1. Definition of queue
2. Graphical representation of queue
3. Applications of queue
4. Implementation of queue
5. Differences between stacks and queues
6. Basic operations
7. Program to implement queue using
a. Array
b. Linked list
DEFINITION OF QUEUE
Queue is a linear data structure, in which the first element is inserted from one end called
the REAR (also called tail), and the removal of existing element takes place from the other end
called as FRONT (also called head).

This makes queue as FIFO (First in First Out) data structure, which means that element inserted
first will be removed first.

Unlike stacks, a queue is open at both its ends. 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.

GRAPHICAL REPRESENTATION OF QUEUE


A real-world example of queue can be a single-lane one-way road, where the vehicle enters
first, exits first. More real-world examples can be seen as queues at the ticket windows and
bus-stops.

APPLICATIONS OF QUEUE
1. Real-time Examples
 Waiting in a line
2. Related to computer science
 Key board buffer
 Job scheduling.

IMPLEMENTATION OF QUEUE

A queue can also be implemented using


 Arrays
 Linked-lists

DIFFERENCES BETWEEN STACKS AND QUEUES


BASIC OPERATIONS
 enqueue () − add (store) an item to the queue.
 dequeue () − remove (access) an item from the queue.
 peek () − Gets the element at the front of the queue without removing it.
 isfull () − Checks if the queue is full.
 isempty () − Checks if the queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or
storing) data in the queue we take help of rear pointer.

ALGORITHM FOR ENQUEUE OPERATION

The following steps should be taken to enqueue (insert) data into a queue
 Step 1 − Check if the queue is full.
 Step 2 − If the queue is full, produce overflow error and exit.
 Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
 Step 4 − Add data element to the queue location, where the rear is pointing.
 Step 5 − Return success.

ALGORITHM FOR DEQUEUE OPERATION

The following steps are taken to perform dequeue operation −


 Step 1 − Check if the queue is empty.
 Step 2 − If the queue is empty, produce underflow error and exit.
 Step 3 − If the queue is not empty, access the data where front is pointing.
 Step 4 − Increment front pointer to point to the next available data element.
 Step 5 − Return success.
PROGRAM TO IMPLEMENT QUEUE USING ARRAY

#include<stdio.h>
#include<conio.h>
int Q[20],f=-1,r=-1,s;
void insert(int x)
{
if(r == s-1)
printf("Queue is full");
else
{
printf("enter the element to insert");
scanf("%d",&x);
if ( r == -1)
f = r =0;
else
r++;
Q[r]= x;
}
}
int del()
{
int x;
if( f == -1)
{
printf("Queue is empty");
return -1;
}
else
{
x = Q[f];
if ( f == r)
f = r = -1;
else
f++;
return x;
}
}

void display()
{
int i;
if(f == -1)
printf("Queue is empty");
else
{
for(i=f;i<=r;i++)
printf("%5d",Q[i]);
}
}
void main()
{
int i,x,op;
clrscr();
printf("\n Enter the size of the Queue");
scanf("%d",&s);
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n 4.Exit");
printf("\n Enter your choice");
scanf("%d",&op);
switch (op)
{
case 1 : insert(x);
break;
case 2 : x = del();
if (x != -1)
printf("\n Deleted elements is %d",x);
break;
case 3 : display();
break;
}
}
while(op != 4);
getch();
}
Circular Queue
1. Limitation of linear queue
2. Definition of circular queue
3. Graphical representation of circular queue
4. Differences between linear queue and circular queue
5. Basic operations
6. Implementation of queue
7. Program to implement queue using
a. Array
b. Linked list

LIMITATION OF LINEAR QUEUE


In a Linear Queue, we can insert elements until queue becomes full. But once the queue becomes
full, we cannot insert the next element until all the elements are deleted from the queue.
For example, consider the queue below...
The queue after inserting all the elements into it is as follows...

Now consider the following situation after deleting three elements from the queue...

This situation also says that Queue is FULL and we cannot insert the new element because
'rear' is still at last position. In the above situation, even though we have empty positions in the
queue we cannot make use of them to insert the new element. This is the major problem in a
linear queue. To overcome this problem we use a circular queue data structure.
DEFINITION OF CIRCULAR QUEUE
A circular queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make a
circle.
GRAPHICAL REPRESENTATION OF CIRCULAR QUEUE

DIFFERENCES BETWEEN LINEAR QUEUE AND CIRCULAR QUEUE

BASIC OPERATIONS
 enqueue () − add (store) an item to the queue.
 dequeue () − remove (access) an item from the queue.
 peek () − Gets the element at the front of the queue without removing it.
 isfull () − Checks if the queue is full.
 isempty () − Checks if the queue is empty.
In circular queue, we always dequeue (or access) data, pointed by front pointer and while
enqueing (or storing) data in the queue we take help of rear pointer.
IMPLEMENTATION OF QUEUE

A queue can also be implemented using


 Arrays
 Linked-lists

ALGORITHM FOR ENQUEUE OPERATION


The following steps should be taken to enqueue (insert) data into a queue
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 5 − Return success.

ALGORITHM FOR DEQUEUE OPERATION


The following steps are taken to perform dequeue operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.

PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAY


#include<stdio.h>
#include<conio.h>

int Q[20],f=-1,r=-1,s,x;

void insert()
{

if(r == s-1&& f == 0 || f == r+1 )


printf("Queue is full");
else
{
printf("enter the element to insert");
scanf("%d",&x);
if ( r == -1)
f = r =0;
else
r=(r+1)%s;
Q[r]= x;
}
}
int del()
{
int x;
if( f == -1)
{
printf("Queue is empty");
return -1;
}
else
{
x = Q[f];
if ( f == r)
f = r = -1;
else
f=(f+1)%s;
return x;
}
}

void display()
{
int i;
if(f == -1)
printf("Queue is empty");
else if (f>r)
{
for(i=f;i<s;i++)
printf("%5d",Q[i]);
for(i=0;i<=r;i++)
printf("%5d",Q[i]);
}
else
{
for(i=f;i<=r;i++)
printf("%5d",Q[i]);
}
}
void main()
{
int i,x,op;
clrscr();
printf("\n Enter the size of the Queue");
scanf("%d",&s);
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n 4.Exit");
printf("\n Enter your choice");
scanf("%d",&op);
switch (op)
{
case 1 : insert();
break;
case 2 : x = del();
if (x != -1)
printf("\n Deleted elements is %d",x);
break;
case 3 : display();
break;
}
}
while(op != 4);
getch();

Dequeue/Deque
1. Need for dequeue
2. Definition of dequeue
3. Graphical representation of dequeue
4. Differences between linear queue and dequeue
5. Basic operations
6. Implementation of dequeue
7. Program to implement queue using
a. Array
b. Linked list
NNED FOR DEQUEUE
When the user want the fast access of data items.
DEFINITION OF DEQUEUE
A double-ended queue is an abstract data type that generalizes a queue, for which elements can
be added to or removed from either the front (head) or rear (tail).
New items can be added at either the front or the rear. Likewise, existing items can be removed
from either end. This linear structure provides all the capabilities of stacks and queues in a single
data structure.
GRAPHICAL REPRESENTATION OF DEQUEUE

In real scenario we can attached it to a Ticket purchasing line, It performs like a queue but some
time It happens that some body has purchased the ticket and suddenly they come back to ask
something on front of queue. In this scenario because they have already purchased the ticket so
they have privilege to come and ask for any further query. So in these kind of scenario we need a
data structure where according to requirement we add data from front. And In same scenario user
can also leave the queue from rear.
Dequeue has 2 possible sub-types:
 An input-restricted dequeue is one where deletion can be made from both ends, but insertion
can be made at one end only.

 An output-restricted dequeue is one where insertion can be made at both ends, but deletion
can be made from one end only.

DIFFERENCES BETWEEN LINEAR QUEUE AND DEQUEUE

LINEAR QUEUE DEQUEUE


Insertion is possible at rear end, deletion is Front end and Rear end can be used both for
possible at front end. insertion and deletion.
Follows FIFO. Does not follow FIFO.

BASIC OPERATIONS
 insertFront (): Adds an item at the front of Dequeue.
 insertRear(): Adds an item at the rear of Dequeue.
 deleteFront(): Deletes an item from front of Dequeue.
 deleteRear(): Deletes an item from rear of Dequeue.
 isEmpty(): Checks whether Dequeue is empty or not.
 isFull(): Checks whether Dequeue is full or not.
In circular queue, we can perform insert, delete operations by considering both front and rear
pointers.

IMPLEMENTATION OF DEQUEUE

A dequeue can also be implemented using


 Arrays
 Double Linked-lists
ALGORITHM FOR INSERT OPERATION
The following steps should be taken to insert data into a queue
Step 1 − Check if the queue is full.
Step 2 − If the queue is full, produce overflow error and exit.
Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
Step 4 − Add data element to the queue location, where the rear is pointing.
Step 5 − Return success.

ALGORITHM FOR DELETE OPERATION


The following steps are taken to perform delete operation −
Step 1 − Check if the queue is empty.
Step 2 − If the queue is empty, produce underflow error and exit.
Step 3 − If the queue is not empty, access the data where front is pointing.
Step 4 − Increment front pointer to point to the next available data element.
Step 5 − Return success.

PROGRAM TO IMPLEMENT DEQUEUE USING ARRAY(circular queue concept)


#define size 5
#include <stdio.h>
int deque[size];
int f=-1, r=-1;
// enqueue_front function will insert the value from the front
void enqueue_front(int x)
{
if((f==0 && r==size-1) || (f==r+1))
{
printf("deque is full");
}
else if((f==-1) && (r==-1))
{
f=r=0;
deque[f]=x;
}
else if(f==0)
{
f=size-1;
deque[f]=x;
}
else
{
f=f-1;
deque[f]=x;
}
}

// enqueue_rear function will insert the value from the rear


void enqueue_rear(int x)
{
if((f==0 && r==size-1) || (f==r+1))
{
printf("deque is full");
}
else if((f==-1) && (r==-1))
{
r=0;
deque[r]=x;
}
else if(r==size-1)
{
r=0;
deque[r]=x;
}
else
{
r++;
deque[r]=x;
}

// display function prints all the value of deque.


void display()
{
int i=f;
printf("\n Elements in a deque : ");
while(i!=r)
{
printf("%d ",deque[i]);
i=(i+1)%size;
}
printf("%d",deque[r]);
}

// getfront function retrieves the first value of the deque.


void getfront()
{
if((f==-1) && (r==-1))
{
printf("Deque is empty");
}
else
{
printf("\nThe value of the front is: %d", deque[f]);
}

// getrear function retrieves the last value of the deque.


void getrear()
{
if((f==-1) && (r==-1))
{
printf("Deque is empty");
}
else
{
printf("\nThe value of the rear is: %d", deque[r]);
}

// dequeue_front() function deletes the element from the front


void dequeue_front()
{
if((f==-1) && (r==-1))
{
printf("Deque is empty");
}
else if(f==r)
{
printf("\nThe deleted element is %d", deque[f]);
f=-1;
r=-1;

}
else if(f==(size-1))
{
printf("\nThe deleted element is %d", deque[f]);
f=0;
}
else
{
printf("\nThe deleted element is %d", deque[f]);
f=f+1;
}
}

// dequeue_rear() function deletes the element from the rear


void dequeue_rear()
{
if((f==-1) && (r==-1))
{
printf("Deque is empty");
}
else if(f==r)
{
printf("\nThe deleted element is %d", deque[r]);
f=-1;
r=-1;

}
else if(r==0)
{
printf("\nThe deleted element is %d", deque[r]);
r=size-1;
}
else
{
printf("\nThe deleted element is %d", deque[r]);
r=r-1;
}
}

int main()
{
// inserting a value from the front.
enqueue_front(2);
// inserting a value from the front.
enqueue_front(1);
// inserting a value from the rear.
enqueue_rear(3);
// inserting a value from the rear.
enqueue_rear(5);
// inserting a value from the rear.
enqueue_rear(8);
// Calling the display function to retrieve the values of deque
display();
// Retrieve the front value
getfront();
// Retrieve the rear value.
getrear();
// deleting a value from the front
dequeue_front();
//deleting a value from the rear
dequeue_rear();
// Calling the display function to retrieve the values of deque
display();
return 0;
}

You might also like