TEAM CODING-BCA

SORTED LINK LIST

LINK  LIST  IN  SORTED  WAY


#include<stdio.h>
#include<stdlib.h>
void sort();
void display();
struct node
{
int data;
struct node *next;
}*start=NULL;
void sort()
{
struct node *temp,*q;
int d;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&d);
temp->data=d;
if(start==NULL || start->data>d)
{
temp->next=start;
start=temp;
}
else
{
q=start;
while(q->next!=NULL && q->next->data<d)
{
q=q->next;
}
temp->next=q->next;
q->next=temp;
}
}
void display()
{
struct node *q;
q=start;
if(start==NULL)
{
printf("no node present \n");
}
while(q!=NULL)
{
printf("\n %d ",q->data);
q=q->next;
}
}
void main()
{
int ch;
while(1)
{
printf("\n enter 1 to create sorted list: \n");
printf("enter 2 to display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
sort();
break;
case 2:
display();
break;
}
}
}



No comments:

Post a Comment