Advertisement
semenrbt

08/04

Mar 13th, 2021
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct Node{
  4.     int value;
  5.     struct Node *next;
  6. } Spisok;
  7.  
  8. Spisok* create(int data) // Создание первого эл-та
  9. {
  10.     // Выделяем дин.память
  11.     Spisok *tmp = (Spisok*)malloc(sizeof(Spisok));
  12.     tmp -> value = data; // Помещаем значение узлу
  13.     tmp -> next = NULL; // Присваиваем началу списка адрес NULL
  14.     return(tmp);
  15. }
  16.  
  17. Spisok* add_element_start(int data, Spisok *head) // Передаем адресс головы и значение
  18. {
  19.     // Выделение памяти под узел списка
  20.     Spisok *tmp = (Spisok*)malloc(sizeof(Spisok));
  21.     // Присваивание значения узлу
  22.     tmp -> value = data;
  23.     // Присваивание указателю на следующий элемент значения указателя на «голову»
  24.     // первоначального списка
  25.     tmp -> next = head;
  26.     return(tmp); // Возвращаем адрес на узел
  27. }
  28.  
  29. Spisok *Delete(int data, Spisok *head)
  30. {
  31.     // Присваивание новому указателю  tmp указателя head, p - NULL
  32.     Spisok *tmp = head, *p = NULL;
  33.     // Проверка списка на пустоту
  34.     if (head == NULL)
  35.     return NULL;
  36.     // Если список не пуст, поиск указателя на искомый элемент
  37.     while (tmp && tmp -> value != data){
  38.         p = tmp;
  39.         tmp = tmp -> next;
  40.     }
  41.     // Если удаляемый элемент первый
  42.     if (tmp == head){
  43.         tmp=tmp -> next;
  44.         free(head);
  45.         return tmp;
  46.     }
  47.     // Если в списке нет искомого элемента, то возвращаем первоначальный список
  48.     if (!tmp) return head;
  49.     // Присваивание новому указателю указателя tmp
  50.     p -> next = tmp -> next;
  51.     // Освобождение памяти для указателя tmp
  52.     free(tmp);
  53.     return head;
  54. }
  55. int last_element(int data, Spisok *head){
  56.     Spisok *tmp = head, *p = NULL;
  57.     if(head == NULL) return  0;
  58.     while(tmp -> next != NULL)      tmp=tmp->next;  // Ищем начало списка
  59.     p=create(data); // Создаем
  60.     tmp -> next = p; // Меняем адрес с NULL на новый p
  61.     return  1;
  62. }
  63. int DeleteAll(Spisok *head){
  64.     Spisok *tmp = head;
  65.     while(tmp -> next != NULL){
  66.         tmp= tmp -> next; // двигаемся по списку
  67.         free(head); // чистмм память
  68.         head= tmp; // сохраняем адрес следующего
  69.     }
  70. return 1;
  71. }
  72.  
  73. int main(int argc, char *argv[]) {
  74.     struct Node *p=NULL;
  75.     p=create(1);
  76.     p=add_element_start(2,p);
  77.     p=add_element_start(3,p);
  78.     p=add_element_start(4,p);
  79.     last_element(5,p);
  80.     p=Delete(4,p);
  81.     DeleteAll(p);
  82.     p=create(1);
  83.     // Вывод
  84.     while (p != NULL){
  85.         printf("%d\n", p -> value);
  86.         p = p -> next;
  87.     }
  88.     return 0;
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement