Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5. #include <string.h>
  6.  
  7. typedef struct Elem {
  8. struct Elem *next;
  9. char *word;
  10. int lenght;
  11. }Elem;
  12.  
  13. Elem *last;
  14. typedef Elem *SLL;
  15.  
  16. SLL Insert(SLL list, char *w, int lenght);
  17. SLL Delete(Elem *l);
  18. int BubbleSort(SLL l);
  19. void Print(SLL list);
  20.  
  21. int main() {
  22. SLL l = NULL; // init
  23. char ch, w[1024];
  24. unsigned int lenght = 0;
  25. do {
  26. scanf("%c", &ch);
  27. if (ch == ' ' || ch == 10 || ch == 13) {
  28. if (lenght == lenght) {
  29. w[lenght] = '\0';
  30. l = Insert(l, w, lenght);
  31. lenght = 0;
  32. }
  33. }
  34. else w[lenght++] = ch;
  35. } while (ch != 10 && ch != 13);
  36. while (BubbleSort(l));
  37. Print(l);
  38. while (!(l == NULL)) l = Delete(l);
  39. return 0;
  40. }
  41.  
  42. SLL Insert(SLL l, char * w, int lenght) {
  43. Elem * elem = malloc(sizeof(Elem));
  44. lenght += 1;
  45. elem->word = malloc(lenght);
  46. strcpy(elem->word, w);
  47. elem->lenght = lenght;
  48. elem->next = NULL;
  49. if (l == NULL) {
  50. last = elem;
  51. return elem;
  52. }
  53. else {
  54. last->next = elem;
  55. last = elem;
  56. return l;
  57. }
  58. }
  59.  
  60. SLL Delete(Elem * l) {
  61. Elem * elem = l->next;
  62. free(l->word);
  63. free(l);
  64. return elem;
  65. }
  66.  
  67. int BubbleSort(SLL l) {
  68. Elem *elem;
  69. unsigned int rev = 0, lenght;
  70. char *w;
  71. while (elem = l->next) {
  72. if (elem->lenght < l->lenght) {
  73. w = l->word;
  74. l->word = elem->word;
  75. elem->word = w;
  76. lenght = l->lenght;
  77. l->lenght = elem->lenght;
  78. elem->lenght = lenght;
  79. rev++;
  80. }
  81. l = elem;
  82. }
  83. return rev;
  84. }
  85.  
  86. void Print(SLL l) {
  87. if (l == NULL) return;
  88. printf("%s ", l->word);
  89. Print(l->next);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement