Posts

Showing posts from June, 2019

Implementation of linked list

How to traverse a linked list Displaying the contents of a linked list is very simple. We keep moving the temp node to the next one and display its contents. When temp is  NULL , we know that we have reached the end of linked list so we get out of the while loop. struct node * temp = head ; printf ( "\n\nList elements are - \n" ); while ( temp != NULL ) { printf ( "%d --->" , temp -> data ); temp = temp -> next ; } The output of this program will be: List elements are - 1 --->2 --->3 ---> How to add elements to linked list You can add elements to either beginning, middle or end of linked list. Add to beginning Allocate memory for new node Store data Change next of new node to point to head Change head to point to recently created node struct node * newNode ; newNode = malloc ( sizeof ( struct node )); newNode -> data = 4 ; newNode -> next = head ; head = newNod