Wednesday, 17 August 2016

Linked list : Implementation using C


Hello guys !
Here is the explanation regarding to implementation of Linked list , in a very easy way .
Its very basics and aimed to clear the the fundamental and logical structure of linked list . I hope you will appreciate this !





  



#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* link;
};
struct node *head=NULL;
int main()
{
//creating first node
struct node *temp;
temp= (struct node*)malloc(sizeof(struct node));
temp->data = 2;
temp->link = NULL;
//connecting with head node
head = temp;


//creating another node
struct node *tempo;
tempo= (struct node*)malloc(sizeof(struct node));
tempo->data = 4;
tempo->link = NULL;

//traversing till NULL for connecting the created node to end .
struct node *temp1;
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
temp1->link=tempo;


//creating another node
struct node *tempoo;
tempoo= (struct node*)malloc(sizeof(struct node));
tempoo->data = 6;
tempoo->link = NULL;

//traversing till NULL for connecting the created node to end .
struct node *temp2;
temp2=head;
while(temp2->link!=NULL)
{
temp2=temp2->link;
}
temp2->link=tempoo;


//printing elements

struct node *a;
a=head;
while(a!=NULL)
{
printf("%d\n",a->data);
a=a->link;
}
}




Thanking you !

2 comments :