Advertisement
apl-mhd

Huda sir bouble sort Linked list

Mar 12th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. using namespace std;
  5. struct list{
  6.  
  7.     int data;
  8.     struct list *next;
  9. };
  10.  
  11. typedef list node;
  12.  
  13. node *insert(node *head){
  14.  
  15.     node *temp, *prev;
  16.     for (int i=0; i<5; i++){
  17.         if(head == NULL){
  18.  
  19.             head = new node();
  20.             scanf("%d",&head->data);
  21.             head->next = NULL;
  22.             prev = head;
  23.         }
  24.         else{
  25.  
  26.             temp = new node();
  27.             scanf("%d",&temp->data);
  28.             temp->next = NULL;
  29.             prev->next = temp;
  30.             prev =temp;
  31.         }
  32.  
  33.  
  34.     }
  35.  
  36.     return head;
  37. }
  38.  
  39. void boubleSort(node *head){
  40.  
  41.     int i,n,j,x;
  42.     n=5;
  43.     node *temp;
  44.  
  45.     for(i=1; i<n; i++){
  46.            temp = head;
  47.         for(j=0; j<n-i; j++){
  48.  
  49.             if(temp->data > temp->next->data){
  50.  
  51.                 x = temp->data;
  52.                 temp->data = temp->next->data;
  53.                 temp->next->data = x;
  54.                 temp=temp->next;
  55.             }
  56.         }
  57.     }
  58.  
  59. }
  60.  
  61. void display(node *head){
  62.  
  63.   //  node *temp;
  64.    // temp = head;
  65.     while(head != NULL){
  66.  
  67.  
  68.         printf("%d ", head->data);
  69.         head = head->next;
  70.     }
  71.  
  72. }
  73.  
  74. int main()
  75. {
  76.   node *head, *head2;
  77.     head =NULL;
  78.     head2=NULL;
  79.  head = insert(head);
  80.  
  81. display(head);
  82.  
  83. cout<<endl<<"After sorting"<<endl;
  84. boubleSort(head);
  85. display(head);
  86.  
  87. cout<<endl;
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement