0% found this document useful (0 votes)
22 views7 pages

Unit-3 2

Uploaded by

ramketha07
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)
22 views7 pages

Unit-3 2

Uploaded by

ramketha07
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/ 7

Data Structures UNIT-III

QUEUES
Queue is a linear data structure used in various applications of computer science. Like
people stand in a queue to get a particular service, various processes will wait in a queue for
their turn to avail a service.
Queue is a linear list in which insertions takes place at one end called
the rear or tail of the queue and deletions at the other end called as front or
head of the queue.
When an element is to join the queue, it is inserted at the rear end of
the queue and when an element is to be deleted, the one at the front end of
the queue is deleted automatically.
In queues always the first inserted element will be the first to be deleted. That‟s why it is
also called as FIFO – First-in First-Out data structure (or FCFS – First Come First Serve data
structure).
APPLICATIONS of QUEUE
 CPU Scheduling (Round Robin Algorithm)
 Printer Spooling
 Tree & Graph Traversals
 Palindrome Checker
 Undo & Redo Operatio ns in some Software‟s

OPERATIONS ON QUEUE
The queue data structure supports two operations:
Enqueue: Inserting an element into the queue is called enqueuing the queue. After the data
have been inserted into the queue, the new element becomes the rear.
Dequeue: Deleting an element from the queue is called dequeuing the queue. The data at the
front of the queue are returned to the user and re move d from the queue.

TYPES OF QUEUES
1. Simple Queue: In this queue insertions take place at one end anddeletions take
place at other end.
2. Circular Queue: It is similar to single ended queue, but the front is connected back to
rear. Here the memory can be utilized effectively.
3. Double Ended Queue: In double ended queue both the insertions and deletions can
take place at both the ends.
4. Priority Queue: In priority queue the elements are not deleted according to the order
they entered into the queue, but according to the priorities associated with the
elements.
IMPLEMENTATION OF QUEUES
Queues can be implemented or represented in memory in two ways:
1. Using Arrays (Static Implementation).
2. Using Linked Lists (Dynamic Implementation).

Implementation of Queue Using Arrays


A common method of implementing a queue data structure is to use another sequential
data structure is arrays. In this representation, two pointers namely, Front and Rear are used
to indicate the two ends of the queue. For insertion of next element, pointer Rear will be
adjusted and for deletion pointer Front will be adjusted.

CSE Dept, VIT 1


Data Structures UNIT-III

However, the array implementation puts a limitation on the capacity of the queue. The
number of elements in the queue cannot exceed the maximum dimension of the one
dimensional array. Thus a queue that is accommodated in an array Q[1:n], cannot hold more
than n elements. Hence every insertion of an element into the queue has to necessarily test
for a QUEUE FULL condition before executing the insertion operation. Again, each deletion has
to ensure that it is not attempted on a queue which is already empty calling for the need to
test for a QUEUE EMPTY condition before executing the deletion operation.

Algorithm of insert operation on a queue


Procedure INSERTQ (Q, n, ITEM, REAR)
// this procedure inserts an element ITEM into Queue with capacity n

Step 1: if(REAR=n ) then


Write:“QUEUE_FULL” and Exit
Step 2: REAR=REAR + 1 //Increment REAR
Step 3: Q[REAR]= ITEM //Insert ITEM as the rear element
Step 4: Exit
It can be observed that addition of every new element into the queue increments the REAR
variable. However, before insertion, the condition whether the queue is full is checked.

Algorithm of delete operation on a queue


Procedure DELETEQ (Q, FRONT, REAR, ITEM)
Step 1: If (FRONT >REAR) then:
Write: “QUEUE EMPTY” and Exit.
Step 2: ITEM = Q[FRONT]
Step 3: FRONT =F RONT + 1
Step 4: Exit.
To perform a delete operation, the participation of both the variables FRONT and REAR
is essential. Before deletion, the condition FRONT =REAR checks for the emptiness of the
queue. If the queue is not empty, the element is removed through ITEM and subsequently
FRONT is incremented by 1.

CSE Dept, VIT 2


Data Structures UNIT-III
printf("Queue Overflow \n");
/*C implementation of Queue using else
Arrays*/ {
#include <stdio.h> if (front == - 1)
#include <conio.h> /*If queue is initially e mpty */
#define MAX 5 front = 0;
printf("Inset the element in queue :
int insert(); /* function prototypes */ ");
int delete(); scanf("%d", &add_item);
void display(); rear = rear + 1;
queue[rear] = add_ite m;
int queue[MAX], rear = -1, front = - 1; }
return;
main()
{ } /*End of insert()*/
int choice;
clrscr(); int delete()
printf("Implmentation of Queue\n"); {
printf(" ------------------- \n"); if (front == - 1 || front > rear)
while (1) {
{ printf("Queue Underflow \n");
printf("1.Insert\n2.Delete\n3.Displa return ;
y\n4.Quit\n"); }
printf("Enter your choice : "); else
scanf("%d", &choice); {
switch (choice) printf("Element deleted from queue
{ is : %d\n", queue[front]);
case 1: front = front + 1;
insert(); }
break; return;
case 2: } /*End of delete() */
delete();
break; void display()
case 3: {
display(); int i;
break; if (front == - 1)
case 4: printf("Queue is empty \n");
exit(1); else
} /*End of switch*/ {
} /*End of while*/ printf("Queue is : \n");
} /*End of ma in()*/ for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
int insert() printf("\ n");
{ }
int add_ite m; } /*End of display() */
if (rear == MAX - 1)

CSE Dept, VIT 3


Data Structures UNIT-III

CIRCULAR QUEUE
One of the major problems with the linear queue is the lack of proper utilization of space.
Suppose that the queue can store 10 elements and the entire queue is full. So, it means that
the queue is holding 10 elements. In case, some of the elements at the front are deleted, the
element at the last position in the queue continues to be at the same position and there is no
efficient way to find out that the queue is not full.

In this way, space utilization in the case of linear queues is not efficient. This problem
is arising due to the representation of the queue.
The alternative representation is to depict the queue as circular. In case, we are
representing the queue using arrays, then, a queue with n elements starts from index 0 and
ends at n- 1. So, clearly, the first element in the queue will be at index 0 and the last element
will be at n-1 when all the positions between index 0 and n-1 (both inclusive) are filled. Under
such circumstances, front will point to 0 and rear will point to n-1. However, when a new
element is to be added and if the rear is pointing to n-1, then, it needs to be checked if the
position at index 0 is free. If yes, then the element can be added to that position and rear can
be adjusted accordingly. In this way, the utilization of space is increased in the case of a
circular queue.
In a circular queue, front will point to one
position less to the first element. So, if the first
element is at position 4 in the array, then the front
will point to position 3. When the circular queue is
created, then both front and rear point t o index 1.
Also, we can conclude that the circular queue is
empty in case both front and rear point to the same
index. Figure depicts a circular queue.
Enqueue (value) - Inserting value into the
Circular Queue
Step 1: Check whether queue is FULL.
If ((REAR == SIZE-1 && FRONT == 0) || (FRONT == REAR+1))
Write “Queue is full ” and Exit
Step 2: If ( rear == SIZE - 1 && front != 0 ) then:
SET REAR := -1.
Step 4: SET REAR = REAR +1
Step 5: SET QUEUE[REAR] = VALUE
Step 6: Exit and check 'front == - 1' if it is TRUE, then set front = 0.
deQueue() - Deleting a value from the Circular Queue
Step 1: Check whether queue is EMPTY ?
If (FRONT == -1 && REAR == -1)
Write “Queue is empty” and Exit.

Step 2: DISPLAY QUEUE[FRONT]


Step 3: SET FRONT := FRONT +1 .
Step 4: If( FRONT = SIZE, then:
SET FRONT := 0.
Step 5: Exit

CSE Dept, VIT 4


Data Structures UNIT-III

/* Program to implement Circular


Queue using Array */ if((front == 0 && rear == SIZE - 1) ||
(front == rear+1))
#include<stdio.h>
printf("\nCircular Queue is Full!
#include <conio.h>
Insertion not possible!!!\n");
#define SIZE 5
else{
if(rear == SIZE-1 && front != 0) rear
int cinsert(int);
= -1;
int cdelete();
void display(); cq[++rear] = value;
printf("\nInsertion Success!!!\n");
int cq[SIZE], front = -1, rear = -1;
if(front == -1)
void main()
front = 0;
{
}
int choice, ele;
return;
clrscr();
} /*End of cinsert() */
while(1){
printf("CIRCULAR QUEUE
int cdelete()
IMPLEMENTATION\n");
{
printf(" \n");
if(front == - 1 && rear == -1)
printf("****** MENU ******\n");
printf("\nCircular Queue is Empty!
printf("1. Insert\n2. Delete\n3.
Deletion is not possible!!!\n");
Display\n4. Exit\n");
else{
printf("Enter yourchoice: ");
printf("\nDeleted ele me nt :
scanf("%d",&choice);
%d\n",cq[front++]);
switch(choice){
if(front == SIZE)
case 1: printf("\nEnter the value to
front = 0;
be insert: ");
if(front-1 == rear)
scanf("%d",&ele);
front = rear = -1;
cinsert(ele);
}
break;
return;
case 2: cdelete();
} /*End of cdelete() */
break;
case 3: display();
void display()
break;
{
case 4: exit(1);
if(front == - 1)
} /*End of switch */
printf("\nCircular Queue is
} /*End of while */
Empty!!!\n");
} /*End of main */
else{
int i = front;
int cinsert(int value)
printf("\nCircular Queue Elements are :
{ \n");
if(front <= rear){

CSE Dept, VIT 5


Data Structures UNIT-III
while(i <= rear)
printf("%d\t",cq[i++]);
}
else{
while(i <= SIZE - 1) printf("%d\t",
cq[i++]);
i = 0;
while(i <= rear)
printf("%d\t",cq[i++]);
}

}
} /*End of display() */

DOUBLE ENDED QUEUE (DEQUEUE)


A Dequeue is a homogeneous list of elements in which insertions and deletion operations are performed on
both the ends. Because of this property it is known as double ended queue i.e. Dequeue or deck. Deque
has two types:

• Input restricted queue: It allows insertion at only one end


• Output restricted queue: It allows deletion at only one end
In dequeue four pointers are used. They are left front(lf), left rear(lr), right front(rf) and right rear(rr).

 If (lf==lr) and (rf==rr) then deque is empty.


 If lr >rr then deque ueis full
 For inserting we have to modify rear pointer. For deleting we have to modify front pointer.
 Always rear pointer is 1 position ahead of last ele ment.
 After insertion on left side, left rear should be incremented.
 After insertion on right side, right rear should be decre mented.

CSE Dept, VIT 6


Data Structures UNIT-III

Input Restricted Double Ended Queue


In input restricted double ended queue, the insertion operation is performed at only one end
and deletion operation is performed at both the ends.

Output Restricted Double Ended Queue


In output restricted double ended queue, the deletion operation is performed at only one end and
insertion operation is performed at both the ends.

CSE Dept, VIT 7

You might also like