Академический Документы
Профессиональный Документы
Культура Документы
QUEUE
Queue is a linear data structure, which is opened at both ends front end and rear end. Insertion
takes place at rear end and deletion takes place at front end. Removal of elements are done in the
same order they were inserted i.e., the element will be removed first which is inserted first. So,
another name for a queue is a “FIFO” or “First-in-first-out” list.
Representation of Queue:
Operations on Queue:
In order to create a queue, we require a one dimensional array and two variables front and rear.
‘front’ and ‘rear’ are initialized with value ‘-1’ initially i.e., when queue is empty.
▪ Now if in any case if “‘rear’ is equal to (size of the array) –1 but ‘front’ is not equal
to 0 then to insert an new element, the elements in the queue must be shifted towards
the left most direction of the array. So that at the rear end one or more free spaces
become available to insert new elements.
Example:
In fig. A, an empty queue is represented with front = -1 and rear = -1. In fig. B, 10 is inserted into
the queue and it is the first element in the queue with front = 0 and rear = 0. In fig. C, 11 is inserted
into the queue with front = 0 and rear = 1. So, like this, 12, 13,14,15,16 and 17 are inserted into the
queue in fig. D, E, G, H, K and L respectively. In fig. J, the existing elements in the queue are
shifted towards the beginning of the queue to make free space for the insertion of 16 and 17.
In fig. F , 10 is deleted with front = 1 and rear = 3. In fig. I, 11 is deleted with front = 2 and rear = 5.
The various queue operations to perform insertion, deletion and display the elements in a queue are
as follows:
• INSERTION IN QUEUE (ENQUEUE)
Step 1: IF front == -1
Step 2: PRINT “ Queue is underflow”
Step 3: END OF IF
Step 4: ELSE
Step 5: front = front + 1
Step 6: IF front > rear
Step 7: front = -1
Step 8: rear = -1
Step 9: END IF
Step 10: END OF ELSE
• ptr→ADDRESS means address part of the node pointed by the pointer “ptr” which points
the next node in the queue implemented using singly linked list.
• ptr→DATA means data part of the node pointed by the pointer “ptr”.
• “newnode” is the pointer which points the node to be inserted into the queue implemented
using singly linked list.
• ‘element’ is a variable to store the new element to be inputted into the queue implemented
using singly linked list.
• ptr→ADDRESS means address part of the node pointed by the pointer “ptr” which points
the next node in the queue implemented using singly linked list.
• “temp” is a pointer to point any node of a queue implemented using singly linked list.
//Implementation of Queue using Linked List
# include <stdlib.h>
struct queue{
int data;
struct queue *next;
};
struct queue *front = NULL;
struct queue *rear = NULL;
/* Function body for Insertion operation in queue : ENQUEUE */
void insertQ(){
struct queue *newnode;
newnode = (struct queue *) malloc(sizeof(newnode));
printf(" Enter data ");
scanf("%d", &newnode>data);
newnode>next = NULL;
if(newnode == NULL){
printf("\n Queue Full");
return;
}
if(front == NULL){
front = newnode;
rear = newnode;
}
else{
rear>next = newnode;
rear = newnode;
}
printf(" Data Inserted into the Queue..");
displayQ();
}// insertQ function ends
/* Function body for Deletion operation in queue : DEQUEUE */
void deleteQ(){
struct queue *temp;
if(front == NULL){
printf("\t Empty Queue..");
return;
}
temp = front;
front = front>next;
printf(" Deleted element from queue is %d ", temp>data);
free(temp);
displayQ();
}// deleteQ function ends
/* Function body for Display operation in queue : PEEK */
void displayQ()
{
struct queue *temp;
if(front == NULL){
printf("\t Empty Queue ");
}
else{
temp = front;
printf("\t Elements in the Queue are: ");
while(temp != NULL ){
printf("%5d ", temp > data);
temp = temp > next;
}
}
}// displayQ function ends
void main(){
int ch;
do{
printf("\n\t **********");
printf("\n \t..Queue operations using pointers.. ");
printf("\n\t **********");
printf("\n 1. Insert ");
printf("\n 2. Delete ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch(ch){
case 1 :
insertQ();
break;
case 2 :
deleteQ();
break;
case 3 :
displayQ();
break;
case 4:
exit(0);
}
} while(ch != '4');
}
OUTPUT
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 1
Enter data 10
Data Inserted into the Queue..
Elements in the Queue are: 10
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 1
Enter data 20
Data Inserted into the Queue..
Elements in the Queue are: 10 20
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 1
Enter data 50
Data Inserted into the Queue..
Elements in the Queue are: 10 20 50
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 2
Deleted element from queue is 10
Elements in the Queue are: 20 50
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 2
Deleted element from queue is 20
Elements in the Queue are: 50
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 2
Deleted element from queue is 50 Empty Queue
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 2
Empty Queue..
**********
..Queue operations using pointers..
**********
1. Insert
2. Delete
3. Display
4. Quit
Enter your choice: 4
TYPES OF QUEUE
1. Simple Queue : Simple queue defines the simple operation of queue in which insertion
occurs at the rear of the list and deletion occurs at the front of the list.
2. Circular Queue: Circular queue is an abstract datatype in which last node is connected
back to the first node. It is also known as ring buffer, circular buffer, cyclic buffer.
3. Double Ended Queue: In Double Ended Queue, insert and delete operation can occur at
both ends that is front and rear of the queue.
4. Priority Queue: A priority queue is a collection of elements such that each element has
been assigned a priority and such that the order in which elements are deleted and processed
comes from the following rules:
1. An element of higher priority is processed before any element of lower priority.
2. Two elements with same priority are processed according to the order in which they
were added to the queue.
In case of max-priority queue, the element will be deleted first which has the largest priority
value and in case of min-priority queue the element will be deleted first which has the
minimum priority value.
APPLICATION OF QUEUE
There are several applications of queue available in computer system. Some of these are given as
follows:
• In printers, queue is used to print the different files.
• Queue is used to access files from a disk system.
• In a multiprogramming environment, queue is used for CPU scheduling or job scheduling of
operating system.
• In any type of ticket reservation system, queue can be used for issuing tickets to the
customers.
• Queue is used in the implementation of breadth first traversal of graph.
• Queue is used in many other real world systems which are used in some scientific research,
military operations etc.
CIRCULAR QUEUE
In static implementation, if ‘rear’ is equal to [(size of the queue)-1] and ‘front’ is not equal to 0
then a new element is inserted into the array at the subscript value 0 th position. Thus queue uses its
storage array as if it were a circle instead of a linear list. In this implementation,
• ‘front’ and ‘rear’ are initialized with the value ‘-1’ (similar to simple queue).
• The overflow condition is “ ‘front’ is equal to the next value of the rear’s recent value”.
• The underflow condition is “ ‘front’ is equal to ‘-1’ “.
• If ‘front’ is not equal to -1 and ‘front’ is equal to ‘rear’ then it means that there is only one
element is available in the circular queue.
Fig. (G) Fig. (H) Fig. (I) Fig. (J) Fig. (K) Fig. (L)
• In fig. (A), an empty circular queue is represented with front = -1 and rear = -1.
• In fig. (B), 10 is inserted into the queue and it is the first element in the queue with front = 0
and rear = 0. Here ‘front’ is equal to ‘rear’ which means only one element is available in the
circular queue.
• In fig. (C), 11 is inserted into the queue with front = 0 and rear = 1.
• Similarly 12,13,14,15,16 and 17 are inserted into the circular queue in figures (D), (E), (G),
(I), (J) and (K) respectively.
• In fig. (F),10 is deleted from the circular queue with front = 1 and rear = 3.
• In fig. (H),11 is deleted from the circular queue with front = 2 and rear = 4.
• In fig. (L), 12 is deleted from the circular queue with front = 3 and rear = 1.
Step 1: IF front == -1
Step 2: PRINT” queue is underflow”
Step 3: END OF IF
Step 4: ELSE
Step 5: IF front == rear)
Step 6: front = -1
Step 7: rear = -1
Step 8: END OF IF
Step 9: ELSE
Step 10: front = (front+1)%(size of cqueue)
Step 11: END OF ELSE
Step 12: END OF ELSE