Implementation of single linked list in c language(Add Node/Node Count/Traversal)

Structure of Single link list

  • In a single-linked list every element contains data and a link to the next element,it can be shown like this..


  • Below you can see whole singly-linked list internal representation..




Implementation of Single link list



  • inorder to implement single linked list first we need a structure of single linked list, witch need two fields i.e. data and next.
  • Create a global pointer variable called 'head' and set the value to NULL.
Ex:







Algorithms for Adding a New Node

  • Step 1: If head status is NULL then follow below procedure...
      • Create a new node and store address into head.
      • Read the node information and store in head->data.
      • Assign NULL value to head->next because it first node and tail node also.
  • Step 2:If head status is other then NULL then find tail position.
      • Take a temporary head(th) and assign current head value to th.i.e th=head.
      • Then travel to tails node position by shifting temporary head to next, i.e. th=th->next.
      • Stop traveling when th->next become NULL.
      • Add New node at th->next position.
      • shift temporary head to newly created node i.e. th=the->next.
      • Read node information and store in th->data.
      • Assign NULL value to th->next because it is tail node.

1 comment: