2_DSA-Linked List(PART-1)
2_DSA-Linked List(PART-1)
2_DSA-Linked List(PART-1)
Data Address
• Construct 3 nodes
• Allocate memory to them
• Connect nodes one by one with each
other.
Construction of a Linked List
• Example: Create the structure of a node
struct Node
{
int data; data next
struct Node* next;
};
Construction of a Linked List
• Example: Construct 3 nodes of same type:
struct Node
{
int data; data next
struct Node* next;
};
1 2000 2 3000
1000 start
start = first;
first->data = 1; // assign data in first node
first->next = second; // Link first node with
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return 0; }
Self Referential Structure
• struct Node
•{
int next;
struct Node next;
}
• Self Referential Structure is a structure which contains a
pointer that points to a variable of the same structure
type.
• Example: Linked list node.
• In a linked list, every node contains a pointer to another
node which is of the same type, it is also called a self-
Memory allocation and De-allocation for
a linked list.
Linked list creation
Linked list creation
Linked List Traversal
• Traversing a linked list means accessing the
nodes of the list in order to perform some
processing on them.
• Traversal and Searching
• START: pointer variable START which stores
the address of the first node of the list.
• End of the list is marked by storing NULL or
–1 in the NEXT field of the last node.
Linked List Traversal
Write an algorithm to print the number of nodes in
the list
Write an algorithm to search a value in the list
Searching means finding whether a given value is present
in the information part of the node or not
Write an algorithm to search a value in the list
Linked List: Insertion
• Insertion: Adding a new node into an already
existing linked list.
• Case 1: The new node is inserted at the
beginning.
• Case 2: The new node is inserted at the end.
• Case 3: The new node is inserted after a given
node.
• Case 4: The new node is inserted before a given
node.
Insertion at the beginning
Insertion at the beginning
Insertion at the beginning
Algorithm to insert a new node at
the end
Algorithm to insert a new node
after a node that has value NUM
Algorithm to insert a new node
before a node that has value NUM
Deleting a Node from a Linked List
• Case 1: The first node is deleted.
• Case 2: The last node is deleted.
• Case 3: The node after a given node is
deleted
Algorithm to delete the first node
Algorithm to delete the last node
Algorithm to delete the node after
a given node
Circular Linked List