Advertisement
Zennoma

Dz3

Dec 4th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <windows.h>
  5.  
  6.  
  7. struct elem
  8. {
  9. int num;
  10. elem* next;
  11. };
  12. void pechat(elem*first)
  13. {
  14. elem* q;
  15. q = first;
  16. while (q != NULL)
  17. {
  18. printf_s("%5d", q->num);
  19. q = q->next;
  20. }
  21. }
  22. void add(elem** first)
  23. {
  24. elem * n, * p, * q,*last;
  25. int fl;
  26. puts("Введите число, которое хотите добавить");
  27. int k;
  28. scanf_s("%d", &k);
  29.  
  30. {
  31. n = new elem;
  32. n->num = k;
  33. q = *first;
  34. if (abs(n->num) < abs(q->num))
  35. {
  36. n->next = *first;
  37. *first = n;
  38. }
  39. else
  40. {
  41. fl = 0;
  42. p = *first;
  43. q = *first;
  44. while ((q = q->next) != NULL && !fl)
  45. {
  46. if (abs(n->num) < abs(q->num)) //вставка n перед q
  47. {
  48. n->next = p->next;
  49. p->next = n;
  50. fl = 1;
  51. }
  52. else p = q;
  53. }
  54. if (!fl)
  55. {
  56. n->next = NULL;
  57. p->next = n;
  58. last = n;
  59. }
  60. }
  61. }
  62. }
  63. void udalenie(elem** first)
  64. {
  65. elem* q, * p, * t;
  66. puts("Введите число, которое хотите удалить");
  67. int k;
  68. scanf_s("%d", &k);
  69. t = *first;
  70. if (t->num == k)
  71. {
  72. q = *first;
  73. *first = t->next;
  74. delete q;
  75. }
  76. else {
  77. q = *first;
  78. t = t->next;
  79. while (t->num != k)
  80. {
  81. q = q->next;
  82. t = t->next;
  83. }
  84. if (t->next == NULL)
  85. {
  86. q->next = NULL;
  87. delete t;
  88. }
  89. else
  90. {
  91. q->next = t->next;
  92. delete t;
  93. }
  94. }
  95. }
  96.  
  97. void main()
  98. {
  99. SetConsoleCP(1251);
  100. SetConsoleOutputCP(1251);
  101. int i, j, k, m, fl;
  102. char string[100];
  103. char chislo[5];
  104.  
  105. elem* first, * q, * n,*p,*last;
  106. puts("Введите строку чисел, через пробел");
  107. gets_s(string, 100);
  108. first = NULL; //обнуление списка
  109. chislo[0] = '\0';
  110. j = 0;
  111. for (i = 0; i <= strlen(string); i++)
  112. {
  113. if ((string[i] == ' ') || (string[i] == '\0'))
  114. {
  115. chislo[j] = '\0';
  116. m = atoi(chislo);
  117. j = 0;
  118. chislo[0] = '\0';
  119. if (first == NULL) //формирование
  120. {
  121. first = new elem;
  122. first->num = m;
  123. first->next = NULL;
  124. last = first;
  125. }
  126. else
  127. {
  128. n = new elem;
  129. n->num = m;
  130. if (abs(n->num) < abs(first->num)) //вставка в начало списка
  131. {
  132. n->next = first;
  133. first = n;
  134. }
  135. else
  136. {
  137. fl = 0;
  138. p = first;
  139. q = first;
  140. while ((q = q->next) != NULL && !fl)
  141. {
  142. if (abs(n->num) < abs(q->num)) //вставка n перед q
  143. {
  144. n->next = p->next;
  145. p->next = n;
  146. fl = 1;
  147. }
  148. else p = q; //н после q
  149. }
  150. if (!fl)
  151. {
  152. n->next = NULL;
  153. p->next = n;
  154. last = n;
  155. }
  156. }
  157. }
  158. }
  159. else chislo[j++] = string[i];
  160. }
  161. puts("Список по абсолютной величине");
  162. pechat(first);
  163. printf_s("\n");
  164. add(&first);
  165. pechat(first);
  166. puts("");
  167. udalenie(&first);
  168. pechat(first);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement