0% found this document useful (0 votes)
22 views19 pages

Linked Lists

The document discusses linked lists as a data structure. It describes how linked lists store data non-contiguously using nodes that contain a data field and a pointer to the next node. The document covers the components of linked lists like head and tail pointers. It also discusses different types of linked lists and basic linked list operations like insertion, deletion and traversal.

Uploaded by

Yash Singhal
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)
22 views19 pages

Linked Lists

The document discusses linked lists as a data structure. It describes how linked lists store data non-contiguously using nodes that contain a data field and a pointer to the next node. The document covers the components of linked lists like head and tail pointers. It also discusses different types of linked lists and basic linked list operations like insertion, deletion and traversal.

Uploaded by

Yash Singhal
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

Advanced Data Structures

Unit-I
Linked Lists
• Linked list is a linear data structure in which elements are not stored at a contiguous location, rather
they are linked using pointers.

• Linked list forms a series of connected nodes, where each node stores the data and the address of
the next node.

• Node Components:
• Data: It holds the actual value or data associated with the node.
• Next Pointer: It stores the memory address (reference) of the next node in the sequence.

• Head and Tail: The linked list is accessed through the head node, which points to the first node in the
list. The last node in the list points to NULL or nullptr, indicating the end of the list. This node is known
as the tail node.
Linked Lists Contd.
• Why do we need linked lists?
• Dynamic Data structure: The size of memory can be allocated or de-allocated at run time based
on the operation.
• Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than arrays since
no elements need to be shifted after insertion and deletion, just the address needed to be
updated.
• Efficient Memory Utilization: Linked list is a dynamic data structure, the size increases or
decreases as per the requirement, so this avoids the wastage of memory.
• Implementation: Various advanced data structures can be implemented using a linked list like a
stack, queue, graph, hash maps, etc.
• For example:
• In a system, if we maintain a sorted list of IDs in an array id[] = [1000, 1010, 1050, 2000, 2040].
• If we want to insert a new ID 1005, then to maintain the sorted order, we have to move all the
elements after 1000 (excluding 1000).
• Deletion is also expensive with arrays until unless some special techniques are used. For
example, to delete 1010 in id[], everything after 1010 has to be moved. Due to this, so much
work is being done which affects the efficiency of the code.
Linked Lists Contd.
• Types of linked lists:
• Singly-Linked List
• Doubly-Linked List
• Circular Linked List

• Singly-Linked List:
• Each node contains a reference to the next node in the sequence. Traversing a singly linked list is done in
forward direction.
Linked Lists Contd.
• Doubly-Linked List:
• Each node contains references to both the next and previous nodes. This allows for traversal in both forward
and backward directions, but it requires additional memory for the backward reference.

• Circular-Linked List:
• The last node points back to the head node, creating a circular structure. It can be either singly or
doubly linked.
Linked Lists Contd.
• Operations on Linked Lists:
• Insertion: Adding a new node to a linked list involves adjusting the pointers of the existing nodes to maintain
the proper sequence. Insertion can be performed at the beginning, end, or any position within the list.
• Deletion: Removing a node from a linked list requires adjusting the pointers of the neighboring nodes to bridge
the gap left by the deleted node. Deletion can be performed at the beginning, end, or any position within the
list.
• Searching: Searching for a specific value in a linked list involves traversing the list from the head node until the
value is found or the end of the list is reached.

• Representation of Linked List:


• The first step of creating linked list of n nodes starts from defining node structure.
Where data is the data you want to store in list. *next is pointer to the same structure type. The *next will store
location of next node if exists otherwise NULL.
Linked Lists Contd.
• Advantages of Linked Lists
• Dynamic Size: Linked lists can grow or shrink dynamically, as memory allocation is done at runtime.
• Insertion and Deletion: Adding or removing elements from a linked list is efficient, especially for large lists.
• Flexibility: Linked lists can be easily reorganized and modified without requiring a contiguous block of memory.

• Disadvantages of Linked Lists


• Random Access: Unlike arrays, linked lists do not allow direct access to elements by index. Traversal is required
to reach a specific node.
• Extra Memory: Linked lists require additional memory for storing the pointers, compared to arrays.
Traversing Linked Lists
Insertion into Linked Lists
• A new node can be added to an existing linked list in the following cases.
• The new node is inserted at the beginning.
• The new node is inserted at the end.
• The new node is inserted after a given node.

• Insert a node at the beginning of the linked list:


• Allocate memory for new node and initialize its DATA part to 24.
• Add the new node as the first node of the list by pointing the NEXT part of the new node to HEAD.
• Make HEAD to point to the first node of the list.
Insertion into Linked Lists Contd.
• Insert a node at the beginning of the linked list:
Insertion into Linked Lists Contd.
• Insert a node at the end of the linked list:
• Allocate memory for new node and initialize its DATA .
• Traverse to last node.
• Point the NEXT part of the last node to the newly created node.
• Make the value of next part of last node to NULL.
Insertion into Linked Lists Contd.
• Insert a node at the end of the linked list:
Insertion into Linked Lists Contd.
• Insert a node after a given node in the linked list:
• Allocate memory for new node and initialize its DATA part.
• Traverse the list until the specified node is reached.
• Change NEXT pointers accordingly.
Insertion into Linked Lists Contd.
• Insert a node after a given node in the linked list:
Deletion from Linked Lists
• A node can be deleted from a linked listed in the following cases.
• The first node is deleted.
• The last node is deleted.
• The node after a given node is deleted.

• Delete a node from the beginning of the linked list:


• Check if the linked list is empty or not. Exit if the list is empty.
• Make HEAD point to the second node.
• Free the first node from memory.
Deletion from Linked Lists Contd.
• Delete last node from the linked list:
• Traverse to the end of the list.
• Change value of next pointer of second last node to NULL.
• Free last node from memory.
Deletion from Linked Lists Contd.
• Delete the node after a given node from the linked list:
• Traverse the list upto the specified node.
• Change value of next pointer of previous node to next pointer of current node.
Searching in Linked Lists
• Finding an element is similar to a traversal operation.

• Instead of displaying data, we have to check whether the data matches with the item to find.
• Initialize PTR with the address of HEAD. Now the PTR points to the first node of the linked list.
• A while loop is executed which will compare data of every node with item.
• If item has been found then control goes to last step.
Thank You

You might also like