0% found this document useful (0 votes)
12 views36 pages

Unit I

The document outlines a course on Data Structures (CS23231) focusing on linear and nonlinear data structures, including linked lists, stacks, queues, trees, and graphs, along with sorting and hashing techniques. It details course objectives, unit topics, suggested activities, evaluation methods, and textbooks. Additionally, it covers the importance of data structures, their classifications, and dynamic memory allocation techniques, providing insights into various data structures and their applications.

Uploaded by

archuanisha
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)
12 views36 pages

Unit I

The document outlines a course on Data Structures (CS23231) focusing on linear and nonlinear data structures, including linked lists, stacks, queues, trees, and graphs, along with sorting and hashing techniques. It details course objectives, unit topics, suggested activities, evaluation methods, and textbooks. Additionally, it covers the importance of data structures, their classifications, and dynamic memory allocation techniques, providing insights into various data structures and their applications.

Uploaded by

archuanisha
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/ 36

CS23231 DATA STRUCTURES

Objectives:
1.​ To apply the concepts of Linked List in the applications of various linear data
structures.
2.​ To demonstrate the understanding of stacks, queues and their applications.
3.​ To apply the concepts of Linked List in the applications of various nonlinear data
structures.
4.​ To understand the implementation of graphs and their applications.
5.​ To be able to incorporate various sorting and hashing techniques in real time scenarios
UNIT-I LINEAR DATA STRUCTURE – LIST
Self-Referential Structures, Dynamic Memory Allocation, Linked list implementation -
Singly Linked List, Doubly Linked List, Circular Linked List, Applications of List.
UNIT-II LINEAR DATA STRUCTURE –STACK AND QUEUE
Stack – Operations, Array and Linked list implementation, Applications – Evaluation of
Arithmetic Expressions, Queues- Operations, Array and Linked list Implementation.
UNIT-III NONLINEAR DATA STRUCTURE –TREES
Tree Terminologies, Binary Tree Representation, Tree Traversals, Binary Search Trees,
Binary Heap, Height Balance Trees – AVL Trees.
UNIT-IV NONLINEAR DATA STRUCTURE –GRAPH
Representation of Graphs, Topological Sort, Depth First Search and Breadth-First Search,
Minimum Spanning Tree – Prim's Algorithm, Shortest path algorithm – Dijikstra’s
Algorithm.
UNIT-V SORTING AND HASHING
Sorting Techniques –Insertion Sort, Quick Sort, Merge Sort, Hashing- Hashing functions –
Mid square, Division, Folding, Collision Resolution Techniques – Separate Chaining – Open
Addressing – Rehashing.
Course Outcomes:
On completion of the course, the students will be able to
1.​ Understand and apply the various concepts of Linear data Structures
2.​ Understand and apply the various concepts of Non Linear data Structures.
3.​ Understand and apply the various sorting and hashing concepts.
4.​ Analyse and apply the suitable data structure for their research.
5.​ Choose efficient data structures and apply them to solve real world problems.
SUGGESTED ACTIVITIES
●​ Role play- Linked List (Unit 1).
●​ Mind Map, Poster Design - Stack and Queue (Unit 2).
●​ Flipped Classroom - Binary Heap (Unit 3).
●​ Poster Design - Graph (Unit4).
●​ Implementation of small module- Hashing (Unit5).
SUGGESTED EVALUATION METHODS
●​ Assignment problems - Linked List (Unit 1).
●​ Tutorial problems - Applications – Evaluation of Arithmetic Expressions (Unit 2).
●​ Quizzes - BST and Binary Heap (Unit 3).
●​ Tutorial problems- Graph traversal (Unit 4).
●​ Quizzes - Hashing and Sorting(Unit5) .
Text Books(s):
1 “Data Structures and Algorithm Analysis in C”, Mark Allen Weiss, 2nd Edition, Pearson
Education, 2005
2 “Data Structures and Algorithm Analysis in C++ - Anna University, Mark Allen Weiss,
Pearson Education, 2017.
Reference Books:
1 “Data Structures Using C and C++”, Langsam, Augenstein and Tanenbaum, 2nd Edition,
Pearson Education, 2015.
2 Thomas H. Cormen, Charles E. Leiserson, Ronald L.Rivest, Clifford Stein, Introduction to
Algorithms”, Fourth Edition, Mcgraw Hill/ MIT Press, 2022.

Description of Experiments (If applicable)


1 Implementation of Single Linked List (Insertion, Deletion and Display).
2 Implementation of Doubly Linked List (Insertion, Deletion and Display).
3 Implementation of Stack using Array and Linked List implementation.
4 Implementation of Queue using Array and Linked List implementation.
5 Implementation of Binary Search Tree and perform Tree Traversal Techniques.
6 Program to perform Quick Sort
7 Program to perform Merge Sort
8 Program to perform Linear Probing.
9 Program to perform Rehashing.
10 Mini Project:
Contact book application using Linked List.
1.​ Dictionary using Binary search trees.
2.​ Snake Game.
3.​ Chess Game.
4.​ Travel Planner (Shortest Path Algorithm).
5.​ Tic-Tac-Toe Game.
6.​ Library Management System.
7.​ Project Management System.
8.​ Other projects.
Web links for Theory & Lab
1 Data Structures - GeeksforGeeks
2 Data Structures | DS Tutorial - javatpoint
3 Data Structure and Types (programiz.com)
CO PO Mapping

Correlationlevels1, 2 or 3 are as defined below:


1: Slight (Low) 2: Moderate (Medium) 3: Substantial (High)
No correlation: “-”
Data Structure
A data structure is a particular way of organising data in a computer so that it can be
used effectively. The idea is to reduce the space and time complexities of different tasks.
Need of Data Structure
The structure of the data and the synthesis of the algorithm are relative to each other.
Data presentation must be easy to understand so the developer, as well as the user, can make
an efficient implementation of the operation. Data structures provide an easy way of
organising, retrieving, managing, and storing data. Here is a list of the needs for data.
●​ Data structure modification is easy.
●​ It requires less time.
●​ Save storage memory space.
●​ Data representation is easy.
●​ Easy access to the large database
Classification/Types of Data Structures:
1.​ Linear Data Structure
2.​ Non-Linear Data Structure.
Linear Data Structure:
●​ Elements are arranged in one dimension, also known as linear dimension.
●​ Example: lists, stack, queue, etc.
Non-Linear Data Structure
●​ Elements are arranged in one-many, many-one and many-many dimensions.
●​ Example: tree, graph, table, etc.
Most Popular Data Structures:
1. Array:
An array is a collection of data items stored at contiguous memory locations. The idea
is to store multiple items of the same type together. This makes it easier to calculate the
position of each element by simply adding an offset to a base value, i.e., the memory location
of the first element of the array (generally denoted by the name of the array).

2. Linked Lists:
Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements
are not stored at a contiguous location; the elements are linked using pointers.
3. Stack:
Stack is a linear data structure which follows a particular order in which the
operations are performed. The order may be LIFO (Last In First Out) or FILO (First In Last
Out). In stack, all insertion and deletion are permitted at only one end of the list.

Stack Operations:
1.​ push(): When this operation is performed, an element is inserted into the stack.
2.​ pop(): When this operation is performed, an element is removed from the top of the
stack and is returned.
3.​ top(): This operation will return the last inserted element that is at the top without
removing it.
4.​ size(): This operation will return the size of the stack i.e. the total number of elements
present in the stack.
5.​ isEmpty(): This operation indicates whether the stack is empty or not.
4. Queue:
Like Stack, Queue is a linear structure which follows a particular order in which the
operations are performed. The order is First in First out (FIFO). In the queue, items are
inserted at one end and deleted from the other end. A good example of the queue is any queue
of consumers for a resource where the consumer that came first is served first. The difference
between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.
Queue Operations:
1.​ Enqueue(): Adds (or stores) an element to the end of the queue..
2.​ Dequeue(): Removal of elements from the queue.
3.​ Peek() or front(): Acquires the data element available at the front node of the
queue without deleting it.
4.​ rear(): This operation returns the element at the rear end without removing it.
5.​ isFull(): Validates if the queue is full.
6.​ isNull(): Checks if the queue is empty.
5. Binary Tree:
Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees
are hierarchical data structures. A binary tree is a tree data structure in which each node has at
most two children, which are referred to as the left child and the right child. It is implemented
mainly using Links. A Binary Tree is represented by a pointer to the topmost node in the tree.
If the tree is empty, then the value of root is NULL. A Binary Tree node contains the
following parts.
1.​ Data
2.​ Pointer to left child
3.​ Pointer to the right child
6. Binary Search Tree:
A Binary Search Tree is a Binary Tree following the additional properties:
●​ The left part of the root node contains keys less than the root node key.
●​ The right part of the root node contains keys greater than the root node key.
●​ There is no duplicate key present in the binary tree.
●​ A Binary tree having the following properties is known as Binary search tree
(BST).

7.Heap:
A Heap is a special Tree-based data structure in which the tree is a complete binary
tree. Generally, Heaps can be of two types:
Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys
present at all of its children. The same property must be recursively true for all sub-trees in
that Binary Tree.
Min-Heap: In a Min-Heap the key present at the root node must be minimum among the
keys present at all of its children. The same property must be recursively true for all sub-trees
in that Binary Tree.
8. Hash Table Data Structure
Hashing is an important Data Structure which is designed to use a special function
called the Hash function which is used to map a given value with a particular key for faster
access of elements. The efficiency of mapping depends on the efficiency of the hash function
used. Let a hash function H(x) maps the value x at the index x%10 in an Array. For example,
if the list of values is [11, 12, 13, 14, 15] it will be stored at positions {1, 2, 3, 4, 5} in the
array or Hash table respectively.

9. Matrix:
A matrix represents a collection of numbers arranged in an order of rows and
columns. It is necessary to enclose the elements of a matrix in parentheses or brackets.
A matrix with 9 elements is shown below.
10. Trie
Trie is an efficient information retrieval data structure. Using Trie, search
complexities can be brought to an optimal limit (key length). If we store keys in the binary
search tree, a well-balanced BST will need time proportional to M * log N, where M is
maximum string length and N is the number of keys in the tree. Using Trie, we can search the
key in O(M) time. However, the penalty is on Trie storage requirements.

11. Graph:
Graph is a data structure that consists of a collection of nodes (vertices) connected by
edges. Graphs are used to represent relationships between objects and are widely used in
computer science, mathematics, and other fields. Graphs can be used to model a wide variety
of real-world systems, such as social networks, transportation networks, and computer
networks.

Applications of Data Structures:


Data structures are used in various fields such as:
●​ Operating system
●​ Graphics
●​ Computer Design
●​ Block chain
●​ Genetics
●​ Image Processing
●​ Simulation, etc.
Static and Dynamic Data Structures
There are two main categories of data structures: static and dynamic. Static data
structures have a fixed size and are allocated in memory during compile-time,
while dynamic data structures can grow and shrink in size during runtime.
Self-Referential Structures
Self-Referential structures are those structures that have one or more pointers which
point to the same type of structure, as their member. In other words, structures pointing to the
same type of structures are self-referential in nature.
Dynamic memory allocation in Data Structure
Introduction:
Dynamic memory allocation is a fundamental concept in data structures and
programming. It allows programs to allocate memory at runtime, providing flexibility and
efficiency when working with data structures of varying sizes.
Understanding Dynamic Memory Allocation
In most programming languages, including C++, memory can be classified into two
categories: stack memory and heap memory. Local variables and function calls are stored in
the stack memory, whereas the more adaptable heap memory can be allocated and released at
runtime. The process of allocating and releasing memory from the heap is known as dynamic
memory allocation. It allows the programmer to manage memory explicitly, providing the
ability to create data structures of varying sizes and adjust memory requirements
dynamically.
Dynamic Memory Allocation Techniques:
In most programming languages, dynamic memory allocation is achieved through
dedicated functions provided by the language or standard library. Three commonly used
techniques are
1. malloc:
The malloc (memory allocation) function is used to allocate a specified number of
bytes in memory. When memory is allocated successfully, it returns a pointer to that block,
otherwise it returns NULL. By dividing the necessary number of elements by each one's
individual size, the block's size is calculated. For example:
1.​ int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
2. calloc:
Resizing a previously allocated memory block is possible with the realloc (reallocate)
function. It accepts two arguments: the new size in bytes and a pointer to the current block.
Upon successful reallocation, a pointer to the newly allocated memory is returned. If not,
NULL is returned. Here's an example:
1.​ int* ptr = (int*) calloc(5, sizeof(int)); // Allocates memory for an array of 5 integers
3. realloc:
The realloc (reallocate) function allows you to resize a previously allocated memory
block. It takes a pointer to the existing block and the new size in bytes as arguments. If
reallocation is successful, it returns a pointer to the reallocated memory. Otherwise, it returns
NULL. Example usage:
1.​ int* ptr = (int*) malloc(5 * sizeof(int)); // Allocates memory for 5 integers
2.​ ptr = (int*) realloc(ptr, 10 * sizeof(int)); // Reallocates memory for 10 integers
Singly linked list
A singly linked list is a fundamental data structure, it consists of nodes where each
node contains a data field and a reference to the next node in the linked list. The next of the
last node is null, indicating the end of the list. Linked Lists support efficient insertion and
deletion operations.
Understanding Node Structure
In a singly linked list, each node consists of two parts: data and a pointer to the next
node. This structure allows nodes to be dynamically linked together, forming a chain-like
sequence.

Operations on Singly Linked List


Insertion:
o​ Insert at the beginning
o​ Insert at the end
o​ Insert at a specific position
Deletion:
o​ Delete from the beginning
o​ Delete from the end
o​ Delete a specific node
Insertion in Singly Linked List
Insertion is a fundamental operation in linked lists that involves adding a new node to
the list. There are several scenarios for insertion:
Insertion at the Beginning of Singly Linked List:

Insert a Node at the Front/Beginning of Linked List


Step-by-step approach:
●​ Create a new node with the given value.
●​ Set the next pointer of the new node to the current head.
●​ Move the head to point to the new node.
●​ Return the new head of the linked list.

Insertion at the End of Singly Linked List:


To insert a node at the end of the list, traverse the list until the last node is reached,
and then link the new node to the current last node-
Step-by-step approach:
●​ Create a new node with the given value.
●​ Check if the list is empty:
o​ If it is, make the new node the head and return.
●​ Traverse the list until the last node is reached.
●​ Link the new node to the current last node by setting the last node's next pointer to the
new node.
Insertion at a Specific Position of the Singly Linked List
To insert a node at a specific position, traverse the list to the desired position, link the
new node to the next node, and update the links accordingly.

We mainly find the node after which we need to insert the new node. If we encounter
a NULL before reaching that node, it means that the given position is invalid. Below is the
function for insertion at a specific position of the singly linked list:
Deletion at the Beginning of Singly Linked List:
To delete the first node, update the head to point to the second node in the list.
Steps-by-step approach:
1.​ Check if the head is NULL.
2.​ If it is, return NULL (the list is empty).
3.​ Store the current head node in a temporary variable temp.
4.​ Move the head pointer to the next node.
5.​ Delete the temporary node.
6.​ Return the new head of the linked list.

Below is the function for deletion at the beginning of singly linked list:
Deletion at the End of Singly Linked List:
To delete the last node, traverse the list until the second-to-last node and update its
next field to none.

Step-by-step approach:
1.​ Check if the head is NULL.
2.​ If it is, return NULL (the list is empty).
3.​ Check if the head's next is NULL (only one node in the list).
4.​ If true, delete the head and return NULL.
5.​ Traverse the list to find the second last node (second_last).
6.​ Delete the last node (the node after second_last).
7.​ Set the next pointer of the second last node to NULL.
8.​ Return the head of the linked list.
Below is the function for deletion at the end of singly linked list:
Deletion at a Specific Position of Singly Linked List
To delete a node at a specific position, traverse the list to the desired position, update
the links to bypass the node to be deleted.

Step-by-step approach:
1.​ Check if the list is empty or the position is invalid, return if so.
2.​ If the head needs to be deleted, update the head and delete the node.
3.​ Traverse to the node before the position to be deleted.
4.​ If the position is out of range, return.
5.​ Store the node to be deleted.
6.​ Update the links to bypass the node.
7.​ Delete the stored node.
Below is the function for deletion at a specific position of singly linked list:
Doubly Linked List
A doubly linked list is a more complex data structure than a singly linked list, but it
offers several advantages. The main advantage of a doubly linked list is that it allows for
efficient traversal of the list in both directions. This is because each node in the list contains a
pointer to the previous node and a pointer to the next node. This allows for quick and easy
insertion and deletion of nodes from the list, as well as efficient traversal of the list in both
directions.
What is a Doubly Linked List?
A doubly linked list is a data structure that consists of a set of nodes, each of which
contains a value and two pointers, one pointing to the previous node in the list and one
pointing to the next node in the list. This allows for efficient traversal of the list in both
directions, making it suitable for applications where frequent insertions and deletions are
required.
Representation of Doubly Linked List in Data Structure
In a data structure, a doubly linked list is represented using nodes that have three fields:
1.​ Data
2.​ A pointer to the next node (next)
3.​ A pointer to the previous node (prev)
Operations on doubly linked list
1.​ Traversal in Doubly Linked List
2.​ Searching in Doubly Linked List
3.​ Finding Length of Doubly Linked List
4.​ Insertion in Doubly Linked List:
●​ Insertion at the beginning of Doubly Linked List
●​ Insertion at the end of the Doubly Linked List
●​ Insertion at a specific position in Doubly Linked List
5.​ Deletion in Doubly Linked List:
●​ Deletion of a node at the beginning of Doubly Linked List
●​ Deletion of a node at the end of Doubly Linked List
●​ Deletion of a node at a specific position in Doubly Linked List

Insertion at the Beginning in Doubly Linked List

To insert a new node at the beginning of the doubly list, we can use the following steps:
●​ Create a new node, say new_node with the given data and set its previous pointer to
null, new_node->prev = NULL.
●​ Set the next pointer of new_node to current head, new_node->next = head.
●​ If the linked list is not empty, update the previous pointer of the current head to
new_node, head->prev = new_node.
●​ Return new_node as the head of the updated linked list.
Insertion at the End of Doubly Linked List

To insert a new node at the end of the doubly linked list, we can use the following steps:
1.​ Allocate memory for a new node and assign the provided value to its data field.
2.​ Initialize the next pointer of the new node to nullptr.
3.​ If the list is empty:
●​ Set the previous pointer of the new node to nullptr.
●​ Update the head pointer to point to the new node.
4.​ If the list is not empty:
●​ Traverse the list starting from the head to reach the last node.
●​ Set the next pointer of the last node to point to the new node.
●​ Set the previous pointer of the new node to point to the last node.

Insertion at a Specific Position in Doubly Linked List


To insert a node at a specific Position in doubly linked list, we can use the following steps:
To insert a new node at a specific position,
1.​ If position = 1, create a new node and make it the head of the linked list and return it.
2.​ Otherwise, traverse the list to reach the node at position – 1, say curr.
3.​ If the position is valid, create a new node with given data, say new_node.
4.​ Update the next pointer of new node to the next of current node and prev pointer of
new node to current node, new_node->next = curr->next and new_node->prev =
curr.
5.​ Similarly, update next pointer of current node to the new node, curr->next =
new_node.
6.​ If the new node is not the last node, update prev pointer of new node’s next to the new
node, new_node->next->prev = new_node.

Deletion at the Beginning of Doubly Linked List

To delete a node at the beginning in doubly linked list, we can use the following steps:
1.​ Check if the list is empty, there is nothing to delete. Return.
2.​ Store the head pointer in a variable, say temp.
3.​ Update the head of linked list to the node next to the current head, head =
head->next.
4.​ If the new head is not NULL, update the previous pointer of new head to
NULL, head->prev = NULL.
Deletion at the End of Doubly Linked List

To delete a node at the end in doubly linked list, we can use the following steps:
1.​ Check if the doubly linked list is empty. If it is empty, then there is nothing to delete.
2.​ If the list is not empty, then move to the last node of the doubly linked list, say curr.
3.​ Update the second-to-last node's next pointer to NULL, curr->prev->next = NULL.
4.​ Free the memory allocated for the node that was deleted.
Deletion at a Specific Position in Doubly Linked List

To delete a node at a specific position in doubly linked list, we can use the following steps:
1.​ Traverse to the node at the specified position, say curr.
2.​ If the position is valid, adjust the pointers to skip the node to be deleted.
●​ If curr is not the head of the linked list, update the next pointer of the node
before curr to point to the node after curr, curr->prev->next = curr-next.
●​ If curr is not the last node of the linked list, update the previous pointer of the
node after curr to the node before curr, curr->next->prev = curr->prev.
3.​ Free the memory allocated for the deleted node.
Advantages of Doubly Linked List
1.​ Efficient traversal in both directions: Doubly linked lists allow for efficient
traversal of the list in both directions, making it suitable for applications where
frequent insertions and deletions are required.
2.​ Easy insertion and deletion of nodes: The presence of pointers to both the previous
and next nodes makes it easy to insert or delete nodes from the list, without having to
traverse the entire list.
3.​ Can be used to implement a stack or queue: Doubly linked lists can be used to
implement both stacks and queues, which are common data structures used in
programming.
4.​ Disadvantages of Doubly Linked List
5.​ More complex than singly linked lists: Doubly linked lists are more complex than
singly linked lists, as they require additional pointers for each node.
6.​ More memory overhead: Doubly linked lists require more memory overhead than
singly linked lists, as each node stores two pointers instead of one.
Applications of Doubly Linked List
1.​ Implementation of undo and redo functionality in text editors.
2.​ Cache implementation where quick insertion and deletion of elements are required.
3.​ Browser history management to navigate back and forth between visited pages.
4.​ Music player applications to manage playlists and navigate through songs efficiently.
5.​ Implementing data structures like Deque (double-ended queue) for efficient insertion
and deletion at both ends.
Circular linked list
A circular linked list is a data structure where the last node connects back to the first,
forming a loop. This structure allows for continuous traversal without any interruptions.
Circular linked lists are especially helpful for tasks like scheduling and managing
playlists, this allowing for smooth navigation. In this tutorial, we’ll cover the basics of
circular linked lists, how to work with them, their advantages and disadvantages, and their
applications.
What is a Circular Linked List?
A circular linked list is a special type of linked list where all the nodes are connected
to form a circle. Unlike a regular linked list, which ends with a node pointing to NULL, the
last node in a circular linked list points back to the first node. This means that you can keep
traversing the list without ever reaching a NULL value.
Types of Circular Linked Lists
We can create a circular linked list from both singly linked lists and doubly linked
lists. So, circular linked list are basically of two types:
1. Circular Singly Linked List
In Circular Singly Linked List, each node has just one pointer called the “next”
pointer. The next pointer of last node points back to the first node and this results in forming
a circle. In this type of Linked list we can only move through the list in one direction.

2. Circular Doubly Linked List:


In circular doubly linked list, each node has two pointers prev and next, similar to
doubly linked list. The prev pointer points to the previous node and the next points to the
next node. Here, in addition to the last node storing the address of the first node, the first
node will also store the address of the last node.

Example of Creating a Circular Linked List


Here’s an example of creating a circular linked list with three nodes (2, 3, 4):
Operations on the Circular Linked list
We can do some operations on the circular linked list similar to the singly and doubly
linked list which are:
●​ Insertion
o​ Insertion at the empty list
o​ Insertion at the beginning
o​ Insertion at the end
o​ Insertion at the given position
●​ Deletion
o​ Delete the first node
o​ Delete the last node
o​ Delete the node from any position
●​ Searching
Insertion in the circular linked list:
Insertion is a fundamental operation in linked lists that involves adding a new node to
the list. The only extra step is connecting the last node to the first one. In the circular linked
list mentioned below, we can insert nodes in four ways:
1. Insertion in an empty List in the circular linked list
To insert a node in empty circular linked list, creates a new node with the given data,
sets its next pointer to point to itself, and updates the last pointer to reference this new node.
2. Insertion at the beginning in circular linked list
To insert a new node at the beginning of a circular linked list, we first create the new
node and allocate memory for it. If the list is empty (indicated by the last pointer being
NULL), we make the new node point to itself. If the list already contains nodes then we set
the new node’s next pointer to point to the current head of the list (which is last->next), and
then update the last node’s next pointer to point to the new node. This maintains the circular
structure of the list.

Insertion at the end in circular linked list


To insert a new node at the end of a circular linked list, we first create the new node
and allocate memory for it. If the list is empty (mean, last or tail pointer being NULL), we
initialize the list with the new node and making it point to itself to form a circular structure.
If the list already contains nodes then we set the new node’s next pointer to point to the
current head (which is tail->next), then update the current tail’s next pointer to point to the
new node. Finally, we update the tail pointer to the new node. This will ensure that the new
node is now the last node in the list while maintaining the circular linkage.
Insertion at specific position in circular linked list
To insert a new node at a specific position in a circular linked list, we first check if the
list is empty. If it is and the position is not 1 then we print an error message because the
position doesn’t exist in the list. If the position is 1 then we create the new node and make it
point to itself. If the list is not empty, we create the new node and traverse the list to find the
correct insertion point. If the position is 1, we insert the new node at the beginning by
adjusting the pointers accordingly. For other positions, we traverse through the list until we
reach the desired position and inserting the new node by updating the pointers. If the new
node is inserted at the end, we also update the last pointer to reference the new node,
maintaining the circular structure of the list.

1. Delete the first node in circular linked list


To delete the first node of a circular linked list, we first check if the list is empty. If it
is then we print a message and return NULL. If the list contains only one node (the head is
the same as the last) then we delete that node and set the last pointer to NULL. If there are
multiple nodes then we update the last->next pointer to skip the head node and effectively
removing it from the list. We then delete the head node to free the allocated memory. Finally,
we return the updated last pointer, which still points to the last node in the list.
2. Delete a specific node in circular linked list
To delete a specific node from a circular linked list, we first check if the list is empty.
If it is then we print a message and return nullptr. If the list contains only one node and it
matches the key then we delete that node and set last to nullptr. If the node to be deleted is
the first node then we update the next pointer of the last node to skip the head node and
delete the head. For other nodes, we traverse the list using two pointers: curr (to find the
node) and prev (to keep track of the previous node). If we find the node with the matching
key then we update the next pointer of prev to skip the curr node and delete it. If the node is
found and it is the last node, we update the last pointer accordingly. If the node is not found
then do nothing and tail or last as it is. Finally, we return the updated last pointer.

Deletion at the end of Circular linked list


To delete the last node in a circular linked list, we first check if the list is empty. If it
is, we print a message and return nullptr. If the list contains only one node (where the head
is the same as the last), we delete that node and set last to nullptr. For lists with multiple
nodes, we need to traverse the list to find the second last node. We do this by starting from
the head and moving through the list until we reach the node whose next pointer points to
last. Once we find the second last node then we update its next pointer to point back to the
head, this effectively removing the last node from the list. We then delete the last node to free
up memory and return the updated last pointer, which now points to the last node.
Advantages of Circular Linked Lists
1.​ In circular linked list, the last node points to the first node. There are no null
references, making traversal easier and reducing the chances of encountering null
pointer exceptions.
2.​ We can traverse the list from any node and return to it without needing to restart from
the head, which is useful in applications requiring a circular iteration.
3.​ Circular linked lists can easily implement circular queues, where the last element
connects back to the first, allowing for efficient resource management.
4.​ In a circular linked list, each node has a reference to the next node in the sequence.
Although it doesn’t have a direct reference to the previous node like a doubly linked
list, we can still find the previous node by traversing the list.
Disadvantages of Circular Linked Lists
1.​ Circular linked lists are more complex to implement than singly linked lists.
2.​ Traversing a circular linked list without a clear stopping condition can lead to infinite
loops if not handled carefully.
3.​ Debugging can be more challenging due to the circular nature, as traditional methods
of traversing linked lists may not apply.
Applications of Circular Linked Lists
1.​ It is used for time-sharing among different users, typically through a Round-Robin
scheduling mechanism.
2.​ In multiplayer games, a circular linked list can be used to switch between players.
After the last player’s turn, the list cycles back to the first player.
3.​ Circular linked lists are often used in buffering applications, such as streaming data,
where data is continuously produced and consumed.
4.​ In media players, circular linked lists can manage playlists, this allowing users to loop
through songs continuously.
5.​ Browsers use circular linked lists to manage the cache. This allows you to navigate
back through your browsing history efficiently by pressing the BACK button.
Applications of List
Lists are a fundamental data structure that have many applications, including:
1.​ Dynamic memory allocation: Linked lists are ideal for applications where the
amount of data is unknown or can change.
2.​ Implementing other data structures: Linked lists can be used to implement stacks,
queues, and hash tables.
3.​ Sequential data storage and retrieval: Lists are often used to store and retrieve
collections of items in sequential order, such as playlists or to-do tasks.
4.​ Undo functionality: Linked lists can be used to implement undo mechanisms in
software applications.
5.​ Browser history management: Doubly linked lists can be used to manage the
functionality of back and forward navigation in web browsers.
6.​ Polynomial arithmetic: Linked lists can be used to represent polynomials in
computer programs.
7.​ Graphs representation: Adjacency lists, which are used to represent graphs, can be
implemented using linked lists.
8.​ Database systems: Linked lists can be used to implement data structures like indexes,
lists, and linked records.
9.​ Task scheduling: Operating systems use linked lists to manage task scheduling.
10.​Image processing: Linked lists can be used to represent images, where each pixel is
represented as a node in the list.
11.​File systems: File systems use linked lists to represent the hierarchical structure of
directories.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy