Linked List
Linked List
DEFINITIONS
A linked list is a non primitive type of data structure in which each element
is dynamically allocated and in which elements point to each other to
define a linear relationship.
A linked list is an ordered collection of finite, homogeneous data elements
called nodes where the linear order is maintained by means of links or
pointers.
A linked list is a data structure where each object is stored in a
node
As well as storing data, the node must also contains a reference/
pointer to the node containing the next item of data
CONT’D
Linked list node in C++
Struck ListNode
{
int data;
ListNode * next;
};
Following are important terms to understand the concepts of Linked List.
Node − It consist of two fields thus.
- Date (to store the actual information)
- Link (to point to the next node)
Next − Each Link of a linked list contain a link to next link called
Next.
REPRESENTATION OF LINKED LIST IN
COMPUTER MEMORY
Linked List can be represented in memory by using two arrays respectively
known as INFO and LINK,
INFO(K) and LINK(K) contains information of element and next node
address respectively.
The list also requires a variable ‘Name’ or ‘Start’, which contains address of
first node.
Pointer field of Last node denoted by NULL which indicates the end of list.
The linked list can be represented in memory as;
CONT’D
As per above shown illustration, following are the
important points to be considered.
Linked List contains an link element called first.
Each Link carries a data fields and a Link Field called next.
Each Link is linked with its next link using its next link.
Last Link carries a Link as null to mark the end of the list.
WAYS OF REPRESENTING LINKED LIST
STATIC REPRESENTATION
-In static representation of a single linked list,
two arrays are maintained.
-One array for data and the other for links
-Two parallel arrays of equal size are allocated
which
should be sufficient to store the entire linked list.
-Programing languages like ALGOL,
FORTRAN, BASIC, etc use
only this representation to manage linked list.
CONT’D
DYNAMIC REPRESENTATION
• The efficient way of representing a linked list is using
the free pool of storage.
• In this method, there is a memory bank (which "is
nothing but a collection of free memory spaces) and a
memory manager (a program)
• During the creation of a linked list, whenever a node is
required the request is placed to the memory manager
• The memory manager will then search the memory bank
for the block requested and, if found, grants the desired
block to the caller.
• A program called garbage collector, whenever a node is
no more in use, it returns the unused node to the
TYPES OF LINKED LIST
SINGLY LINKED LIST
• it is the basic type of linked list.
• Each node contains data and pointer to the next node.
• last node’s pointer is null.
• Traversals can be done in one direction only as there is only a single
link
• between two nodes of the same list.
CONT’D
DOUBLY LINKED LIST
Each node of doubly linked list contains data and two pointers.
Previous pointer (left pointer that points to the previous node)
Next pointer (right pointer that points to the next node)
Can traverse to any direction, forward or reverse.
Can delete a node with little trouble since we have pointers to the previous and
next nodes
It requires more memory compared to singly linked list because we need an
extra pointer to point previous node.
L and R in image denotes left most and right most nodes in the list.
Left link of L node and right link of R node is NULL, indicating the end of list
for each direction
CONT’D
CONT’D
CIRCULAR LINKED LIST
Circular linked list is a singly linked list where last node points
to
first node in the list.
It does not contain null pointers like singly linked list.
We can traverse only in one direction that is forward direction.
Circular linked lists can exist in both singly linked list and
doubly
linked list.
CONT’D
IMPLEMENTATION OF LINKED LIST
Below is a simple implementation of a singly linked list in C++
#include <iostream>
class Node { public:
int data; // Data stored in the node
Node* next; // Pointer to the next node in the list
// Constructor to initialize a node with given data and a null next pointer
Node(int value) : data(value), next(nullptr) {}
};
Node Class:
- ‘Node’ class represents each element in the linked list.
- it has two members: ‘head’ which points to the first node in the list.
- the constructor initializes a node with the given data and a null next pointer.
CONT’D
class LinkedList {
private:
Node* head; // Pointer to the first node in the list
public:
// Constructor to initialize an empty linked list
LinkedList() : head(nullptr) {}
// Function to insert a new node at the beginning of the list
void insertAtBeginning(int value) {
Node* newNode = new Node(value); // Create a new node with the given
value
newNode->next = head; // Set the new node's next pointer to the
current head
head = newNode; // Update the head to the new node }
CONT’D
// function to display the elements of the linked list
Void display () {
Node* current = head; //start at the head of the list
// Traverse the list and print each element
While (current != nullptr) {
std: :cout << current->data << “ “;
current = current -> next; // move to the next node
}
std: :cout << std: :endl;
}
};
CONT’D
LinkedList Class:
-The LinkedList class manages the list.
-It has a head pointer to the first node and methods for inserting at the
beginning (insertAtBeginning) and displaying the list (display).
Int main() { LinkedList mylist; //create an instance of the LinkedList class
//Insert elements into the linked list
myList.insertAtBeginning(3);
myList.insertAtBeginning(7);
myList.insertAtBeginning(12);
//Display the elements of the linked list
myList.Display();
return 0; }
CONT’D
MAIN FUNCTION:
-An instance of the LinkedList class is created.
-Elements are inserted at the beginning of the list.
-The elements are displayed.
OPERATIONS PERFORMED ON LINKED
LIST
INSERTION:
This operation is to add/insert a new node to the list.
• Insert at the Beginning: This operation involves adding a new
node to the start of the list. It's efficient in a singly linked
list as it requires updating only the head pointer.
• Insert at the End: To add a new node to the end of the list,
traverse the list until you reach the last node, and then update
its reference to the new node.
• Insert at a Specific Position: This operation involves inserting a
node at a user- defined position in the list. It requires updating
the references of adjacent nodes accordingly.
CONT’D
CONT’D
#include <iostream> void print(node* head) {
using namespace std; while (head != NULL) {
class node { public: cout << head->data << "->";
int data; head = head->next;
node* next; } }
node(int d){ int main() {
data = d;
node* head = NULL;
next = NULL;
} }; insertAthead(head, 6);
void insertAthead(node*& head, int data) { insertAthead(head, 0);
node* n = new node(data); insertAthead(head, 4);
n->next = head; insertAthead(head, 1);
head = n; } print(head);}
CONT’D
Traversal
This operation is to access each element of the
linked list.
• Forward Traversal: Starting from the head node,
you can traverse the list node by node, visiting
each element in sequence.
• Reverse Traversal: In a doubly linked list, you can
traverse the list in reverse by starting from the tail
and moving backward.
TO TRAVERSE
CONT’D
• Start with the head
of the list.
• Access the content
of the head node if
it is not null.
• Go to the next
node(if exists) and
access the node
information
• Continue until no
more nodes (that is,
you have reached
the null node)
CONT’D
DELETION
- This operation to delete/remove a specific node from a list (if exist).
- It also involves updating pointers to maintain the correct sequence of nodes.
To delete a node;
• Search for the node to Delete
- Traverse the linked list to find the node that needs to be deleted.
- Track the current node and its previous node during traversal
• Update pointers
- if the node to be deleted is the first node (head), update the head to point to the next node.
- if the node to be deleted is not the first node:
- update the next pointer of the previous node to skip the node to be
deleted
- update the next pointer of the node to be deleted previous node to
point to the node after the one being deleted.
• Free memory
- Deallocate the memory of the node to be deleted.
CONT’D
CONT’D
SEARCHING
• It involves traversing the list to find a specific element.
• Starting from t he head (beginning) of the list, each node
is checked sequentially until the target element is found or
the end of the list is reached.
• The search operation typically involves comparing the
value being searched with the data in each node.
CONT’D
Process illustrated in pseudocode
TYPES OF SEARCH
LINEAR SEARCH
• It involves traversing the linked list sequentially
• The time complexity is O(n), where n is the number of
nodes in the linked list, as it may need to visit each node in
the worst case.
CONT’D
BINARY SEARCH
• Applicable only if the linked list
is sorted
• It works similarly to binary
search in arrays, dividing the
search interval in half t each
step.
• The time complexity is reduced
to O(log n)
• Linked list don’t support random
access, so the middle element
has to be found by traversing the
list.
CONT’D
SORTING
• Sorting a linked list typically involves dividing the into
smaller sublists.
• Sorting each sublist individually and merging them back
together in a sorted manner.
ALGORITHMS OF SORTING LINKED
LIST
MERGE SORT
• Divide: Recursively divide the linked list into two
halves until each sublist contains one element or is
empty.
• Conquer: Sort each sublist individually.
• Combine/Merge: Merge the sorted sublists back
together, ensuring that the merged list remains sorted.
• Merge sort has a time complexity of 0(n log n) but
requires additional space for the merging process.
CONT’D
CONT’D
Insertion Sort
• Build Sorted List: Maintain a sorted list while traversing
the original list.
• Insert Elements: For each element in the original list,
insert it into its correct position in the sorted list.
• Insertion sort has a time complexity of 0(n^2) but is more
space-efficient.
CONT’D