Advertisement
apl-mhd

Huda Sir BoubleSort recursive

Mar 12th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 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. node *boubleSort(node *head){
  40.  
  41.     int i,n=5,j,x;
  42.     node *temp;
  43.  
  44.     for(i=1; i<n; i++){
  45.          temp = head;
  46.         for(j=0; j<n-i; j++){
  47.  
  48.             if(temp->data > temp->next->data){
  49.  
  50.                 x = temp->data;
  51.                 temp->data = temp->next->data;
  52.                 temp->next->data = x;
  53.             }
  54.             temp = temp->next;
  55.         }
  56.     }
  57.  
  58.     return head;
  59. }
  60.  
  61. void listLength(node *head){
  62.  
  63.         int count =0;
  64.         while(head !=NULL){
  65.             count++;
  66.             head = head->next;
  67.         }
  68.     cout<<count<<endl;
  69. }
  70.  
  71. void display(node *head){
  72.  
  73.   //  node *temp;
  74.    // temp = head;
  75.     while(head != NULL){
  76.  
  77.  
  78.         printf("%d ", head->data);
  79.         head = head->next;
  80.     }
  81.  
  82. }
  83.  
  84. int main()
  85. {
  86.   node *head, *head2;
  87.     head =NULL;
  88.     head2=NULL;
  89.  head = insert(head);
  90.  
  91. display(head);
  92.  
  93. cout<<endl<<"After sorting"<<endl;
  94. head =  boubleSort(head);
  95. display(head);
  96.  
  97. cout<<endl<<"total list"<<endl;
  98. listLength(head);
  99.  
  100. cout<<endl;
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement