Write a short note on queue and priority queue. क्यू और प्राथमिकता क्यू पर एक संक्षिप्त लेख लिखिए.


Q.) Write a short note on queue and priority queue. क्यू और प्राथमिकता क्यू पर एक संक्षिप्त लेख लिखिए.

Subject: Data Structures

Queue

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. In a queue, elements are added from one end, called the rear or tail, and removed from the other end, called the front or head. This means that the first element added to the queue is the first element to be removed.

Queues are often implemented using an array or a linked list. In an array implementation, the front and rear of the queue are tracked using two pointers. The front pointer points to the first element in the queue, and the rear pointer points to the last element in the queue. When an element is added to the queue, the rear pointer is incremented, and the element is added to the array at the position pointed to by the rear pointer. When an element is removed from the queue, the front pointer is incremented, and the element at the position pointed to by the front pointer is removed from the array.

In a linked list implementation, a queue is represented as a linked list, where each node contains an element and a pointer to the next node. The front of the queue is the head of the linked list, and the rear of the queue is the tail of the linked list. When an element is added to the queue, a new node is created and added to the end of the linked list. When an element is removed from the queue, the head of the linked list is removed.

Queues have a number of applications, including:

  • Scheduling: Queues are used to schedule tasks in a computer system. The tasks are added to the queue in order of priority, and the operating system removes tasks from the queue and executes them in the order in which they were added.
  • Communication: Queues are used to communicate between different parts of a computer system. For example, a queue can be used to pass data from a producer process to a consumer process.
  • Data storage: Queues can be used to store data temporarily. For example, a queue can be used to buffer data that is being read from a disk or written to a disk.

Priority Queue

A priority queue is a queue in which elements are assigned a priority. When an element is added to a priority queue, it is placed in the queue in order of priority, with the highest priority elements at the front of the queue. When an element is removed from a priority queue, the highest priority element is always removed.

Priority queues are often implemented using a binary heap. A binary heap is a complete binary tree, where each node contains an element and a priority. The highest priority element is always stored at the root of the tree. When an element is added to a priority queue, it is inserted at the bottom of the tree and then bubbled up the tree until it reaches its correct position, which is the position of the smallest priority element in the subtree rooted at the node. When an element is removed from a priority queue, the element at the root of the tree is removed and the last element in the tree is moved to the root and then bubbled down the tree until it reaches its correct position.

Priority queues have a number of applications, including:

  • Scheduling: Priority queues are used to schedule tasks in a computer system, with the highest priority tasks being executed first.
  • Resource allocation: Priority queues are used to allocate resources to different processes, with the highest priority processes being allocated resources first.
  • Event handling: Priority queues are used to handle events in a computer system, with the highest priority events being handled first.