Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include "pch.h"
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7.  
  8.  
  9. struct Node
  10. {
  11. char* data;
  12. Node* next;
  13. };
  14.  
  15. struct List
  16. {
  17. Node* head;
  18. };
  19.  
  20. bool isEmpty(List* list)
  21. {
  22. assert(list != NULL);
  23.  
  24. if (list->head == NULL)
  25. return true;
  26. else
  27. return false;
  28. }
  29.  
  30. int getSize(List* list)
  31. {
  32. assert(list != NULL);
  33. int size = 0;
  34.  
  35. if (list->head != NULL)
  36. {
  37. size++;
  38. Node* p = list->head;
  39. while (p->next != NULL)
  40. {
  41. size++;
  42. p = p->next;
  43. }
  44. }
  45. return size;
  46. }
  47.  
  48. //нахождение последнего элемента
  49. Node* getLastNodeIfNotEmpty(List* list)
  50. {
  51. assert(list != NULL);
  52. assert(!isEmpty(list));
  53. Node* i = list->head;
  54. while (i->next != NULL)
  55. {
  56. i = i->next;
  57. }
  58. return i;
  59. }
  60.  
  61. //вставка в любое место
  62. void insertMiddle(List* list, char* value, int number)
  63. {
  64. char *s;
  65. Node* tmp = (Node*)malloc(sizeof(Node));
  66. tmp->data = value;
  67. Node* current = list->head;
  68. for (int i = 0; i < number; i++)
  69. {
  70. current = current->next;
  71. }
  72. tmp->next = current->next->next;
  73. current->next = tmp;
  74. }
  75.  
  76. //вставка в конец
  77. void insertBack(List* list, char* value)
  78. {
  79. assert(list != NULL);
  80. Node* tmp = (Node*)malloc(sizeof(Node));
  81. tmp->data = value;
  82. tmp->next = NULL;
  83. if (list->head == NULL)
  84. {
  85. list->head = tmp;
  86.  
  87. }
  88. else
  89. {
  90. Node* last = getLastNodeIfNotEmpty(list);
  91. last->next = tmp;
  92. }
  93. }
  94.  
  95. void swap(char* a, char* b)
  96. {
  97. char c;
  98. c = *a;
  99. *a = *b;
  100. *b = c;
  101. }
  102.  
  103. bool sort_two(Node* first_node)
  104. {
  105. assert(first_node != NULL);
  106. assert(first_node->next != NULL);
  107.  
  108. if (first_node->data > first_node->next->data)
  109. {
  110. swap(first_node->data, first_node->next->data);
  111. return true;
  112. }
  113. return false;
  114. }
  115.  
  116. //нахождение последнего элемента
  117. bool isLast(Node* node)
  118. {
  119. if (node->next == NULL)
  120. return true;
  121. else
  122. return false;
  123. }
  124.  
  125. void sort(List* list)
  126. {
  127. if (isEmpty(list))
  128. return;
  129.  
  130. int size = getSize(list);
  131. if (size == 1)
  132. return;
  133. else if (size > 1)
  134. {
  135. for (int k = 0; k < size; k++)
  136. {
  137. Node* i = list->head;
  138. while (!isLast(i))
  139. {
  140. sort_two(i);
  141. i = i->next;
  142. }
  143. }
  144. }
  145. }
  146.  
  147. void printList(List* list)
  148. {
  149. assert(list != NULL);
  150. Node* i = list->head;
  151. while (i != NULL)
  152. {
  153. printf("%s ", i->data);
  154. i = i->next;
  155. }
  156. }
  157.  
  158. List makeEmptyList()
  159. {
  160. List empty_list;
  161. empty_list.head = NULL;
  162. return empty_list;
  163. }
  164.  
  165. void writeList(List* list, int number)
  166. {
  167. for (int i = 0; i < number; i++)
  168. {
  169. printf("Vvedite element: ");
  170. Node* UsingMem = (Node*)malloc(sizeof(Node));
  171. char* str = (char*)malloc(1000);
  172. gets_s(str, 1000);
  173. UsingMem->data = str;
  174. insertBack(list, UsingMem->data);
  175. }
  176. }
  177.  
  178. int main()
  179. {
  180. List l = makeEmptyList();
  181. isEmpty(&l) == true;
  182. int k;
  183. printf("Vvedite kolichestvo elementov: ");
  184. scanf_s("%d", &k);
  185.  
  186.  
  187. /*Node* UsingMem = (Node*)malloc(sizeof(Node));
  188. char* str = (char*)malloc(1000);
  189. gets_s(str, 1000);
  190. UsingMem->data = str;
  191. printf("%s", UsingMem->data);*/
  192.  
  193. //char* tmp = (char*)malloc(sizeof(char));
  194.  
  195. //Node* UsingMem = (Node*)malloc(sizeof(Node));
  196. for (int i = 0; i < k; i++)
  197. {
  198. char* str = (char*)malloc(1000);
  199. printf("Vvedite element: ");
  200. gets_s(str, 1000);
  201. printf("Entered string (%d): %s", i, str);
  202. insertBack(&l, str);
  203. }
  204. //writeList(&l, k);
  205. printList(&l);
  206. printf("Enter element number:");
  207. int number;
  208. scanf_s("%d", &number);
  209. printf("Enter element: ");
  210.  
  211. //insertMiddle(&l, value, number);
  212. printf("Before sort:\n ");
  213. printList(&l);
  214. sort(&l);
  215. printf("After sort:\n ");
  216. printList(&l);
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement