0% found this document useful (0 votes)
3 views

Queues

This document provides an overview of queues, including their definition, operations (insert and delete), types (circular-queue, dequeue, and priority-queue), and applications in various fields such as operating systems and data management. It also covers memory representation of queues using arrays and the algorithms for inserting and deleting elements. Additionally, the document briefly introduces linked-lists and their variations, along with basic operations on singly linked-lists.

Uploaded by

divya R K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Queues

This document provides an overview of queues, including their definition, operations (insert and delete), types (circular-queue, dequeue, and priority-queue), and applications in various fields such as operating systems and data management. It also covers memory representation of queues using arrays and the algorithms for inserting and deleting elements. Additionally, the document briefly introduces linked-lists and their variations, along with basic operations on singly linked-lists.

Uploaded by

divya R K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Queues

CHAPTER 4: QUEUE

4.1 QUEUE

4.1.1 MEMORY REPRESENTATION OF QUEUES (USING ARRAYS)

4.2 OPERATIONS ON QUEUE

4.2.1 INSERT OPERATION

4.2.2 DELETE OPERATION

4.3 TYPES OF QUEUES (VARIATIONS OF QUEUES)

4.3.1 CIRCULAR-QUEUE

4.3.1.1 MEMORY REPRESENTATION OF CIRCULAR-QUEUES

4.3.1.2 OPERATIONS ON CIRCULAR-QUEUE


4.3.2 DEQUEUE (DOUBLE-ENDED QUEUE)

4.3.3 PRIORITY-QUEUES

4.3 APPLICATIONS OF QUEUES

CHAPTER 4: QUEUE
4.1 QUEUE

Definition

• A queue is a linear data-structure.

• It follows the FIFO (First In, First Out) principle.

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

1) Insert: An element is added at the rear-end.

2) Delete: An element is removed from the front-end.

3) Overflow: Check whether the queue is full or not.

4) Underflow: Check whether the queue is empty or not.

Applications

1) Scheduling processes in operating systems.

2) Handling requests in printers.

3) Managing calls in call centers (customer service).

4.1.1 MEMORY REPRESENTATION OF QUEUES (USING ARRAYS)

• A queue can implement using arrays where:

i) An array holds the queue elements, i.e., `queue[size]`.

ii) Two variables, front and rear, indicate the positions of the frontmost and rearmost elements in
the queue.

• Key Conditions

When front = -1 and rear = -1, the queue is empty.

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.

Figure 4.2: Insert Operation

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.

Figure 4.3: Delete Operation

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;
}

4.2} TYPES OF QUEUES (VARIATIONS OF QUEUES)


4.2.1
} CIRCULAR-QUEUE
Definition
• A circular-queue is a linear data-structure where the last position connects back to the first,
forming a circle.
• It follows the FIFO principle.
Key Features
• Elements are added at the rear and removed from the front.
• The queue is circular, meaning:
When rear reaches the last position, it wraps around to the first position if space is available.
• Memory is used efficiently by reusing empty spaces.
Applications
• Buffer management in computers and real-time systems.
• Handling streaming data like audio or video.

Figure 4.4: Circular-queue

4.2.1.1 MEMORY REPRESENTATION OF CIRCULAR-QUEUES (USING ARRAYS)


• A circular-queue can implement using arrays where:
i) An array holds the queue elements, i.e., `queue[size]`.
ii) Two variables, front and rear, track the positions of the first and last elements in
the queue.
• Key Conditions
When front = -1 and rear = -1, the queue is empty. When
(rear + 1) % size == front, the queue is full.

2.1.2 OPERATIONS ON CIRCULAR-QUEUE INSERT


OPERATION
Definition
• Adds an element to the rear of the circular-queue.
Algorithm
1) Check for Overflow: If (rear + 1) % size == front, the queue is full.
2) Insert the Element: If space is available:
o Increment rear as (rear + 1) % size.
o Insert the new element at queue[rear].
3) Initialize Front (if Needed): If front == -1, set front = 0.

Figure 4.5: Insert Operation Function to Add a

void insert() {
if ((rear + 1) % SIZE == front) {
DELETE OPERATION
Definition
• Removes the front element of the circular-queue.

Figure 4.6: Delete Operation

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.

Figure 4.8: Input-Restricted Dequeue

Figure 4.9: Output-Restricted Dequeue

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

4.3 APPLICATIONS OF QUEUES

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.1 DEFINITION OF A LINKED-LIST


• A linked-list is a linear data-structure that consists of a sequence of nodes.
• Each node has 2 fields:
1) The data portion holds the actual information.
2) The next-pointer points to the next node in the list.

• It dynamically allocates memory to store and organize elements.


• The different types of linked-lists include:
1) Singly Linked-List (SLL)
2) Doubly Linked- List (DLL)
3) Header Linked-List (HLL)
4) Circular Singly Linked-List (CSLL)
• The various operations of linked-lists include:
1) Insertion
2) Deletion
3) Display (Traversing)
4) Searching

5.1.1 MEMORY REPRESENTATION OF A LINKED-LIST


• A linked-list connects nodes using pointers.
• In memory, each node is represented as an individual block of memory, referred to as a "node".
• The node contains 2 fields:
1) The first part stores the data.
2) The second part holds the memory-address of the next node.
• The first-node in the list is called the head-node.
• The last-node points to NULL, indicating the end of the list.

Figure 3.1: Linked-list


• The declaration of a node is given as follows -
struct node
{
int info;
struct node *next;
}
5.2 TYPES OF LINKED-LISTS (VARIATIONS OF LINKED-LISTS)
1) Singly Linked-List (SLL)
• A linked-list consists of nodes, where each node contains 2 fields:
i) Data: It holds the actual value or data.
ii)Next-pointer: It points to the next node in the sequence and stores its memory-
address. The last node points to NULL.
Figure 3.2: Node
• It allows traversal only in one direction, from the head (the first-node) to the tail (the last-
node).
• The head-pointer points to the first node in the linked-list, providing the starting point to access
the list.
• Each node knows only the address of the next node.

Figure 3.3: Singly Linked-list

2) Doubly Linked-List (DLL)


• DLL extends the functionality of SLL.
• It consists of nodes, where each node contains 3 fields:
i) Data: It holds the actual value or data.
ii)Next-pointer: It points to the next node in the sequence and stores its memory-
address. The last node points to NULL.
iii) Previous pointer: It points to the previous node in the sequence and stores its
memory-address. The first node points to NULL.

Figure 3.4: Node


• It allows traversal in both directions, from the head to the tail and vice versa.
• The head-pointer points to the first node in the linked-list, providing the starting point to
access the list.
• Each node knows address of both the previous and next-nodes.

Figure 3.5: Doubly Linked-list


3) Header Linked-List (HLL)
• HLL modifies SLL by adding an extra empty node at the beginning, called the
header-node.
• The header-node does not store any data but serves as a reference point for the list.
• It consists of nodes, where each node contains 2 fields:
i) Data: It holds the actual value or data.
ii)Next-pointer: It points to the next node in the sequence and stores its memory-
address. The last node in the list points to NULL.
• It allows traversal only in one direction, from the head (the first-node) to the tail (the last-
node).
• The head-pointer points to the header-node in the list, providing the starting point to access the
list.
• Each node only knows address of the next-node.

Figure 3.6: Header Linked-list

4) Circular Singly Linked-List (CSLL)


• CSLL extends SLL by modifying the last node to point back to the first node, forming a
circular structure.
• It consists of nodes, where each node contains 2 fields:
i) Data: It holds the actual value or data.
ii)Next-pointer: It points to the next node in the sequence and stores its memory-
address. The last node connects back to the first node.
• It allows traversal only in one direction, from the head (the first-node) to the tail (the last-
node).
• The head-pointer points to the first node in the list, providing the starting point to access the
list.
• Each node knows only the address of the next node.

Figure 3.7: Singly circular linked-list


5.3 OPERATIONS ON SINGLY LINKED-LIST (SLL)
• A SLL 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.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.

5.5 APPLICATIONS OF LINKED-LISTS


1) Dynamic Memory Allocation
• Operating systems use linked-lists for dynamic memory-management, allocating and
deallocating memory at runtime.
2) Implementation of Stacks and Queues
• Linked-lists efficiently implement stacks and queues by allowing dynamic memory allocation
and deallocation.
3) Representation of Graphs
• Graph theory uses adjacency lists, which implement vertices and edges using linked-
lists.
4) Polynomial Arithmetic
• Linked-lists represent and perform arithmetic operations on polynomials, with each node storing
a term's coefficient and exponent.
5) Operating Systems
• Operating systems use linked-lists for process scheduling (e.g., ready queue, wait queue) and
memory-management (e.g., free list, page replacement).
6) Hashing
• Hashing uses linked-lists for collision resolution, where each bucket stores keys with the same
hash value in a linked-list.
7) File Allocation
• File systems use linked-lists to track file blocks non-contiguously, allowing efficient file storage.
8) Undo Operations in Editors
• Text editors use linked-lists to implement undo and redo functionalities by storing each
operation as a node.
9) Music and Image Playlists
• Media players use linked-lists to organize songs, videos, or images, enabling forward
and backward navigation.
10) Sparse Matrix Representation
• Linked-lists store sparse matrices efficiently by keeping only non-zero elements, reducing
memory usage.

You might also like