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

queue and types

The document provides an overview of queues, an abstract data structure that follows the FIFO (First In First Out) principle, detailing its basic operations such as enqueue, dequeue, and peek. It discusses the advantages and disadvantages of queues, as well as different types including Simple Queue, Circular Queue, Priority Queue, and Double Ended Queue. Additionally, it includes implementations of queues using both arrays and linked lists in C programming.
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 views

queue and types

The document provides an overview of queues, an abstract data structure that follows the FIFO (First In First Out) principle, detailing its basic operations such as enqueue, dequeue, and peek. It discusses the advantages and disadvantages of queues, as well as different types including Simple Queue, Circular Queue, Priority Queue, and Double Ended Queue. Additionally, it includes implementations of queues using both arrays and linked lists in C programming.
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/ 10

Data Structures

Unit II Queue
Queue is an abstract data structure, similar to Stacks. A queue follows the FIFO (First In First Out)
method and is open at both of its ends.
A queue can be defined as an ordered list which enables insert operations to be performed at one end
called REAR and delete operations to be performed at another end called FRONT.

Example
Let’s consider a line of people waiting to buy a ticket at a cinema hall. A new person will join the
line from the end and the person standing at the front will be the first to get the ticket and leave the
line.

The representation of the queue is shown in the below image –

Basic Operations

Below are the basic queue operations in data structure:


Operation Description
enqueue() Process of adding or storing an element to the end of the queue
dequeue() Process of removing or accessing an element from the front of the queue
peek() Used to get the element at the front of the queue without removing it
initialize() Creates an empty queue
isfull() Checks if the queue is full
isempty() Check if the queue is empty

Advantages of Queue:
 A large amount of data can be managed efficiently with ease.
 Operations such as insertion and deletion can be performed with ease as it follows the first in
first out rule.
 Queues are useful when a particular service is used by multiple consumers.
 Queues are fast in speed for data inter-process communication.
 Queues can be used in the implementation of other data structures.

Disadvantages of Queue:
 The operations such as insertion and deletion of elements from the middle are time consuming.
 Limited Space.
 In a classical queue, a new element can only be inserted when the existing elements are deleted
from the queue.

Page | 1
Data Structures

 Searching an element takes more time.


 Maximum size of a queue must be defined prior.

Types of Queue
There are four different types of queue that are listed as follows -
o Simple Queue or Linear Queue
o Circular Queue
o Priority Queue
o Double Ended Queue (or Deque)

Simple Queue or Linear Queue


In Linear Queue, an insertion takes place from one end while the deletion occurs from another end.
The end at which the insertion takes place is known as the rear end, and the end at which the deletion
takes place is known as front end. It strictly follows the FIFO rule.

The major drawback of using a linear Queue is that insertion is done only from the rear end. If the
first three elements are deleted from the Queue, we cannot insert more elements even though the
space is available in a Linear Queue. In this case, the linear Queue shows the overflow condition as
the rear is pointing to the last element of the Queue.

Implementation of queue using array

#include<stdio.h>
#include<stdlib.h>
# define SIZE 100

void enqueue();
void dequeue();
void show();

int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;

void main()
{
int ch;
while (1)
{
printf("1.Enqueue Operation\n");
printf("2.Dequeue Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch)

Page | 2
Data Structures

{
case 1: enqueue();
break;
case 2: dequeue();
break;
case 3: show()
break;
case 4: exit (0);
break;
default: printf("Incorrect choice \n");
}
}
}

void enqueue()
{
int insert_item;
if (Rear == SIZE - 1)
printf("Overflow \n");
else
{
if (Front == - 1)
Front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &insert_item);
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}

void dequeue()
{
if (Front == - 1 || Front > Rear)
{
printf("Underflow \n");
return ;
}
else
{
printf("Element deleted from the Queue: %d\n", inp_arr[Front]);
Front = Front + 1;
}
}

void show()
{

if (Front == - 1)
printf("Empty Queue \n");
else
{
printf("Queue: \n");
for (int i = Front; i <= Rear; i++)
printf("%d ", inp_arr[i]);
printf("\n");
}
}

Page | 3
Data Structures

Output

Linked List implementation of Queue

In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Each
element of the queue points to its immediate next element in the memory.

In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear
pointer. The front pointer contains the address of the starting element of the queue while the rear
pointer contains the address of the last element of the queue.

Insertion and deletions are performed at rear and front end respectively. If front and rear both are
NULL, it indicates that the queue is empty.

The linked representation of queue is shown in the following figure.

Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void del();
void display();
void main()

Page | 4
Data Structures

{
int choice;
while(choice != 4)
{
printf("\nMain Menu");
printf("\n1.insert element\n2.Delete element\n3.Display the queue
\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:printf("\nEnter valid choice");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("\nOVERFLOW");
return;
}
else
{
printf("\nEnter value:");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void del()
{
struct node *ptr;
if(front == NULL)
{

Page | 5
Data Structures

printf("\nUNDERFLOW");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue");
}
else
{ printf("\nprinting values : ");
while(ptr != NULL)
{
printf("\n%d",ptr -> data);
ptr = ptr -> next;
}
}
}

Output

Page | 6
Data Structures

Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue except
that the last element of the queue is connected to the first element. It is also known as Ring Buffer, as
all the ends are connected to another end. The representation of circular queue is shown in the below
image -

The drawback that occurs in a linear queue is overcome by using the circular queue. If the empty
space is available in a circular queue, the new element can be added in an empty space by simply
incrementing the value of rear. The main advantage of using the circular queue is better memory
utilization.

Operations on Circular Queue


The following are the operations that can be performed on a circular queue:

o Front: It is used to get the front element from the Queue.


o Rear: It is used to get the rear element from the Queue.
o enQueue(value): This function is used to insert the new value in the Queue. The new element
is always inserted from the rear end.
o deQueue(): This function deletes an element from the Queue. The deletion in a Queue always
takes place from the front end.

Page | 7
Data Structures

The enqueue and dequeue operation through the diagrammatic representation

Implementation of circular queue using Array

#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow\n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("Queue Underflow\n");
return ;
}

Page | 8
Data Structures

printf("Element deleted from queue is : %d\n",cqueue_arr[front]);


if(front == rear)
{
front = -1;
rear = -1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}

int main()
{
int choice,item;
do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

Page | 9
Data Structures

switch(choice)
{
case 1 :
printf("Input the element for insertion in queue:");
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice\n");
}
}while(choice!=4);
return 0;
}
Output

Page | 10

You might also like