Sorting inside Linklist:- This Blog will show you, how to create a sorted linklist.
By using that can achieve the Sorting inside our Linklist. The Element will Enter inside Linklist in Sorted Manne.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE *node=NULL,*start=NULL,*end=NULL;
void insert();
void disp();
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:Insert");
printf("\n2:Display");
printf("\n3:Exit");
printf("\n\tEnter Your Choice:-");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
disp();
break;
case 3:
exit(1);
default:
printf("\n\tOption Not Available");
break;
}
}
getch();
}
void insert()
{
NODE *temp,*p;
temp=p=start;
node=(NODE *)malloc(sizeof(NODE));
printf("\n\tEnter the Element:-");
scanf("%d",&node->info);
if(start==NULL)
{
start=node;
end=node;
node->next=NULL;
}
else
{
while(p!=NULL)
{
if(node->info<start->info)
{
node->next=start;
start=node;
}
else if(node->info>end->info)
{
end->next=node;
node->next=NULL;
end=node;
}
else if(node->info>temp->info && node->info<p->info)
{
node->next=p;
temp->next=node;
temp=node;
}
else
{
temp=p;
p=p->next;
}
}
}
}
void disp()
{
NODE *temp;
temp=start;
if(start==NULL)
{
printf("\n\tNo Element to Display Please Insert Element First");
}
else
{
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->next;
}
}
}
0 Comment(s)