Advertisement
Guest User

kdasld

a guest
May 26th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct list {
  5.     int data;
  6.     struct list *next;
  7. } LIST;
  8.  
  9.  
  10. LIST *append( LIST *, int );
  11. LIST *sort( LIST * );
  12. LIST *list_switch( LIST *, LIST * );
  13. void print_list( LIST * );
  14.  
  15. int main(void)
  16. {
  17.     LIST *try;
  18.     int i;
  19.  
  20.     // This is just testing code
  21.     try = NULL;
  22.     try = append( try, 5 );
  23.     try = append( try, 2 );
  24.     try = append( try, 9 );
  25.     try = append( try, 8 );
  26.     try = append( try, 1 );
  27.     try = append( try, 7 );
  28.  
  29.     printf("Original list:\n");
  30.     print_list( try );
  31.     try = sort( try );
  32.     printf("Sorted list:\n");
  33.     print_list( try );
  34.     return 0;
  35. }
  36.  
  37. void print_list( LIST *t )
  38. {
  39.     while( t != NULL ) {
  40.         printf( "%d\n", t->data );
  41.         t = t->next;
  42.     }
  43. }
  44.  
  45. LIST *append( LIST *start, int newdata )
  46. {
  47.     LIST *new, *end, *ret;
  48.  
  49.     if( (new = (LIST *)malloc(sizeof(LIST))) == NULL) {
  50.         fprintf( stderr, "Memory Allocation error.\n" );
  51.         // In Windows, replace following with a return statement.
  52.         exit(1);
  53.     }
  54.     if( start == NULL )
  55.         ret = new;
  56.     else {
  57.         ret = start;
  58.         end = start;
  59.         while( end->next != NULL ) end = end->next;
  60.         end->next = new;
  61.     }
  62.     new->data = newdata;
  63.     new->next = NULL;
  64.     return ret ;
  65. }
  66.  
  67. LIST *sort( LIST *start )
  68. {
  69.     LIST *p, *q, *top;
  70.     int changed = 1;
  71.  
  72.     /*
  73.     * We need an extra item at the top of the list just to help
  74.     * with assigning switched data to the 'next' of a previous item.
  75.     * It (top) gets deleted after the data is sorted.
  76.     */
  77.  
  78.     if( (top = (LIST *)malloc(sizeof(LIST))) == NULL) {
  79.         fprintf( stderr, "Memory Allocation error.\n" );
  80.         // In Windows, replace following with a return statement.
  81.         exit(1);
  82.     }
  83.  
  84.     top->next = start;
  85.     if( start != NULL && start->next != NULL ) {
  86.         /*
  87.         * This is a survival technique with the variable changed.
  88.         *
  89.         * Variable q is always one item behind p. We need q, so
  90.         * that we can make the assignment q->next = list_switch( ... ).
  91.         */
  92.  
  93.         while( changed ) {
  94.             changed = 0;
  95.             q = top;
  96.             p = top->next;
  97.             while( p->next != NULL ) {
  98.                 /* push bigger items down */
  99.                 if( p->data > p->next->data ) {
  100.                     q->next = list_switch( p, p->next );
  101.                     changed = 1;
  102.                 }
  103.                 q = p;
  104.                 if( p->next != NULL )
  105.                     p = p->next;
  106.             }
  107.         }
  108.     }
  109.     p = top->next;
  110.     free( top );
  111.     return p;
  112. }
  113.  
  114. LIST *list_switch( LIST *l1, LIST *l2 )
  115. {
  116.     l1->next = l2->next;
  117.     l2->next = l1;
  118.     return l2;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement