Advertisement
vasa_stulo

Untitled

Jan 23rd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5.  
  6. typedef struct List
  7. {
  8. int info;
  9. struct List* next;
  10. } List;
  11.  
  12. List *First, *Last, *el, *tmp,*tmp2;
  13.  
  14.  
  15. void delete(int k)
  16. {
  17. List *el, * tmp;
  18. int count=0;
  19. if (First == NULL)
  20. return;
  21. if (k < 0)
  22. return;
  23. for (el = First; el != NULL; el = el->next)
  24. count++;
  25. if (k > count)
  26. {
  27. printf("\nТакого элемента не существует\n");
  28. return;
  29. }
  30. if (First->next != NULL && k == 1) {
  31. First = First->next;
  32. return;
  33. }
  34. if (k == 2)
  35. {
  36. el= First->next;
  37. First->next = el->next;
  38. }
  39. el = First;
  40. tmp= el->next;
  41. count = 2;
  42. for (el = First; el != NULL; el = el->next,count++)
  43. if (count == k ) {
  44. tmp = el->next;
  45. el->next = tmp->next;
  46. }
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53. void push(int inf)
  54. {
  55. struct List* el;
  56.  
  57. el = (struct List*)malloc(sizeof(struct List));
  58.  
  59. if (el == NULL)
  60. {
  61. printf("Не удалось выделить память под элемент списка.\n");
  62. return;
  63. }
  64.  
  65. el->info = inf;
  66. el->next = NULL;
  67.  
  68. if (First == NULL)
  69. First = Last = el;
  70. else
  71. {
  72. Last->next = el;
  73. Last = Last->next;
  74. }
  75. }
  76.  
  77. int main(void)
  78. {
  79. int inf, k = 0;
  80. setlocale(LC_ALL, "Russian");
  81. First = Last = NULL;
  82. scanf("%d", &k);
  83.  
  84. printf("Введите элементы списка (окончание ввода -- любой символ, кроме цифры):\n");
  85. while (scanf("%d", &inf) != 0)
  86. {
  87. push(inf);
  88. }
  89.  
  90. delete(k);
  91. printf("\n");
  92.  
  93. for (el = First; el != NULL; el = el->next)
  94. {
  95. printf("%d ", el->info);
  96. }
  97.  
  98. for (el = First; el != NULL;)
  99. {
  100. tmp = el;
  101. el = el->next;
  102. free(tmp);
  103. }
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement