Introduction to singly Link List:

There are various linked list data structure in computer science which are used to store data. the data is grouped with the address of next data and this group identity is called node in linked list.

For example : let we are having a set of {2,3,4} integers. Now linked list representation will be like below:

lin

 

The above figure show the singly linked list.

Implementation of singly linked list in C

To implement the singly linked list we have to make a data node which will hold the data and address.

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;

};

int main ()
{
    struct node *head;
   /* create first node */
     head = (struct node *) malloc (sizeof (struct node));
     /* insert data */
      head->data = 2;
      head->next = NULL;

      /* create second node */
 struct node *node_2 = (struct node *) malloc (sizeof (struct node));
/* insert data */
node_2->data = 3;
/* store the address of this node in first node */
head->next = node_2;

     /* now node_2 is our last node so
     we have to insert NULL in the address field of node2 */
     node_2->next = NULL;

/* print list */
while (head != NULL)
{
     printf ("%4d", head->data);
     head = head->next;
}
printf ("\n");
return 0;
}



Related Contents to follow