L2
L2
Algorithms
Module 2: Linear Data Structure- Linked List
1
Contents
• Linked List
• Singly Linked List
• Operation on the linear list using singly linked storage
structures
• Circular List
• Applications of Linked list.
2
Linked List
A linked list is a data structure used for storing collections of data.
A linked list has the following properties.
1. Successive elements are connected by pointers
2. The last element points to NULL
3. Can grow or shrink in size during the execution of a program
4. Can be made just as long as required (until systems memory exhausts)
5. Does not waste memory space (but takes some extra memory for pointers).
6. It allocates memory as the list grows.
3
Representation
Head Data link Data link Data link NULL
Node Node Node
• LL - Consists of a sequence of elements where each element (called a node) comprises of two items -
the data and a reference (pointer) to the next node
• The last node has a reference to null.
• The entry point of the linked list is called the head (start) of the list.
• Head is not a separate node but a reference to the first node.
• If the list is empty, then the head is pointing to a null reference
4
Linked Lists ADT
The following operations make linked lists an ADT:
Main Linked Lists Operations
1. Insert: inserts an element into the list
2. Delete: removes and returns the specified position element from the list
Auxiliary Linked Lists Operations
3. Delete List: removes all elements of the list (disposees the list)
4. Count: returns the number of elements in the list
5. Find the nth node from the end of the list
5
Advantages of Linked Lists
• Dynamic Size: Linked lists can grow and shrink dynamically during runtime, making them
more flexible than arrays with a fixed size.
• Efficient Insertions/Deletions: Insertions and deletions are more efficient, especially for
large data sets, as they only require changing pointers rather than shifting elements (as in
arrays).
• No Wasted Memory: Linked lists use memory efficiently by allocating memory as needed for
each element (node), avoiding the need to pre-allocate a block of memory-like arrays.
• Easy Implementation of Data Structures: Linked lists are a foundation for implementing
other data structures like stacks, queues, and graphs.
• Efficient Use of Memory: Memory is only allocated when needed, unlike arrays that may
allocate extra memory to accommodate potential future growth
6
Disadvantages of Linked Lists:
• Higher Memory Overhead: Each node requires additional memory for the pointer(s), which
can result in significant memory usage for large lists.
• Slower Access Times: Linked lists do not allow random access to elements; you must
traverse the list sequentially, which can be time-consuming, especially for large lists.
• Complexity in Algorithms: Operations like searching or accessing a specific element can be
more complex and slower than arrays.
• Pointer Management: Managing pointers can introduce bugs or lead to issues like memory
leaks if not handled properly, especially in languages like C or C++.
• Cache Performance: Due to non-contiguous memory allocation, linked lists may perform
worse than arrays regarding cache performance, which can affect speed.
7
Arrays Vs Linked Lists
Feature Array Linked List
Definition Collection of elements of similar data Collection of nodes, each with data and a
type. pointer to the next node.
Memory Location Elements are stored in contiguous Nodes can be stored anywhere in memory
memory locations. (non-contiguous).
Memory Static memory allocation (fixed size). Dynamic memory allocation (size can grow
Management or shrink at runtime).
Element Dependency Elements are independent of each Elements (nodes) are linked through
other. pointers.
Insertion/Deletion Takes more time (due to shifting of Takes less time (only pointers need to be
elements). adjusted).
8
Arrays Vs Linked Lists
Feature Array Linked List
Access Speed Faster access through direct indexing Slower access (O(n)) as traversal is required
(O(1)). from the head.
Memory Allocation Allocated at compile-time. Allocated at runtime.
Memory Utilization Inefficient if array is not fully used Efficient, as memory is allocated only as
(unused memory space). needed.
Usage Scenario Suitable when size is known in Suitable when frequent insertions/deletions
advance and frequent access is are required.
needed.
9
Types of Linked List
• Singly Linked List: Each node has a reference to the next node in the
sequence.
Head Data link Data link Data link NULL
Node Node Node
• Doubly Linked List: Each node has two references, one to the next node and
one to the previous node.
10
Types of Linked List
• Circular Linked List: The last node returns to the first node, forming a
circular structure.
Head Data link Data link Data link
Node Node Node
11
Operations – Creation of LL
Head NULL
@xxxx10
12
Operations – Insertion
Head Data=10 Link=NULL
@xxxx10
END
13
Operations – Insertion(End)
Step :1 Identify the end node
Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=NULL
Link == Null ?
Link == Null ?
14
Operations – Insertion(End)
• Create a new node with the given data.
• If the list is empty, set head to the new node.
• Else, traverse the list to find the last node.
• Set the link of the last node to the new node
Operations – Insertion(End)
Step :2 Insert the node after the end node
Data=40 Link=NULL
@xxx40
16
Operations – Insertion(Beginning)
• Create a new node.
• Set the link of the new node to head.
• Update head to point to the new node.
Operations – Insertion(Beginning)
Step 1: Copy the reference of the Head node to new node of link
Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=NULL
Data=40 Link=xxxx10
@xxx40
18
Operations – Insertion(Beginning)
Step 2: copy the new node reference to Head
Head Data=10 Link=xxx20 Data=20 Link= xxx30 Data=30 Link=NULL
Data=40 Link=xxxx10
@xxx40
19
Remaining operations
• insertBefore(int bNodeData, int data): Insert a node before a node with specified data.
• insertAfter(int aNodeData, int data): Insert a node after a node with specified data.
• deleteBeg(): Delete the node at the beginning.
• deleteEnd(): Delete the node at the end.
• deletePos(int pos): Delete a node at a specified position.
• getPosition(int data): Get the position of a node by its data.
• display(): Display the entire linked list.
• search(int data): Search for a node with specific data.
Refer the Code
20