CONTENTS What is queue ? Basic operation Enqueue Dequeue Types of Queue Linear queue Circular queue Double ended queue Priority queue Applications of queue
QUEUE :- Queue is the linear data structure type which is used to organize the data. It is used for temporary storage of data values. A new element is added at one end called rear end. The existing element deleted from the other end is called front end. First-in-First-out property.
Basic operations Enqueue(): By this, we add the element to the rear end of the queue. The elements will be added after the previous element and this process will continue. Dequeue(): It is the opposite of enqueue this is used to remove the value from the front end of the queue it also returns the removed value. isEmpty(): This is a boolean operation that sees whether the queue is empty or not. If the queue is empty it will return true and if the queue is not empty it will return false. isFull (): This is also a boolean operation that results true or false depending on the size of the queue if the size of the queue is full it will return true otherwise it will return false. peek(): It will give the value of the top element of the queue or you can say that the element of the front of the queue.
Algorithm of enqueue 1 − START 2 – Check if the queue is full. 3 − If the queue is full, produce overflow error and exit. 4 − If the queue is not full, increment rear pointer to point the next empty space. 5 − Add data element to the queue location, where the rear is pointing. 6 − return success. 7 – END
Example of Enqueue operations struct Queue { int items[MAX_SIZE]; int front, rear; }; // Function to enqueue an element void enqueue(struct Queue *q, int value) { if (q->rear == MAX_SIZE - 1) { printf("Queue is full. Cannot enqueue.\n"); } else { q->rear++; q->items[q->rear] = value; printf("Enqueued %d\n", value); } }
ALGORITHM OF Dequeue START Check if the queue is empty. If the queue is empty, produce underflow error and exit. If the queue is not empty, access the data where front is pointing. Increment front pointer to point to the next available data element. 6 Return success. 7 END
Example of Dequeue void dequeueFront(struct Dequeue * dq ) { if (isEmpty( dq )) { printf("Queue is empty. Cannot dequeue from front.\n"); return; } if ( dq ->front == dq ->rear) { initialize( dq ); } else if ( dq ->front == MAX_SIZE - 1) { dq ->front = 0; } else { dq ->front++; } }
1. LINEAR QUEUE Linear 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 In a circular queue, all nodes are treated as circular. Last node is connected back to the first node. Circular queue is also called as Ring Buffer. Circular queue contains a collection of data which allows insertion of data at the end of the queue and deletion of data at the beginning of the queue
3. Double ended Queue:- In Double Ended Queue, insert and delete operation can be occur at both ends that is front and rear of the queue
4. Priority queue A priority queue is one of the types of queue in data structure in which each element is assigned a priority and the elements are served in order of priority. This means that the highest priority element is served first and the lowest priority element is served last.
Application of Queue:- Task scheduling and prioritization (e.g. printing, CPU task scheduling) Communication systems (e.g. email, messaging) Call centers and customer service Traffic management (e.g. in networking) Resource allocation (e.g. allocating resources in operating systems)