New Want to Write for Computaholics ? if Yes Contact us at "contact@computaholics.in"

Program to Create a Linked List and Traversal of Linked List

 Program to Create a Simple Linked List With 4 Nodes:-

 A linked list is represented by a pointer to the first node of the linked list.
 The first node is called head. If the linked list is empty, then value of head is NULL.
 Each node in a list consists of at least two parts:
 1) data
 2) pointer to the next node

Link List Traversal:-
we have created a simple linked list with three nodes.
Let us traverse the created list and print the data of each node

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

struct node{

int data;
struct node *next;  //structure for a single node...A node have two parts 1->data(int,float,double..etc), 2->pointer to next node
};


void LinkListTraversal(struct node *n)
{
while(n!=0)
{
printf("%d\t",n->data);
n=n->next;
}
printf("\n");
}

main()

{

//initially assign 'NULL' to all node..otherwise garbage value will be in node

struct node *head=0;
struct node *first=0;
struct node *second=0;
struct node *third=0;


//Create space for node->Allocate Memory to All node using malloc or calloc.
// malloc return a void pointer which casting in structure data type.

head=(struct node*)malloc(sizeof(struct node));
first=(struct node*)malloc(sizeof(struct node));
second=(struct node*)malloc(sizeof(struct node));
third=(struct node*)malloc(sizeof(struct node));


//assign value to all node & point to next node

//head node

head->data=2;        //head`s data part store value 2                 ----------
                                               //                                                |  2   |    |-->
head->next=first;    //'next of head' point to node first             ----------
                                                 

//first node

first->data=3;        //first`s data part store value 2                        ----------       ----------
                                               //                                                     |  2   |     |-->|  3   |     |-->
first->next=second;    //'next of first' point to node second          ----------       ----------



//second


second->data=4;        //second`s data part store value 2                 ----------      ----------     ----------
                                               //                                                       |  2   |     |-->|  3  |     |-->|  4 |   |
second->next=third;    //'next of second' point to node second       ----------      ----------     ----------


//third node

third->data=5;        //second`s data part store value 2    ----------      ----------   ----------     --------
                                               //                                       |  2   |    |-->|  3   |     |-->|  4   |    |-->|  5 |    |-->0
third->next=0;    //'next of third' point to NULL            ----------      ----------    ----------     ---------

LinkListTraversal(head);   //Function Call

}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics