Advertisement
Mukit1234

Untitled

Oct 21st, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5.     int data;
  6.     struct node *next;
  7. }*head;
  8. void createlinklist(int n);
  9. void display();
  10. void sortlist(struct node *head);
  11. int main()
  12. {
  13.     int n;
  14.     printf("Enter number of nodes: ");
  15.     scanf("%d",&n);
  16.     createlinklist(n);
  17.     printf("Link-list data are:\n");
  18.     display();
  19.     printf("sorted list is :\n");
  20.     sortlist(head);
  21.     display1(head);
  22.     return 0;
  23. }
  24. void createlinklist( n)
  25. {
  26.     struct node *p,*temp;
  27.     int data,i;
  28.     printf("Input data for node no 1: ");
  29.     scanf("%d",&data);
  30.     head=(struct node*)malloc(sizeof(struct node));
  31.     head->data=data;
  32.     head->next=NULL;
  33.     p=head;
  34.     temp=head;
  35.     for(i=2; i<=n; i++)
  36.     {
  37.         printf("Input data for node no %d: ",i);
  38.         scanf("%d",&data);
  39.         temp=(struct node*)malloc(sizeof(struct node));
  40.         temp->data=data;
  41.         temp->next=NULL;
  42.         p->next=temp;
  43.         p=p->next;
  44.     }
  45. }
  46. void display()
  47. {
  48.     struct node *temp;
  49.     temp=head;
  50.     while(temp!=NULL)
  51.     {
  52.         printf("%d\n",temp->data);
  53.         temp=temp->next;
  54.     }
  55. }
  56. void sortlist(struct node *head)
  57. {
  58.  
  59.     struct node *i,*j;
  60.     int temp;
  61.     for (i=head;i->next!=NULL;i=i->next)
  62.     {
  63.         for(j=i->next;j!=NULL;j=j->next)
  64.         {
  65.             if(i->data>j->data)
  66.             {
  67.                 temp=i->data;
  68.                 i->data=j->data;
  69.                 j->data=temp;
  70.             }
  71.  
  72.         }
  73.  
  74.     }
  75.  
  76. }
  77. void display1()
  78. {
  79.     struct node *temp;
  80.     temp=head;
  81.     while(temp!=NULL)
  82.     {
  83.         printf("%d\n",temp->data);
  84.         temp=temp->next;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement