Concept of Dqueue and Priority Queue

Comprehensive study notes, diagrams, and exam preparation for Concept of Dqueue and Priority Queue.

Concept of Dequeue and Priority Queue

Definition

A Deque (Double Ended Queue) is a linear data structure in which insertion and deletion operations can be performed at both the front and the rear ends.

A Priority Queue is a special type of queue in which each element has a priority value, and deletion occurs based on priority rather than insertion order. If two elements have the same priority, they are usually served according to FIFO order.


Main Content

1. First Concept

Deque (Double Ended Queue)

  • A deque is a generalized form of a queue that supports operations at both ends.
  • It combines the features of both stack and queue:
  • It can behave like a queue when insertion is done at one end and deletion at the other.
  • It can behave like a stack when insertion and deletion are done at the same end.

Types of Deque

Input-restricted deque

  • : insertion is allowed at only one end, but deletion is allowed at both ends.

Output-restricted deque

  • : deletion is allowed at only one end, but insertion is allowed at both ends.

Example

If elements are inserted and removed from both ends, the structure may look like this:

Front <--- 10 20 30 40 ---> Rear

Operations such as:

  • Insert 5 at front → 5 10 20 30 40
  • Insert 50 at rear → 5 10 20 30 40 50
  • Delete from front → 10 20 30 40 50
  • Delete from rear → 10 20 30 40

Characteristics

  • More flexible than a normal queue.
  • Useful for problems where both ends need access.
  • Can be implemented using arrays or doubly linked lists.

2. Second Concept

Priority Queue

  • A priority queue stores elements along with their priority.
  • The element with the highest priority is removed first.
  • Priority may be:
  • Max-priority: largest key has the highest priority.
  • Min-priority: smallest key has the highest priority.

Example

Suppose the following items are inserted:

Element Priority
A 2
B 5
C 1
D 4

In a max-priority queue, B is removed first because it has the highest priority value.

Real-world analogy

A hospital emergency ward is a good example:

  • A patient with a serious condition is attended before a patient with a minor issue.
  • Arrival time matters only when priority is the same.

Characteristics

  • Based on priority, not insertion order.
  • Can be implemented using arrays, linked lists, heaps, or trees.
  • Essential in scheduling and resource management.

3. Third Concept

Differences Between Deque and Priority Queue

  • A deque is based on position of insertion/deletion at ends.
  • A priority queue is based on priority value attached to elements.
  • Deque is mainly used when both ends are needed for operations.
  • Priority queue is used when the most important item must be processed first.

Comparison Table

Feature Deque Priority Queue
Insertion Front and/or rear Based on implementation, usually at the rear or according to priority maintenance
Deletion Front and/or rear Highest or lowest priority element
Ordering Principle End-based access Priority-based access
Flexibility High High, but governed by priority
Common Use Sliding window, palindrome checking, undo/redo CPU scheduling, shortest path, event simulation

Similarity with other structures

  • Deque is related to both stack and queue.
  • Priority queue is related to queue but with an added ordering rule.
  • Both are abstract data structures that can be implemented in multiple ways.

Implementation Notes

  • Deque is efficiently implemented using a doubly linked list or a circular array.
  • Priority queue is efficiently implemented using a binary heap.

Working / Process

1. Initialization

  • For a deque, create an empty structure with front and rear pointers or indices.
  • For a priority queue, prepare the structure with an associated priority field for each element.

2. Insertion

  • In a deque, insert elements at the front or rear depending on the requirement.
  • In a priority queue, insert the element and place it according to its priority or maintain heap order.

3. Deletion / Removal

  • In a deque, remove elements from the front or rear as required.
  • In a priority queue, remove the element with the highest priority in a max-priority queue, or the lowest priority in a min-priority queue.

Working of Deque

Initial:
Front -> [20, 30, 40] <- Rear

Insert 10 at Front:
Front -> [10, 20, 30, 40] <- Rear

Insert 50 at Rear:
Front -> [10, 20, 30, 40, 50] <- Rear

Delete from Front:
Front -> [20, 30, 40, 50] <- Rear

Working of Priority Queue

Items inserted with priorities:
(A,2), (B,5), (C,1), (D,4)

Arrangement by priority (highest first):
(B,5) -> (D,4) -> (A,2) -> (C,1)

Deletion order:
B, D, A, C

Important operational idea

  • In deque, both ends are active.
  • In priority queue, only the element with the most important priority is active for deletion.

Advantages / Applications

Deque Advantages

  • Very flexible because insertion and deletion are possible from both ends.
  • Useful in algorithms like palindrome checking, undo/redo systems, and sliding window problems.
  • Can efficiently model real-world situations where items need access from either side.

Priority Queue Advantages

  • Processes the most important item first, which is ideal for scheduling and emergency handling.
  • Helps in efficient algorithm design, especially in graph algorithms and task management.
  • Supports dynamic ordering based on importance rather than arrival time.

Applications

  • Deque:
    • Browser history navigation
    • Palindrome checking
    • Sliding window maximum/minimum
    • Task scheduling with front/rear insertion needs
  • Priority Queue:
    • CPU scheduling
    • Printer job management
    • Dijkstra’s shortest path algorithm
    • Event-driven simulation
    • Bandwidth and packet scheduling in networks

Summary

  • Deque is a queue where insertion and deletion can happen at both ends.
  • Priority queue removes elements according to priority, not arrival order.
  • These structures are useful in many real-world and algorithmic problems.
  • Important terms to remember: Deque, Double Ended Queue, Input-Restricted Deque, Output-Restricted Deque, Priority Queue, Max-Priority Queue, Min-Priority Queue, FIFO, Priority