Lecture 2 - Linked List
Lecture 2 - Linked List
G Bharathi Mohan
Assistant Professor- CSE -AIE
Linked List
• The linked list is a linear data structure that contains a sequence of
elements such that each element links to its next element in the sequence.
• Each element in a linked list is called "Node".
• Single Linked List
• Single linked list is a sequence of elements in which every element has link
to its next element in the sequence.
• In a single linked list, the address of the first node is always stored in a
reference node known as "front" (Some times it is also known as "head").
• Always next part (reference part) of the last node must be NULL.
Linked List
Operations on Single Linked List
• If the position where we are asked to insert pos is smaller than 1 or greater
than the size of the list, it is invalid, and we will return.
• Else, we will make variable curr and make it point to the head of the list.
• Now we will run a for loop using curr to reach to the node at (pos-
1)th position:
• The for loop will be: for(int i=1;inext;}
• After the termination of the above loop, curr will be standing at the (position –
1)th node.
• As explained above, we will simply make newnode → next = curr → next and curr
→ next = newnode.
• If the pos was equal to 1, we will make the head point
to newnode as newnode will become the first node of the list.
Insertion – pos
void insertAtPosition(struct struct Node*
Node* head, int pos, int data) temp=getNode(data);
{ temp->next=curr->next;
if (pos < 1 || pos > size + 1) curr->next=temp;
printf( "Invalid position!\n" ); if(pos=1)
else { head=temp;
struct Node *curr=head; size++;
for(int i=1;i<pos-1;i+ }}
+) curr="curr-">next;
Insert a Node at the end
Deletion
• 2. if ((START → DATA) is equal to DATA)
• (a) TEMP = START
• (b) START = START → Next
• (c) Set free the node TEMP, which is deleted
• (d) Exit
Deletion
• 4. while ((HOLD → Next → Next) • 5. if ((HOLD → next → DATA) ==
not equal to NULL)) DATA)
• (a) if ((HOLD → NEXT → DATA) • (a) TEMP = HOLD → Next
equal to DATA)
• (b) Set free the node TEMP,
• (i) TEMP = HOLD → Next
which is deleted
• (ii) HOLD → Next = TEMP → Next
• (c) HOLD → Next = NULL
• (iii) Set free the node TEMP, which
is deleted • (d) Exit
• (iv) Exit • 6. Disply “DATA not found”
• (b) HOLD = HOLD → Next • 7. Exit
ALGORITHM FOR DISPLAY ALL NODES
• Suppose START is the address of the first node in the linked list. Following algorithm
• will visit all nodes from the START node to the end.
1. If (START is equal to NULL)
(a) Display “The list is Empty”
(b) Exit
2. Initialize TEMP = START
3. Repeat the step 4 and 5 until (TEMP == NULL )
4. Display “TEMP → DATA”
5. TEMP = TEMP → Next
6. Exit
ALGORITHM FOR SEARCHING A NODE
• Suppose START is the address of the first node in the linked list and DATA is the information to be
searched. After searching, if the DATA is found, POS will contain the corresponding position in the list.
1. Input the DATA to be searched
2. Initialize TEMP = START; POS =1;
3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL)
4. If (TEMP → DATA is equal to DATA)
(a) Display “The data is found at POS”
(b) Exit
5. TEMP = TEMP → Next
6. POS = POS+1
7. If (TEMP is equal to NULL)
(a) Display “The data is not found in the list”
8. Exit
Create linked list from a given array
• int main() { • struct Node {
int arr[] = { 1, 2, int data;
3, 4, 5 }, n = 5; Node* next;
Node* root = };
createLinkedList(arr, struct Node* newNode(int
n); data) {
Node* node = new Node;
printLinkedList(root); node->data = data;
return 0; node->next = NULL;
} return node;
}
Create linked list from a given array