Queues
Queues
CHAPTER 4: QUEUE
4.1 QUEUE
4.3.1 CIRCULAR-QUEUE
4.3.3 PRIORITY-QUEUES
CHAPTER 4: QUEUE
4.1 QUEUE
Definition
Key Features
• Elements are added at one end called the rear (or tail).
• Elements are removed from the other end called the front (or head).
Operations
Applications
ii) Two variables, front and rear, indicate the positions of the frontmost and rearmost elements in
the queue.
• Key Conditions
When rear = size - 1 and front = 0, the queue is full (in a simple queue).
DATA STRUCTURES AND APPLICATIONS
4.1 OPERATIONS ON QUEUE
4.1.1 INSERT OPERATION
Definition
• Adds an element to the rear of the queue.
Algorithm
1) Check for Overflow: Verify if the queue is full (rear == size - 1).
2) Insert the Element: If space is available:
o Increment rear by 1.
o Insert the new element at queue[rear].
Function to Add an Element to the Queue
void insert() {
if (rear == SIZE - 1) {
cout << "Queue Overflow" << endl;
} else {
rear = rear + 1;
cout << "Enter the element to be inserted: ";
cin >> queue[rear];
}
if (front == -1) {
front = 0;
}
}
QUEUE
4-1
Definition
• Removes the front element of the queue.
Algorithm
1) Check for Underflow: Verify if the queue is empty (front == -1 or front > rear).
2) Remove the Element: If elements are present:
o Remove the value at queue[front].
o Increment front by 1.
3) Reset Queue (if Empty): If front == rear, reset both front and rear to -1.
Function to delete an element from the queue
void DeleteQ() {
if (front == -1) {
cout << "Queue Underflow" << endl;
} else {
cout << "The element deleted is " << queue[front] << endl; if (front ==
rear) {
front = -1;
rear = -1;
} else {
front = front + 1;
}
void insert() {
if ((rear + 1) % SIZE == front) {
DELETE OPERATION
Definition
• Removes the front element of the circular-queue.
Algorithm
1) Check for Underflow: If front == -1, the queue is empty.
2) Remove the Element: If elements are present:
o Remove the value at queue[front].
o Increment front as (front + 1) % size.
3) Reset Queue (if Empty): If front == rear, reset both front and rear to -1.
Function to Delete an Element from the Circular-queue
void DeleteQ() {
if (front == -1) {
cout << "Queue Underflow" << endl;
} else {
cout << "The element deleted is " << queue[front] << endl; if (front ==
rear) {
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
Illustration Of Insert Operation And Delete Operation
}
}
4.2.2 DEQUEUE (DOUBLE-ENDED QUEUE)
Definition
• A dequeue is a type of queue where elements can be added or removed from both ends (front and
rear).
Figure 4.7: Dequeue
Key Features
• It is not restricted to the FIFO principle.
• Supports insertion and deletion at both ends of the queue.
Types of Dequeues
1) Input-Restricted Dequeue: Insertion is allowed only at one end, but deletion can be done
from both ends.
2) Output-Restricted Dequeue: Deletion is allowed only at one end, but insertion can be done from
both ends.
Applications
• Used in implementing sliding window algorithms.
• Helpful in managing tasks where operations at both ends are required, such as undo functionality.
4.2.3 PRIORITY-QUEUES
Definition
• A priority-queue stores elements based on their priority instead of their arrival order.
Key Features
• Elements with higher priority are processed before elements with lower priority.
• If two elements have the same priority, they are processed based on their order of arrival
(FIFO).
Operations
1) Insert: Add an element to the queue along with its priority.
2) Delete: Remove the element with the highest priority.
Types
1) Max-Priority-queue: Processes elements with the highest priority first.
2) Min-Priority-queue: Processes elements with the lowest priority first.
Applications
• Scheduling tasks in operating systems (e.g., job scheduling).
• Managing data in simulations and network routing.
Figure 4.10: Min-Priority-queue Figure 4.11: Max-Priority-queue
Job Scheduling
• Operating systems use queues to manage tasks or processes based on their priority or arrival
time.
CPU Task Management
• Multiple processes waiting for CPU time are stored in a queue.
• The scheduler allocates CPU to these processes in a specific order, like FCFS
Printer Task Management
• When multiple print requests are made, they are stored in a queue.
• The printer processes them one by one in the order they were received.
Data Buffering
• Queues are used in buffering applications like handling input/output devices or managing
data in network communications.
Call Centers
• Customer calls waiting to be attended are placed in a queue.
• They are answered based on the order of their arrival.
Breadth-First Search (BFS)
• Queues are used in BFS algorithms in graph traversal to keep track of the next node to visit.
Traffic Management
• Queues are used to simulate and analyze traffic systems, like cars waiting at a traffic
signal.
Messaging Systems
• Queues are used in asynchronous messaging systems, where messages are stored temporarily
until the receiver processes them.
Shared Resources
• Multi-user systems use queues to manage access to shared resources, such as printers or
databases.
Simulation Systems
• Queues are used in simulation models, such as customer queues in banks or service center
CHAPTER 5: LINKED-LIST
5.3.1 ALGORITHM FOR INSERTING & DELETING AT THE BEGINNING OF A SLL AND
DISPLAYING THE NODES
Algorithm: Inserting at the beginning
1) Create a New Node: Create a new node with the given data.
2) Link the New Node to the Current Head: Set the next-pointer of the new node to
point to the current head.
3) Update the Head-pointer: Update the head-pointer to point to the new node.
Algorithm: Deleting at the beginning
1) Check if the List is Empty: If the list is empty (head is NULL), return as there is nothing
to delete.
2) Create a Temporary-pointer: Create a temporary-pointer pointing to the head-node.
3) Update the Head-pointer: Update the head-pointer to point to the next node.
4) Free the Memory: Free the memory allocated for the temporary-pointer.
Algorithm: Displaying the nodes in a list
1) Initialize a Temporary-pointer: Set a temporary-pointer to point to the head-
node.
2) Iterate Through the List: While the temporary-pointer is not NULL:
i) Print the Node Value: Print the value stored in the current-node.
ii) Move to the Next Node: Update the temporary-pointer to point to the next node.
3) End the Traversal: Stop when the temporary-pointer becomes NULL.
5.3.2 ALGORITHM FOR INSERTING & DELETING AT THE END OF A SLL AND DISPLAYING THE
NODES
Algorithm: Inserting at the End
1) Create a New Node: Create a new node with the given data.
2) Check if List is Empty: If the head is NULL (the list is empty), set the head to point to
the new node and return.
3) Traverse the List: Start from the head and move through the list until reaching
the last node (the node whose next-pointer is NULL).
4) Link the Last Node to New Node: Set the next-pointer of the last node to point to
the new node.
5) Mark New Node as Last: Set the next-pointer of the new node to NULL, indicating
that it is now the last node.
Algorithm: Deleting at the End
1) Check if List is Empty: If the head is NULL (the list is empty), return as there is nothing
to delete.
2) Check if List has Only One Node: If the head is the only node in the list (head->next is
NULL), free the memory allocated for the head-node, set head to NULL, and return.
3) Find the Second Last Node: Traverse the list starting from the head until reaching the
second last node (the node whose next-pointer points to the last node).
4) Store the Last Node Address: Store the address of the last node in a temporary-pointer.
5) Unlink the Last Node: Set the next-pointer of the second last node to NULL, indicating
that it is now the last node.
6) Delete the Last Node: Free the memory allocated for the last node using the temporary-
pointer.
Algorithm: Displaying the nodes in a list
• Explanation same as in 5.3.1
5.3.3 ALGORITHM FOR INSERTING & DELETING AT A SPECIFIC POSITION OF A SLL AND
DISPLAYING THE NODES
Algorithm: Inserting a Node after a Given Node
1) Create a New Node: Create a new node with the given data.
2) Check if the List is Empty: If the head is NULL (the list is empty), set the head to
point to the new node and return.
3) Find the Insertion Point: Traverse the list to locate the node after which the new node
should be inserted.
4) Link the New Node to the Next Node: Set the next-pointer of the new node to point to
the next node of the given node.
5) Update the Previous Node’s Pointer: Update the next-pointer of the given node to
point to the new node.
Algorithm: Deleting a Specific Node
1) Check if the List is Empty: If the head is NULL (the list is empty), return as there is
nothing to delete.
2) Check if the Head is the Only Node: If the head is the only node in the list (head->next
is NULL), free the memory allocated for the head-node, set head to NULL, and return.
3) Find the Node to Delete: Traverse the list to locate the node containing the specified
element.
4) Identify the Previous Node: Create a temporary-pointer pointing to the node just before
the node to be deleted.
5) Update the Links: Set the next-pointer of the temporary node to point to the next node of
the node to be deleted.
6) Delete the Node: Free the memory allocated for the node to be deleted.
Algorithm: Displaying the nodes in a list
• Explanation same as in 5.3.1
5.3.4 ALGORITHM FOR SEARCHING IN A SLL
Algorithm: Searching in a Singly Linked-list
1) Start at the Head-node: Begin at the head-node of the linked-list.
2) Initialize Pointer Variable: Initialize a pointer-variable, let's call it "current," to point to
the head-node.
3) Traverse the List: While the current-node is not NULL, do the following:
i) Check for Match: Check if the value stored in the current-node matches
the target value being searched for.
If it does, return the current-node or indicate that the value is found.
ii) Move to Next Node: If the value does not match, move the current- pointer to the
next node in the list.
4) End of List: If the loop ends without finding the target value and the current- node
becomes NULL, the value is not present in the list.
5.4 OPERATIONS ON DOUBLY LINKED-LIST (DLL)
• A DLL supports the following main operations:
1) Insertion
2) Deletion
3) Display (Traversing)
4) Searching
• The insertion can be done in the following ways:
i) Insertion at the beginning
ii) Insertion at the end
iii) Insertion at a specific position
• The deletion can be done in the following ways:
i) Deleting the first node
ii) Deleting the last node
iii) Deleting a specific node
5.4.1 ALGORITHM FOR INSERTING & DELETING AT THE BEGINNING OF A DLL AND
DISPLAYING THE NODES
Algorithm: Inserting at the beginning
1) Create New Node: Create a new node with the given data.
2) Update Next-pointer: Set the next-pointer of the new node to point to the current
head.
3) Set Previous Pointer: Set the previous-pointer of the new node to NULL.
4) Update Previous Pointer of Head: If the current head is not NULL, set its previous-
pointer to point to the new node.
5) Update Head-pointer: Update the head-pointer to point to the new node.
Algorithm: Deleting at the beginning
1) Check if List is Empty: If the list is empty (head is NULL), return as there is nothing to
delete.
2) Create Temporary-pointer: Create a temporary-pointer pointing to the head- node.
3) Update Head-pointer: Update the head-pointer to point to the next-node.
4) Update Previous Pointer of New Head: If the new head is not NULL, set its previous-
pointer to NULL.
5) Free Memory: Free the memory allocated for the temporary-pointer.
Algorithm: Displaying the nodes in a list
1) Initialize Temporary-pointer: Set a temporary-pointer to point to the head- node.
2) Traverse the List: While the temporary-pointer is not NULL:
i) Print the value stored in the current-node.
ii) Move the temporary-pointer to the next-node.
3) End the Loop: End the loop when the temporary-pointer reaches NULL.