Вы находитесь на странице: 1из 15

DS Notes Class: B.Tech.

CSE 4th Sem

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:

• enqueue: inserts an element at the end of the queue.


• dequeue: deletes an element from the start of the queue.

ARRAY IMPLEMENTATION OF QUEUE (Static Implementation)

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.

• Here to insert an element,


▪ at first the value of ‘rear’ is incremented if it is possible and then the element is
inputted into the array at the subscript value equal to the value of ‘rear’.

▪ The overflow condition for array implementation is “ ‘front’ is equal to 0 AND


‘rear’ is equal to (size of the array) –1’’.

▪ 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.

• Here in case of deletion operation,


▪ ‘front’ variable is incremented by one
▪ the underflow condition is “ ‘front’ is equal to ‘–1’’’.

Example:

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Fig. A Fig. B Fig. C Fig. D Fig. E Fig. F

Fig. G Fig. H Fig. I Fig. J Fig. K Fig. L

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.

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

The various queue operations to perform insertion, deletion and display the elements in a queue are
as follows:
• INSERTION IN QUEUE (ENQUEUE)

Algorithm for insertion operation in queue [ENQUEUE]:

➢ Here queue[ ] is the array to store the array elements.


➢ ‘front’ is a variable used to represent the front end of the queue.
➢ ‘rear’ is a variable to represent the rear end of the queue.
➢ ‘element’ is a variable to store the new element to be inputted into the queue.

Step 1: IF front == 0 AND rear == (size of queue)-1 THEN


Step 2: PRINT “ Queue is overflow”
Step 3: END OF IF
Step 4: ELSE
Step 5: IF rear == (size of queue) -1 AND front > 0 THEN
Step 6: FOR I = 0 To I == rear - front
Step 7: queue[I] = queue[front + I]
Step 8: END OF FOR
Step 9: rear = rear - front
Step 10: front = 0
Step 11: END OF IF
Step 12: IF front == - 1
Step 13: front = 0
Step 14: END OF IF
Step 15: rear = rear + 1
Step 16: queue[rear] = element
Step 17: END OF ELSE

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

• DELETION IN QUEUE (DEQUEUE)

Algorithm for deletion operation in queue [DEQUEUE]:

➢ Here queue[ ] is the array to store the array elements.


➢ ‘front’ is a variable used to represent the front end of the queue.
➢ ‘rear’ is a variable to represent the rear end of the queue.

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

Algorithm for displaying queue elements [PEEK]:

Step 1: IF front == -1 AND rear == -1


Step 2: PRINT “ Queue is empty”
Step 3: END OF IF
Step 4: ELSE
Step 5: FOR I = front to I = rear - 1
Step 6: PRINT “queue[I] ”
Step 7: END OF FOR
Step 8: END OF ELSE

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

LINKED LIST IMPLEMENTATION OF QUEUE (Dynamic Implementation)


[LINKED QUEUE]
• In this implementation queue is a singly linked list with two pointers ‘rear’ and ‘front’.
• ‘front’ points the first element in the list and ‘rear’ points the last elements in the list.
• ‘front’ and ‘rear’ are initialized to NULL.
• Here the underflow condition is ‘front’ is equal to NULL.
• In deletion operation, the node pointed by ‘front’ is deleted and then ‘front’ will point to the
next node of the deleted node if available.
• In insertion operation the new node is inserted after the node pointed by rear i.e at the last
position and ‘rear’ will point the new last node in the list.
Example of a queue implemented using Singly Linked List:

• AFTER INSERTION OF A NEW QUEUE ELEMENT:

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Algorithm for insertion operation in a queue [ENQUEUE]:

• 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.

Step 1: ALLOCATE MEMORY FOR newnode


Step 2: DATA(newnode) = element
Step 3: ADDRESS(newnode)=NULL
Step 4: IF front == NULL
Step 5: front = newnode
Step 6: rear = newnode
Step 7: END OF IF
Step 8: ELSE
Step 9: ADDRESS(rear) = newnode
Step 10. rear = ADDRESS(rear)
Step 11: END OF ELSE

• AFTER DELETION OF A QUEUE ELEMENT:

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Algorithm for delettion operation in a queue [DEQUEUE]:

• 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.

Step 1: IF front == NULL


Step 2: PRINT “ Queue is underflow”
Step 3: END OF IF
Step 4: ELSE
Step 6: temp = front
Step 7: front = ADDRESS(front)
Step 8: IF front == NULL THEN
Step 9: rear = NULL
Step 10: END OF IF
Step 11: DEALLOCATE MEMORY FOR temp
Step 12: END OF ELSE

C program of queue using 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;
}

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

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

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

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

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

 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 

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

 ­­­­­­­­­­­**********­­­­­­­­­­­­­
  ..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.

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

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.

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

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

There are two problems associated with linear queue:


• Time consuming: linear time to be spent in shifting the elements to the beginning of the
queue.
• Signaling queue full: even if the queue is having vacant position.

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.

(A) (B) B (C) (D) (E) (F)

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

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.

Algorithm for insertion in Circular Queue [ENQUEUE]:


➢ Here cqueue[ ] is the array to store the queue elements.
➢ ‘front’ is a variable used to represent the front end of the circular queue.
➢ ‘rear’ is a variable to represent the rear end of the circular queue.
➢ ‘element’ is a variable to store the new element to be inputted into the circular queue.
➢ When the value of ‘rear’ or ‘front’ is equal to (size of the queue) -1 then the next value of
‘rear’ or ‘front’ is equal to ((rear or front) + 1)%(size of the queue) i.e 0 other wise it will be
one more than the recent value of ‘rear’ or ‘front’.

Provided By: Shipra Swati, PSCET, Bihar


DS Notes Class: B.Tech. CSE 4th Sem

Step 1: IF front == (rear+1)% (size of cqueue)


Step 2: PRINT “ Queue overflow”
Step 3: END OF IF
Step 4: ELSE
Step 5: rear = (rear+1)%(size of cqueue)
Step 6: IF front == -1
Step 7: front = 0
Step 8: END OF IF
Step 9: cqueue[ rear ] = element
Step 10: END OF ELSE

Algorithm for deletion in Circular Queue [DEQUEUE]:

➢ Here cqueue[ ] is the array to store the queue elements.


➢ ‘front’ is a variable used to represent the front end of the circular queue.
➢ ‘rear’ is a variable to represent the rear end of the circular queue.

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

Provided By: Shipra Swati, PSCET, Bihar

Вам также может понравиться