Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- }*head;
- void createlinklist(int n);
- void display();
- void sortlist(struct node *head);
- int main()
- {
- int n;
- printf("Enter number of nodes: ");
- scanf("%d",&n);
- createlinklist(n);
- printf("Link-list data are:\n");
- display();
- printf("sorted list is :\n");
- sortlist(head);
- display1(head);
- return 0;
- }
- void createlinklist( n)
- {
- struct node *p,*temp;
- int data,i;
- printf("Input data for node no 1: ");
- scanf("%d",&data);
- head=(struct node*)malloc(sizeof(struct node));
- head->data=data;
- head->next=NULL;
- p=head;
- temp=head;
- for(i=2; i<=n; i++)
- {
- printf("Input data for node no %d: ",i);
- scanf("%d",&data);
- temp=(struct node*)malloc(sizeof(struct node));
- temp->data=data;
- temp->next=NULL;
- p->next=temp;
- p=p->next;
- }
- }
- void display()
- {
- struct node *temp;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d\n",temp->data);
- temp=temp->next;
- }
- }
- void sortlist(struct node *head)
- {
- struct node *i,*j;
- int temp;
- for (i=head;i->next!=NULL;i=i->next)
- {
- for(j=i->next;j!=NULL;j=j->next)
- {
- if(i->data>j->data)
- {
- temp=i->data;
- i->data=j->data;
- j->data=temp;
- }
- }
- }
- }
- void display1()
- {
- struct node *temp;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d\n",temp->data);
- temp=temp->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement