Slip 1_2, Slip 13_2 : Write a program that sorts the elements of linked list using any of sorting technique / Sort linked list using bubble sort

Solution :
#include<stdio.h>
#include<stdlib.h>
struct node
{ int data;
 struct node *next;
};
struct node *f;
void create()
 {
 int i,n;
 struct node *s;
 printf("\nEnter no of nodes ");
 scanf("%d",&n);
 f=(struct node *)malloc(sizeof(struct node));
 printf("\n Enter node ");
 scanf("%d",&f->data);
 s=f;
for(i=1;i<n;i++)
 {
 s->next=(struct node *)malloc(sizeof(struct node));
 s=s->next;
 printf("\n Enter node ");
 scanf("%d",&s->data);
 }
 s->next=NULL;
}
void display()
{ struct node *s;
 for(s=f;s!=NULL;s=s->next)
 { printf("\t %d ->",s->data);
 }
}
void sort()
{ struct node *p,*q;
 int temp;
 for(p=f;p!=NULL;p=p->next)
 {
 for(q=p->next;q!=NULL;q=q->next)
 {
 if(p->data > q->data)
 { temp = p->data;
 p->data = q->data;
 q->data = temp;
 }
 }
 }
}
main()
{
 create();
 printf("\n Link list is : ");
 display();
 printf("\n After sorting Link list is = ");
 sort();
 display();
}

Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 23_2: Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes. Accept a name of the city from user and use binary search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”.