0% found this document useful (0 votes)
14 views29 pages

Data Structures: Abstract Data Types Explained

The document provides an overview of Data Structures and Algorithms, focusing on Abstract Data Types (ADT) such as lists, stacks, and queues. It explains the characteristics and operations associated with these data structures, including insertion, deletion, and traversal methods. Additionally, it covers linked lists, including singly and circular linked lists, detailing their implementation and manipulation techniques.

Uploaded by

mahiphilip88
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)
14 views29 pages

Data Structures: Abstract Data Types Explained

The document provides an overview of Data Structures and Algorithms, focusing on Abstract Data Types (ADT) such as lists, stacks, and queues. It explains the characteristics and operations associated with these data structures, including insertion, deletion, and traversal methods. Additionally, it covers linked lists, including singly and circular linked lists, detailing their implementation and manipulation techniques.

Uploaded by

mahiphilip88
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

Data Structures and Algorithms

UNIT 1
Introduction to Data Structures and Algorithms
Data Structures is about how data can be stored in different structures.

What are Algorithms?

An algorithm is a set of step-by-step instructions to solve a given problem or achieve


a specific goal.

A cooking recipe written on a piece of paper is an example of an algorithm, where the


goal is to make a certain dinner. The steps needed to make a specific dinner are
described exactly.

When we talk about algorithms in Computer Science, the step-by-step instructions are
written in a programming language, and instead of food ingredients, an algorithm uses
data structures.

Abstract Data Type


A data type defines the type of data structure. A data type can be categorized into a
primitive data type (for example integer, float, double etc.) or an abstract data type
(for example list, stack, queue etc.). In this article, we will discuss about Abstract
Data Types (ADT).

Abstract data type in data structure type allows reusability of a code i.e. it makes it
convenient for a programmer to write a shorter code.

What is Abstract Data Type in Data Structure?

An Abstract Data Type in data structure is a kind of a data type whose behavior is
defined with the help of some attributes and some functions. Generally, we write
these attributes and functions inside a class or a structure so that we can use an object
of the class to use that particular abstract data type.

Examples of Abstract Data Type in Data Structure are list, stack, queue etc.

Abstract Data Type Model

List ADT

Lists are linear data structures in which data is stored in a non - continuous fashion.
List consists of data storage boxes called 'nodes'. These nodes are linked to each other
i.e. each node consists of the address of some other block. In this way, all the nodes
are connected to each other through these links. You can read more about lists in this
article: Linked List Data Structure.

Stack ADT

Stack is a linear data structure in which data can be only accessed from its top. It only
has two operations i.e. push (used to insert data to the stack top) and pop (used to
remove data from the stack top).
Queue ADT

Queue is a linear data structure in which data can be accessed from both of its ends i.e.
front and rear. It only has two operations i.e. push (used to insert data to the rear of
the queue) and pop (used to remove data from the front of the queue).

You can read more about queues in this article: Queue Data Structure.

Advantages of Abstract Data Type

1. Abstract data type in data structure makes it very easy for us to use the
complex data structures along with their complex functions. It follows an
object - oriented programming paradigm.
2. By using abstract data types, we can also customize any data structure
depending on how we plan to use that particular data structure.
3. Abstract data type in data structure follows the concept of reusability of a code.
This means that we don't have to write a particular piece of code again and
again. We can just create an abstract data type and we can use it by simply
calling the functions present in it.

 An abstract data type in data structure is a kind of a data type whose behavior is
defined with the help of some attributes and some functions.
 An abstract data type in data structure can be that of a list data structure, stack
data structure and a queue data structure. Several valid operations on a particular
data structure are defined in the abstract data type.
 Abstract data types in data structure follow an object - oriented paradigm.

List ADT

• List is a collection of elements in sequential order.

• In memory we can store the list in two ways, one way is we can store the elements
in sequential memory locations. This is known as arrays. And the other way is, we can
use pointers or links to associate the elements sequentially. This known as Linked
Lists.
• In ADT the implementation details are hidden. Hence the ADT will be-

AbstractDataType List

Instances: List is a collection of elements which are arranged in a linear manner.

Operations: Various operations that can be carried out on list are -

1. Insertion: This operation is for insertion of element in the list.

2. Deletion: This operation removed the element from the list.

3. Searching: Based on the value of the key element the desired element can be
searched.

4. Modification: The value of the specific element can be changed without changing
its location.

5. Display: The list can be displayed in forward or in backward manner.

• The List can be implemented by two ways

1. Array based implementation.

2. Linked List based implementation.

Array based Implementation

• The linked list that can be represented by arrays is called static linked list.

• In this section we will discuss in detail how exactly the list can be represented using
arrays.
• Basically list is a collection of elements.

• To show the list using arrays we will have 'data' and 'link' fields in the array.

• The array can be created as array of structure as

struct node

int data;

int next;

} a[10];

• For instance: Consider a list of (10,20,30,40,50). We can store it in arrays as :

• Various operations that can be performed on static linked list are -

1. Creation of list.

2. Display of list.

3. Insertion of any element in the list.

4. Deletion of any element from the list.

5. Searching of desired element in the list.


Let us understand each operation one by one.

1. Creation of list

The list can be created by placing the node of list in an array. Each node consists of
two fields 'data' and 'next' pointer. We need to give the address of starting node which
is to be placed in the array.

Thus the list can be created as follows

While creating the list we have to first enter the location in an array where the first
node is placed and then input the pair: Data and next.

2. Display

After creation we can display the list. Normally when the list is displayed simply the
data fields are to be displayed.

3. Deletion of a node

While deleting a node from the list we simply manipulate the next pointer of previous
node in the list. And the data field of the node to be deleted is initialized to - 1.

Linked List Implementation

• Various operations of linked list are

1. Creation of linked list.


2. Display of linked list.

3. Insertion of any element in the linked list.

4. Deletion of any element from the linked list.

5. Searching of desired element in the linked list.

Example(REFER PRACTICAL PROGRAM -1)

Singly linked list


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.

// Definition of a Node in a singly linked list


struct Node

int data;
Node* next;

Node(int data)
{
this->data = data;
this->next = nullptr;
}
};
In this example, the Node class contains an integer data field (data) to store the
information and a pointer to another Node (next) to establish the link to the next
node in the list.

1. Traversal of Singly Linked List


Traversal in a linked list means visiting each node and performing operations like
printing or processing data.
Step-by-step approach:
1. Initialize a pointer (current) to the head of the list.
2. Loop through the list using a while loop until current becomes NULL.
3. Process each node (e.g., print its data).
4. Move to the next node by updating current = current->next.

2. Searching in Singly Linked List

Searching in a Singly Linked List refers to the process of looking for a specific
element or value within the elements of the linked list.
Step-by-step approach:
1. Start from the head of the linked list.
2. Check each node’s data:
 If it matches the target value, return true (element found).
 Otherwise, move to the next node.
3. Repeat until the end (NULL) is reached.
4. If no match is found, return false.

3. Length of Singly Linked List

Finding the length of a Singly Linked List means counting the total number of
nodes.
Step-by-step approach:
1. Initialize a counter (length = 0).
2. Start from the head, assign it to current.
3. Traverse the list:
 Increment length for each node.
 Move to the next node (current = current->next).
4. Return the final length when current becomes NULL.

4. 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:
a. Insertion at the Beginning of Singly Linked List: Insertion at the beginning
involves adding a new node before the current head, making it the new head.
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.
b. 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

Insertion at end of Linked List


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.
c. 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.

Step-by-step approach:
 Create a new node and assign it a value.
 If inserting at the beginning (position = 1):
o Point the new node’s next to the current head.
o Update the head to the new node.
o Return (Insertion done).
 Otherwise, traverse the list:
o Start from the head and move to the (position - 1)ᵗʰ node (just before
the desired position).
o If the position is beyond the list length, return an error or append
at the end.
 Insert the new node:
o Point the new node’s next to the next node of the current position.
o Update the previous node’s next to the new node.
 Return the updated list.

5. Deletion in Singly Linked List

Deletion involves removing a node from the linked list. Similar to insertion, there
are different scenarios for deletion:
a. 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.

Deletion at beginning in a Linked List


Steps-by-step approach:
 Check if the head is NULL.
o If it is, return NULL (the list is empty).
 Store the current head node in a temporary variable temp.
 Move the head pointer to the next node.
 Delete the temporary node.
 Return the new head of the linked list.
b. 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.

Deletion at the end of linked list


Step-by-step approach:
 Check if the head is NULL.
o If it is, return NULL (the list is empty).
 Check if the head's next is NULL (only one node in the list).
o If true, delete the head and return NULL.
 Traverse the list to find the second last node (second_last).
 Delete the last node (the node after second_last).
 Set the next pointer of the second last node to NULL.
 Return the head of the linked list.
c. 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.

Delete a Linked List node at a given position


Step-by-step approach:
 Check if the list is empty or the position is invalid, return if so.
 If the head needs to be deleted, update the head and delete the node.
 Traverse to the node before the position to be deleted.
 If the position is out of range, return.
 Store the node to be deleted.
 Update the links to bypass the node.
 Delete the stored node.

6. Modify a Singly Linked List

Updating in a Singly Linked List means modifying the value of a node at a given
position.
Step-by-step approach:
 Start from the head of the list.
 Traverse to the required position (move current node to position).
 Check if the position is valid:
o If the position is out of bounds, return an error.
 Update the node’s data with the new value.
 Return the modified list.

7. Reversing a Singly Linked List


Reversing a singly linked list means changing the direction of pointers so that the
last node becomes the new head.
Step-by-step approach:
 Initialize three pointers:
o prev = NULL (to track the previous node)
o current = head (starting point)
o next = NULL (to store the next node temporarily)
 Iterate through the list:
o Store next = current->next (save next node).
o Reverse the link: current->next = prev.
o Move prev and current forward (prev = current, current = next).
 Update head to prev (new head is the last node).

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, 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 lists 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 the 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.
Representation of Circular Singly Linked List

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.

Representation of Circular Doubly Linked List


Note: In this article, we will use the singly linked list to explain the working of
circular linked lists.

Representation of a Circular Singly Linked List

Let's take a look on the structure of a circular linked list.

Representation of a Circular Singly Linked List

Create/Declare a Node of Circular Linked List

Syntax to Declare a Circular Linked List in Different Languages:


struct Node
{ int data;
Node* next;
Node(int value)
{
data = value;
next = nullptr;
}
};
In the code above, each node has data and a pointer to the next node. When we
create multiple nodes for a circular linked list, we only need to connect the last node
back to the first one.
Example of Creating a Circular Linked List

Here’s an example of creating a circular linked list with three nodes (2, 3, 4):

Created a circular linked list with 3 nodes


first = new Node(2);
second = new Node(3);
last = new Node(4);
irst->next = second;
second->next = last;
last->next = first;

In the above code, we have created three nodes first, second, and last having values 2,
3, and 4 respectively.
 After creating three nodes, we have connected these node in a series.
 Connect the first node "first" to "second" node by storing the address of
"second" node into first's next
 Connect the second node "second" to "third" node by storing the address of
"third" node into second's next
 After connecting all the nodes, we reach the key characteristic of a circular
linked list: linking the last node back to the first node. Therefore, we store the
address of the "first" node in the "last" node.

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:
1. Insertion
 Insertion at the empty list
 Insertion at the beginning
 Insertion at the end
 Insertion at the given position
2. Deletion
 Delete the first node
 Delete the last node
 Delete the node from any position
3. 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.

Insertion in an empty List


2. Insertion at the beginning in circular linked list
To insert a new node at the beginning of a circular linked list, we
create a new node and check if the list is empty. If empty, the new node
points to itself. If not, we make the new node's next pointer point to the
current head (last->next) and update the last node's next to the new
node, preserving the circular structure.

Insertion at the beginning in circular linked list


3. Insertion at the end in circular linked list
To insert a node at the end of a circular linked list, we create the new
node and, if the list is empty, make it point to itself. Otherwise, we
update the tail's next pointer to the new node and then set the tail to
the new node, preserving the circular linkage.

4. Insertion at specific position in circular linked list


To insert a node at a specific position in a circular linked list, we
handle edge cases for an empty list and invalid positions. For valid
positions, we traverse the list and adjust the pointers to insert the
new node, updating the tail if it's inserted at the end.

Deletion from a Circular Linked List

Deletion involves removing a node from the linked list. The main difference is that
we need to ensure the list remains circular after the deletion. We can delete a node
in a circular linked list in three ways:
1. Delete the first node in circular linked list
To delete the first node of a circular linked list, we check if the list is
empty or has only one node. If so, we handle those cases by deleting
the node and updating the last pointer. For multiple nodes, we update
the last node’s next pointer to skip the head and free the head node,
returning the updated last pointer.

2. Delete a specific node in circular linked list


To delete a specific node from a circular linked list, we handle empty
list and single node cases. For other nodes, we use two pointers to
find the node, update the previous node’s next pointer to skip the
target, and delete it, updating the last pointer if needed.

3. Deletion at the end of Circular linked list


To delete the last node in a circular linked list, we handle the empty
and single node cases. For multiple nodes, we traverse to find the
second last node, update its next pointer to the head, delete the last
node, and return the updated last pointer.
Searching in Circular Linked list

Searching in a circular linked list is similar to searching in a regular linked list. We


start at a given node and traverse the list until you either find the target value or
return to the starting node. Since the list is circular, make sure to keep track of
where you started to avoid an infinite loop.

Advantages of Circular Linked Lists

 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.
 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.
 Circular linked lists can easily implement circular queues, where the last
element connects back to the first, allowing for efficient resource management.
 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

 Circular linked lists are more complex to implement than singly linked lists.
 Traversing a circular linked list without a clear stopping condition can lead
to infinite loops if not handled carefully.
 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

 It is used for time-sharing among different users, typically through a Round-


Robin scheduling mechanism.
 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.
 Circular linked lists are often used in buffering applications, such as
streaming data, where data is continuously produced and consumed.
 In media players, circular linked lists can manage playlists, this allowing
users to loop through songs continuously.
 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.

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.

Doubly Linked List

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)

Node Structure of Doubly Linked List

Node Definition

struct Node {
// To store the Value or data.
int data;

// Pointer to point the Previous Element


Node* prev;

// Pointer to point the Next Element


Node* next;
// Constructor
Node(int d) {
data = d;
prev = next = nullptr;
}
};
Each node in a Doubly Linked List contains the data it holds, a pointer to
the next node in the list, and a pointer to the previous node in the list. By linking
these nodes together through the next and prev pointers, we can traverse the list in
both directions (forward and backward), which is a key feature of a Doubly Linked
List.

1. Traversal in Doubly Linked List

Traversal in a Doubly Linked List involves visiting each node, processing its data,
and moving to the next or previous node using the forward (next) and backward (prev)
pointers.
Step-by-Step Approach for Traversal:
1. Start from the head of the list.
2. Traverse forward:
 Visit the current node and process its data (e.g., print it).
 Move to the next node using current = current->next.
 Repeat the process until the end of the list (current == NULL).
3. Optionally, traverse backward:
 Start from the tail (last node).
 Visit the current node and process its data.
 Move to the previous node using current = current->prev.
 Repeat the process until the beginning of the list (current == NULL).
Traversal is useful for displaying or processing all nodes in a doubly linked list.

2. Finding Length of Doubly Linked List

A Doubly Linked List (DLL) is a type of linked list where each node has two pointers:
1. One pointing to the next node in the sequence.
2. One pointing to the previous node in the sequence.
To find the length of a doubly linked list, we need to traverse the list while counting
the nodes.
Step-by-Step Approach for finding length:
1. Initialize a counter: Start with a counter variable (count = 0).
2. Set a pointer to the head node: Use a pointer (current) and initialize it to the
head of the linked list.
3. Traverse the list:
 While the pointer (current) is not NULL, increment the count by 1.
 Move to the next node (current = [Link]).
4. Stop at the end of the list: When the pointer reaches NULL, stop the loop.
5. Return the count: The final value of count gives the length of the doubly
linked list.

3. Insertion in a Doubly Linked List

Insertion in a Doubly Linked List (DLL) involves adding a new node at a specific
position while maintaining the connections between nodes. Since each node contains
a pointer to both the previous and next node, insertion requires adjusting these
pointers carefully.
There are three primary types of insertion in a DLL:
1. Insertion at the Beginning
1. Create a new node with the given data.
2. Set the next pointer of the new node to the current head.
3. If the list is not empty, update the prev pointer of the current head to point to
the new node.
4. Update the head of the list to the new node.
2. Insertion at the End
1. Create a new node with the given data.
2. If the list is empty, set the new node as the head.
3. Traverse the list until the last node is found.
4. Set the next pointer of the last node to the new node.
5. Set the prev pointer of the new node to the last node.
3. Insertion at a Specific Position
1. Create a new node with the given data.
2. If inserting at the beginning, follow the steps for insertion at the start.
3. Traverse the list to find the node after which insertion is needed.
4. Set the next pointer of the new node to the next node of the current position.
5. Set the prev pointer of the new node to the current node.
6. Update the prev pointer of the next node to point to the new node (if it exists).
7. Update the next pointer of the previous node to point to the new node.

4. Deletion in a Doubly Linked List

Deletion in a Doubly Linked List (DLL) involves removing a node while


maintaining the integrity of the list. Since each node contains pointers to both its
previous and next nodes, deletion requires careful pointer adjustments to ensure no
broken links occur.
Types of Deletion in a Doubly Linked List
1. Deletion at the Beginning
1. Check if the list is empty; if it is, return as there is nothing to delete.
2. Store the current head node in a temporary variable.
3. Move the head pointer to the next node.
4. If the new head exists, update its prev pointer to NULL.
5. Delete the old head node to free memory.
2. Deletion at the End
1. Check if the list is empty; if it is, return.
2. Traverse the list to find the last node.
3. Store the last node in a temporary variable.
4. Update the next pointer of the second-last node to NULL, making it the new
tail.
5. Delete the last node to free memory.
3. Deletion at a Specific Position
1. Check if the list is empty; if it is, return.
2. Traverse the list to find the node to be deleted.
3. Store the node to be deleted in a temporary variable.
4. Update the next pointer of the previous node to point to the next node.
5. Update the prev pointer of the next node to point to the previous node (if it
exists).
6. Delete the target node to free memory.

Advantages of Doubly Linked List

 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.
 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.
 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.

Disadvantages of Doubly Linked List

 More complex than singly linked lists: Doubly linked lists are more complex
than singly linked lists, as they require additional pointers for each node.
 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

 Implementation of undo and redo functionality in text editors.


 Cache implementation where quick insertion and deletion of elements are
required.
 Browser history management to navigate back and forth between visited pages.
 Music player applications to manage playlists and navigate through songs
efficiently.
 Implementing data structures like Deque (double-ended queue) for efficient
insertion and deletion at both ends.

Applications of Linked List

Dynamic Memory Allocation

Dynamic Memory Allocation is one of the key application of linked list, where
memory is assigned at run-time instead of compile time. Unlike arrays, which actually
requires a fixed size, a linked list dynamically allocates and deallocates memory as
needed, preventing wastage.

Applications of linked list data structure in memory management including tracking


free and allocated memory blocks in operating systems. Since linked lists store
elements in different locations and use pointers for connections, they ensure efficient
memory usage. This flexibility allows efficient insertion and deletion operations,
making linked lists important for dynamic memory allocation.

Implementation of Stacks and Queues

One of the most useful and important applications of linked list is making stacks and
queues, which help store and manage data. A stack is like a stack of plates, where you
can only add or remove the top element. This follows the LIFO structure (Last In,
First Out), meaning the last thing you put in is the first thing you take out. A queue is
like a ticket counter line, where the first person in line goes first, following FIFO (just
opposite of using stacks).

In applications of linked list data structure ,stacks and queues are better than arrays
because linked lists don’t need a fixed size. With arrays, you have to decide the size
before using them, and making them bigger is hard. But with a linked list, you can
add or remove elements whenever you want, making it more flexible.

Graph Representations (Adjacency List)

One more important application of linked list is in representing graphs using an


adjacency list. A graph is like a network of cities connected by roads. Instead of using
a big table (matrix) to store connections, an adjacency list prefers to use linked lists to
store only the necessary links as well as saving memory.
Each node in the graph has a list of neighbors, making it easy to find connections.
This is useful in maps, social networks, and recommendation systems. Since
applications of linked list data structure help in handling dynamic connections,
adjacency lists are the best way to store large graphs efficiently.

Undo/Redo Functionality in Editors

Undo / Redo functionality is the coolest applications of linked list data structure.
When you type something and press undo, the editor or user goes back to the previous
state. If you press the Redo option, it moves forward again. A doubly linked list data
structure executes this process that lets you move backward and forward easily.

Browser History Management

Another useful application of linked list in front of us is browser history management.


When you visit a website, your browser saves it in history. You can press Back and go
to the previous page or forward to move ahead. This works using a doubly linked list,
where each page is a node connected to the previous and next pages like the original
linked list structure does.

Music and Video Playlists

Music and video playlists are another important application of linked list. When you
play songs or videos in order, you can go to the next or previous one easily. This
works using a doubly linked list, where each song or video is a node connected to the
one before and after it.

Since applications of linked list data structure allows smooth movement in directions.
You can shuffle, repeat or skip tracks without issues. This makes the playlist work
efficiently without needing a fixed size. Application of linked list helps media players
manage songs and videos easily.

Hash Tables (Chaining for Collision handling)

One important application of linked list is in hash tables, especially for handling
collisions. When two pieces of data end up in the same spot (called a collision), a
linked list helps by storing both items in a list.

When we talk about the structure of a hash table then each location is like a box, and
when multiple items are stored in the same box we need linked list data structure to
keep them in order.

Operating System Process Handling


Operating system process handling is another application of linked list where each
process is like a task that needs to be managed. These processes are stored in a linked
list to keep a track of which one is running, waiting process or finished
process. Applications of linked list data structure help the operating system run
multiple processes without confusion, making task management smooth and fast.

Polynomial Manipulation in Mathematics

One interesting application of linked list is in polynomial manipulation in


mathematics. A polynomial is like a math equation with terms (like 9x² + 8x + 10).
Each term can be represented as a node in a linked list, where the coefficient (number)
and exponent (power of x) are stored. Using a linked list helps in performing
operations like addition, subtraction, and multiplication of polynomials. When you
add or multiply polynomials, the linked list makes it easier to manage each term and
combine them. Since applications of linked list data structure allow efficient handling
of data, they make polynomial manipulation much smoother and faster.

File System Directory Structure

One useful application of linked list is in organizing file system directory structures.
Each folder or file can be a node in a linked list, with links connecting files and
subfolders. This helps navigate and manage files efficiently, making it easy to add,
remove, or search for files.

Linked List Operations: Traverse, Insert ,Delete,Merge

There are various linked list operations that allow us to perform different actions on
linked lists. For example, the insertion operation adds a new element to the linked list.

Here's a list of basic linked list operations that we will cover in this article.

 Traversal - access each element of the linked list


 Insertion - adds a new element to the linked list
 Deletion - removes the existing elements
 Merge- Merge two linked list
 Search - find a node in the linked list
 Sort - sort the nodes of the linked list
Before you learn about linked list operations in detail, make sure to know
about Linked List first.

Things to Remember about Linked List


 head points to the first node of the linked list
 next pointer of the last node is NULL, so if the next current node is NULL,
we have reached the end of the linked list.
In all of the examples, we will assume that the linked list has three nodes 1 --->2 ---
>3 with node structure as below:

struct node {

int data;

struct node *next;

};

 Traverse a Linked List


Displaying the contents of a linked list is very simple. We keep moving the temp node
to the next one and display its contents.

When temp is NULL, we know that we have reached the end of the linked list so we
get out of the while loop.

struct node *temp = head;printf("\n\nList elements are - \n");while(temp != NULL) {

printf("%d --->",temp->data);

temp = temp->next;

The output of this program will be:

List elements are -

1 --->2 --->3 --->

 Insert Elements to a Linked List


You can add elements to either the beginning, middle or end of the linked list.

1. Insert at the beginning


 Allocate memory for new node
 Store data
 Change next of new node to point to head
 Change head to point to recently created node

struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = 4;

newNode->next = head;

head = newNode;

2. Insert at the End


 Allocate memory for new node
 Store data
 Traverse to last node
 Change next of last node to recently created node

struct node *newNode;

newNode = malloc(sizeof(struct node));

newNode->data = 4;

newNode->next = NULL;

struct node *temp = head;while(temp->next != NULL){

temp = temp->next;

temp->next = newNode;

3. Insert at the Middle


 Allocate memory and store data for new node
 Traverse to node just before the required position of new node
 Change next pointers to include new node in between

struct node *newNode;

newNode = malloc(sizeof(struct node));


newNode->data = 4;

struct node *temp = head;

for(int i=2; i < position; i++) {

if(temp->next != NULL) {

temp = temp->next;

newNode->next = temp->next;

temp->next = newNode;

Delete from a Linked List


You can delete either from the beginning, end or from a particular position.

1. Delete from beginning


 Point head to the second node

head = head->next;

2. Delete from end


 Traverse to second last element
 Change its next pointer to null

struct node* temp = head;while(temp->next->next!=NULL){

temp = temp->next;

temp->next = NULL;

3. Delete from middle


 Traverse to element before the element to be deleted
 Change next pointers to exclude the node from the chain

for(int i=2; i< position; i++) {

if(temp->next!=NULL) {

temp = temp->next;

temp->next = temp->next->next;

 Merge two sorted linked lists

The goal here is merge two linked lists that are already sorted.
For example: if L1 = 1 -> 3 -> 10 and L2 = 5 -> 6 -> 9 then your program should
output the linked list 1 -> 3 -> 5 -> 6 -> 9 -> 10.

Algorithm

The algorithm for this question is quite simple since the two linked lists are already
sorted. We create a new linked list and loop through both lists appending the smaller
nodes.

(1) Create a new head pointer to an empty linked list. (2) Check the first value of both
linked lists. (3) Whichever node from L1 or L2 is smaller, append it to the new list
and move the pointer to the next node. (4) Continue this process until you reach the
end of a linked list.

Example

L1 = 1 -> 3 -> 10 L2 = 5 -> 6 -> 9 L3 = null Compare the first two nodes in both
linked lists: (1, 5), 1 is smaller so add it to the new linked list and move the pointer in
L1. L1 = 3 -> 10 L2 = 5 -> 6 -> 9 L3 = 1 Compare the first two nodes in both linked
lists: (3, 5), 3 is smaller so add it to the new linked list and move the pointer in L1. L1
= 10 L2 = 5 -> 6 -> 9 L3 = 1 -> 3 Compare the first two nodes in both linked lists: (10,
5), 5 is smaller so add it to the new linked list and move the pointer in L2. L1 = 10 L2
= 6 -> 9 L3 = 1 -> 3 -> 5 Compare the first two nodes in both linked lists: (10, 6), 6 is
smaller so add it to the new linked list and move the pointer in L2. L1 = 10 L2 = 9 L3
= 1 -> 3 -> 5 -> 6 Compare the first two nodes in both linked lists: (10, 9), 9 is smaller
so add it to the new linked list and move the pointer in L2. L1 = 10 L2 = null L3 = 1 -
> 3 -> 5 -> 6 -> 9 Because L2 points to null, simply append the rest of the nodes from
L1 and we have our merged linked list. L3 = 1 -> 3 -> 5 -> 6 -> 9 -> 10

Code
function Node(data, next) {
[Link] = data;
[Link] = next;
}
function merge(L1, L2) {

// create new linked list pointer


var L3 = new Node(null, null);
var prev = L3;

// while both linked lists are not empty


while (L1 !== null && L2 !== null) {
if ([Link] <= [Link]) {
[Link] = L1;
L1 = [Link];
} else {
[Link] = L2;
L2 = [Link];
}
prev = [Link];
}

if (L1 === null) { [Link] = L2; }


if (L2 === null) { [Link] = L1; }

// return the sorted linked list


return [Link];

}
// create first linked list: 1 -> 3 -> 10var n3 = new Node(10, null);var n2 = new
Node(3, n3);var n1 = new Node(1, n2);var L1 = n1;
// create second linked list: 5 -> 6 -> 9var n6 = new Node(9, null);var n5 = new
Node(6, n6);var n4 = new Node(5, n5);var L2 = n4;
merge(L1, L2);

You might also like