Advertisement
Ladies_Man

#SORT__3_7 Bubble LISTsort (сортировка списка пузырьком )

Dec 30th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_STRING_LEN 20
  5. #define MAX_STRING_NUM 15
  6.  
  7. struct Elem {
  8.     struct Elem *next;
  9.     char *word;
  10. };
  11.  
  12. char array[MAX_STRING_NUM][MAX_STRING_LEN], array2[10][10], s[35] = { 0 };
  13. struct Elem *turn, *swp, *inl, *z, *bsa, *oper, *element;
  14. int wnum, count;
  15.  
  16. int wcount (char *dest) {
  17.     int i, n = 0;
  18.     for (i = 0; i != strlen(dest); i++) if ((dest[i] != ' ') && (dest[i] != 9) && (dest[i+1] == ' ' || dest[i+1] == 13 || dest[i+1] == 9 || dest[i+1] == 0)) n++;
  19.     wnum = n;
  20.     return n;
  21. }
  22.  
  23. struct Elem* InitSingleLinkedList ()
  24. {
  25.   struct Elem *first=(struct Elem*)malloc(sizeof(struct Elem));
  26.   first->next=first;
  27.   return first;
  28. }
  29.  
  30.  
  31. void insertafter(struct Elem *x, struct Elem *y)
  32. {
  33.   struct Elem *z;
  34.   z=x->next;
  35.   x->next=y;
  36.   y->next=z;
  37. }
  38.  
  39. void initarray (char *dest)
  40. {
  41.     int i = 0, j = 0, k = 0;
  42.     while (i < strlen (dest)) {
  43.         while (i < strlen(dest) && dest[i] == ' ') i++;
  44.         while (i < strlen(dest) && dest[i] != ' ') {
  45.             array[j][k] = dest[i];
  46.             k++;
  47.             i++;
  48.         }
  49.         j++;
  50.         k = 0;
  51.     }
  52. }
  53.  
  54. int compare(struct Elem *a, struct Elem *b)
  55. {
  56.   if (strlen(a->word)>strlen(b->word)) return 1;
  57.   return 0;
  58. }
  59.  
  60. void swap(struct Elem *a, struct Elem *b)
  61. {
  62.   struct Elem *s;
  63.  
  64.   s=(struct Elem*)malloc(sizeof(struct Elem));
  65.   s->word=a->word;
  66.   a->word=b->word;
  67.   b->word=s->word;
  68.   free(s);
  69. }
  70.  
  71. void result (struct Elem *list)
  72. {
  73.     int i;
  74.     bsa = list;
  75.     for(i = 0; i < wnum; i++) {
  76.         printf("%s ", bsa->word);
  77.         bsa = bsa->next;
  78.     }
  79. }
  80.  
  81. struct Elem *bsort(struct Elem *list)
  82. {
  83.   int i, j;
  84.   for(i = wnum - 1; i > 0; i--)
  85.           for(j = 0, bsa = list; j < i; j++, bsa = bsa->next)
  86.             if (1 == compare(bsa, bsa->next)) swap (bsa, bsa->next);
  87.         bsa = list;
  88.     for(i = 0; i < wnum; i++) {
  89.         printf("%s ", bsa->word);
  90.         bsa = bsa->next;
  91.     }
  92. }
  93.  
  94. int main()
  95. {
  96.   int i, j;
  97.   char s[1000];
  98.   gets(s);
  99.   wnum = wcount(s);
  100.   initarray(s);
  101.  
  102.   struct Elem *current;
  103.   current=InitSingleLinkedList();
  104.  
  105.   struct Elem *element;
  106.  
  107.   for(i = wnum; i >= 0; i--){
  108.     element = (struct Elem*)malloc(sizeof(struct Elem));
  109.     insertafter (current, element);
  110.     element->word = array[i];
  111.   }
  112.  
  113.   bsort(current->next);
  114.   //mem_salvation();
  115.   int n = wnum;
  116.   current = current->next;
  117.   int freed [n+1];
  118.  
  119.   for(i = 0; i <= n; i++) {
  120.         freed[i] = current;
  121.         current = current->next;
  122.   }
  123.  
  124.   for(i = 0; i <= n; i++) free (freed[i]);
  125.  
  126.   free(current);
  127.   return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement