Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node {
  5.     int val;
  6.     struct Node *next;
  7. } Node;
  8.  
  9. // Длина списка
  10. int counter(Node **head) {
  11.     int counter = 0;
  12.     Node *current = *head;
  13.     if (*head == NULL) exit(-1);
  14.     else {
  15.         counter++;
  16.         while (current->next != NULL) {
  17.             counter++;
  18.             current = current->next;
  19.         }
  20.     }
  21.     return counter;
  22. }
  23.  
  24. // Печать списка
  25. void print(Node *head) {
  26.     while (head != NULL) {
  27.         printf("%d ", head->val);
  28.         head = head->next;
  29.     }
  30. }
  31.  
  32. // Добавление элемента в конец
  33. void pushBack(Node **head, int data) {
  34.     Node *tmp = (Node *) malloc(sizeof(Node));
  35.     tmp->val = data;
  36.     tmp->next = NULL;
  37.     Node *current = *head;
  38.     if (*head == NULL) *head = tmp;
  39.     else {
  40.         while (current->next != NULL) current = current->next;
  41.         current->next = tmp;
  42.     }
  43. }
  44.  
  45. // Удаление элемента из конца списка
  46. void popBack(Node **head) {
  47.     Node *current = *head;
  48.     if (*head == NULL) exit(-1);
  49.     else {
  50.         while (current->next->next != NULL) current = current->next;
  51.         free(current->next);
  52.         current->next = NULL;
  53.     }
  54.  
  55. }
  56.  
  57. // Вставка элемента
  58. void insert(Node **head, int ind, int value) {
  59.     int count = counter(head);
  60.     if (count > 0 && ind <= count)  {
  61.         Node *current = *head;
  62.         Node *tmp = (Node *)malloc(sizeof(Node));
  63.         tmp->val = value;
  64.         for (int i = 1; i < ind-1; i++)  current = current->next;
  65.         tmp->next = current->next;
  66.         current->next = tmp;
  67.     } else exit(-1);
  68. }
  69.  
  70. // Удаление элемента
  71. void delete(Node **head, int ind) {
  72.     int count = counter(head);
  73.     if ( count > 0 && ind < count) {
  74.         Node *current = *head;
  75.         Node *prev = *head;
  76.         for (int i = 1; i < ind-1; i++) prev = prev->next;
  77.         for (int i = 1; i < ind; i++) current = current->next;
  78.         prev->next = current->next;
  79.         free(current);
  80.     }
  81. }
  82.  
  83. int main() {
  84.     Node *head = NULL;
  85.     int count;
  86.     printf("Заполнение массива с помощью функции pushBack \n");
  87.     for (int i = 1; i <= 15; i++) pushBack(&head, i);
  88.     print(head);
  89.     printf("\nУдаление последнего элемента\n");
  90.     popBack(&head);
  91.     print(head);
  92.     printf("\nВставка элемента по индексу \n");
  93.     insert(&head, 5, 5);
  94.     print(head);
  95.     printf("\nУдаление элемента по индексу\n");
  96.     delete(&head, 5);
  97.     print(head);
  98.     count = counter(&head);
  99.     printf("\nДлина списка - %d \n", count);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement