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

queue

Jjk ch 2

Uploaded by

jannatjoya466
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

queue

Jjk ch 2

Uploaded by

jannatjoya466
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Queue

A queue in the data structure can be considered similar to the


queue in the real-world.
world. A queue is a data structure in which
whatever comes first will go out first. It follows the FIFO (First-In-
(First
First-Out)
Out) policy. In Queue, the insertion is done from one end
known as the rear end or the tail of the queue, whereas the
deletion is done from another end known as the front end or the
head of the queue. In other words, it can be defined as a list or a
collection with a constraint that the insertion can be performed at
one end called as the rear end or tail of the queue and deletion is
performed on another end called as the front end or the head of
the queue.
Queue is a linear structure which follows a particular order in
which the operations are performed. The order
is First In First Out(FIFO).
ut(FIFO).
A queue can be defined as an ordered list which enables insert
operations to be performed at one end called REAR and delete
operations
rations to be performed at another end called FRONT.
FRONT Queue is
referred to be as First In First Out list. For example, people waiting
in line for a rail ticket form a queue.
Types of Queues in Data Structure

Queue in data structure is of the following types


1. Linear Queue
2. Circular Queue
3. Priority Queue
4. Dequeue (Double Ended Queue)

Linear Queue
The simple queue is a normal queue where insertion takes place at
the FRONT of the queue and deletion takes place at the END of the
queue.

Circular Queue
 In a circular queue, the last node is connected to the first node.
 Circular queue is also called as Ring Buffer.
 Insertion in a circular queue happens at the FRONT and deletion at
the END of the queue.
Priority Queue
 In a priority queue, the nodes will have some predefined priority.
 Insertion in a priority queue is performed in the order of arrival of the
nodes.
 The node having the least priority will be the first to be removed from
the priority queue.
Dequeue (Doubly Ended Queue)
In a Double Ended Queue, insertion and deletion operations can be done at
both FRONT and END of the queue.

Operations on Queue There are two fundamental operations performed on a


Queue:
o Enqueue: The enqueue operation is used to insert the
element at the rear end of the queue. It returns void.
o Dequeue: The dequeue operation performs the deletion from
the front-end of the queue. It also returns the element which
has been removed from the front-end. It returns an integer
value. The dequeue operation can also be designed to void.
o Peek: This is the third operation that returns the element,
which is pointed by the front pointer in the queue but does
not delete it.
o Queue overflow (isfull): When the Queue is completely full,
then it shows the overflow condition.
o Queue underflow (isempty): When the Queue is empty, i.e.,
no elements are in the Queue then it throws the underflow
condition.
Applications of Queue
o Queues are widely used as waiting lists for a single shared

resource like printer, disk, CPU.


o Queues are used in asynchronous transfer of data (where data

is not being transferred at the same rate between two


processes) for eg. pipes, file IO, sockets.
o Queues are used as buffers in most of the applications like

MP3 media player, CD player, etc.


o Queue are used to maintain the play list in media players in

order to add and remove the songs from the play-list.


o Queues are used in operating systems for handling interrupts.

The difference between stacks and queues is in removing. In a


stack we remove the item the most recently added; in a queue, we
remove the item the least recently added.
Difference between Stack and Queue
Stack Queue

A Linear List Which allows


A Linear List Which allows
insertion or deletion of an
insertion atone end and deletion at
element at one end only is
another end is called as Queue
called as Stack
Since insertion and deletion of
Since insertion and deletion of an
an element are performed at
element are performed at opposite
one end of the stack, the
end of the queue, the elements can
elements can only be removed
only be removed in the same order
in the opposite order of
of insertion.
insertion.
Stack is called as Last In First Queue is called as First In First Out
Out (LIFO) List. (FIFO) List.
The most and least accessible Insertion of element is performed
elements are called as TOP and at FRONT end and deletion is
BOTTOM of the stack performed from REAR end
Example of stack is arranging Example is ordinary queue in
plates in one above one. provisional store.
Insertion operation is referred Insertion operation is referred as
as PUSH and deletion ENQUEUE and deletion operation
operation is referred as POP is referred as DQUEUE
Function calling in any Task Scheduling by Operating
languages uses Stack System uses queue

Implementation of Queue
Queue can be implemented in two ways :
1. Static implementation (using arrays)
2. Dynamic implementation (using linked list)
Static implementation (using arrays)
A queue data structure can be implemented using one dimensional
array. The queue implemented using array stores only fixed
number of data values. The implementation of queue data
structure using array is very simple. Just define a one dimensional
array of specific size and insert or delete the values into that array
by using FIFO (First In First Out) principle with the help of
variables 'front' and 'rear'. Initially both 'front' and 'rear' are set
to -1. Whenever, we want to insert a new value into the queue,
increment 'rear' value by one and then insert at that position.
Whenever we want to delete a value from the queue, then delete
the element which is at 'front' position and increment 'front' value
by one.
enQueue(value) - Inserting value into the queue
In a queue data structure, enQueue( ) is a function used to insert a
new element into the queue. In a queue, the new element is always
inserted at rear position. The enQueue() function takes one
integer value as a parameter and inserts that value into the queue.
We can use the following steps to insert an element into the
queue...
Step 1 - Check whether queue is FULL. if(rear == SIZE-1)
Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is
not possible!!!" and terminate the function.
Step 3 - If it is NOT FULL, then increment rear value by one
(rear++) and set queue[rear] = value.
Algorithm
 Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
 Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
 Step 3: Set QUEUE[REAR] = NUM
 Step 4: EXIT
deQueue() - Deleting a value from the Queue
In a queue data structure, deQueue() is a function used to delete
an element from the queue. In a queue, the element is always
deleted from front position. The deQueue() function does not take
any value as parameter. We can use the following steps to delete
an element from the queue...
Step 1 - Check whether queue is EMPTY. if(front == rear)
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion
is not possible!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then increment the front value by one
(front ++). Then display queue[front] as deleted element. Then
check whether both front and rear are equal (front == rear), if
it TRUE, then set both front and rear to '-1' (front = rear = -1).
Algorithm
 Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
 Step 2: EXIT

display() - Displays the elements of a Queue


We can use the following steps to display the elements of a queue...
Step 1 - Check whether queue is EMPTY. (front == rear)
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and
terminate the function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and
set 'i = front+1'.
Step 4 - Display 'queue[i]' value and increment 'i' value by one
(i++). Repeat the same until 'i' value reaches to rear (i <= rear)

Example: Program to implement a queue using Array

#include <stdio.h>
#include<conio.h>
#define MAX 10
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
do
{
printf("1.Insert \n");
printf("2.Delete\n");
printf("3.Display \n");
printf("4.Exit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Inavlid choice \n");
} /*End of switch*/
}while(choice!=4); /*End of while*/
getch();
} /*End of main()*/
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Deleted Element is : %d\n", queue_array[front]);
front = front + 1;
}
} /*End of delete() */
display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /*End of display() */

You might also like