Thursday 9 August 2012

implementation of insertion sorting through single linked list in c


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
struct node *first=NULL,*new,*temp;
void creation()
{
int data;
do
{
printf("enter the data of node which you want to sort :");
scanf("%d",&data);
new=(struct node*)malloc(sizeof(struct node));
new->data=data;
new->link=NULL;
if(first==NULL || new->data < first->data)
{
new->link=first;
first=new;
}
else
{
temp=first;
while(temp->link!=NULL&&new->data>=temp->link->data)
temp=temp->link;
new->link=temp->link;
temp->link=new;
}
printf("\n do u want to add one more node[1/0] : ");
scanf("%d",&data);
}while(data);
}

void display()
{
if(first==NULL)
{
printf("\n list is empty... nothing to display");
}
else
{
temp=first;
printf("\n sorted list is :\n");
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}
}
}
int main()
{
creation();
display();
getch();
return 0;
}

No comments:

Post a Comment