top of page

Single Linked List

  • Writer: Ishika Gurnani
    Ishika Gurnani
  • Mar 9, 2020
  • 2 min read

The basic structure of the node :

We declare *head as a global variable so it can be used in all of our functions.

1. Push Head

-Create a new node, name it newNode points to the newly created node.

-Link the newly created node with the head node, the newNode will now point to head node.

-Make the newNode as the head node, now head node will point to newNode.

2. Push Mid

-Create a new node.

-Traverse to the n-1th position of the linked list (Example: If we have 5 datas we will travel to the 4th node) and connect the new node with the n+1th node. (Example: We have 5 datas and we want to insert a data in between the 4th and the 5th data, so we will connect the data to both of them). Means the new node should also point to the same node that the n-1th node is pointing to. (newNode->next = temp->nextwhere temp is the n-1th node).

3. Push Tail

-Create a new node and make sure that the address part of the new node points to NULL. (Because we are inserting in the end we have to make sure that we point it to NULL because the last data's address is always NULL.

-Traverse to the last node of the linked list and connect the last node of the list with the new node, last node will now point to new node. (Connect the last node with the node we want to input in the end.) Example: If we have 5 datas and want to add a 6th data in the end we should connect the 5th and the 6th data and make sure the 5th data stores the address of the 6th data.)

 
 
 

Comments


Proudly created with Wix.com

bottom of page