Advertisement
ailuro

lab8old

Dec 7th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <locale.h>
  7. #include <limits.h>
  8.  
  9.  
  10.  
  11. struct node
  12. {
  13.     int data;
  14.     struct node *next;
  15. };
  16. struct node *head;
  17. struct node *tail;
  18. void push();
  19. void pop();
  20. void delx();
  21. void display();
  22. void main()
  23. {
  24.     setlocale(LC_ALL, "rus");
  25.  
  26.     int choice = 10;
  27.  
  28.     while (choice != 4)
  29.     {
  30.  
  31.         printf("\n=================================================================\n");
  32.         printf("\n1.Добавить элемент\n2.Удалить элемент\n3.Показать элементы очереди\n4.Удалить первое вхождение Х\n5.Выход\n");
  33.         scanf("%d", &choice);
  34.         switch (choice)
  35.         {
  36.         case 1:
  37.             push();
  38.             break;
  39.         case 2:
  40.             pop();
  41.             break;
  42.         case 3:
  43.             display();
  44.             break;
  45.         case 4:
  46.             delx();
  47.             break;
  48.         case 5:
  49.             exit(0);
  50.         default:
  51.             printf("\nПовторите ввод\n");
  52.         }
  53.  
  54.     }
  55.     display();
  56. }
  57.  
  58. void push()
  59. {
  60.     struct node *ptr;
  61.     int item;
  62.  
  63.     ptr = (struct node *) malloc(sizeof(struct node));
  64.     if (ptr == NULL)
  65.     {
  66.         printf("\nПереполнение\n");
  67.  
  68.         return;
  69.     }
  70.     else
  71.     {
  72.         printf("\nВведите  значение\n");
  73.         scanf("%d", &item);
  74.         ptr->data = item;
  75.         if (head == NULL)
  76.         {
  77.             head = ptr;
  78.             tail = ptr;
  79.             head->next = NULL;
  80.             tail->next = NULL;
  81.         }
  82.         else
  83.         {
  84.             tail->next = ptr;
  85.             tail = ptr;
  86.             tail->next = NULL;
  87.         }
  88.     }
  89. }
  90. void pop()
  91. {
  92.     struct node *ptr;
  93.     if (head == NULL)
  94.     {
  95.         printf("\nОчередь пуста\n");
  96.  
  97.         return;
  98.     }
  99.     else
  100.     {
  101.         ptr = head;
  102.         head = head->next;
  103.         free(ptr);
  104.     }
  105. }
  106.  
  107. void delx()
  108. {
  109.     struct node * ptr, *prev;
  110.     ptr = head;
  111.     int x;
  112.     printf("Введите значение X\n");
  113.     scanf("%d", &x);
  114.     if (head == NULL)
  115.     {
  116.         printf("\nОчередь пуста\n");
  117.  
  118.         return;
  119.     }
  120.     else
  121.     {
  122.         while (ptr != NULL)
  123.         {
  124.  
  125.             if (ptr->data == x)
  126.             {
  127.                 if (ptr==head)
  128.                 {
  129.                     if (head->next == NULL)
  130.                     {
  131.                         free(ptr);
  132.                         head = NULL;
  133.  
  134.  
  135.                         return;
  136.                     }
  137.                     head = ptr->next;
  138.                     free(ptr);
  139.                     ptr = head;
  140.                     return;
  141.  
  142.  
  143.                 }
  144.                 else
  145.                 {
  146.                     if (ptr->next != NULL)
  147.                     {
  148.                         prev->next = ptr->next;
  149.                         free(ptr);
  150.                         ptr = prev->next;
  151.                         return;
  152.  
  153.  
  154.                     }
  155.                     else
  156.                     {
  157.  
  158.                         prev->next = NULL;
  159.                         free(ptr);
  160.  
  161.                         return;
  162.                     }
  163.                 }
  164.  
  165.             }
  166.             prev = ptr;
  167.  
  168.             ptr = ptr->next;
  169.  
  170.         }
  171.  
  172.  
  173.     }
  174.  
  175.  
  176. }
  177. void display()
  178. {
  179.     struct node *ptr;
  180.     ptr = head;
  181.     if (head == NULL)
  182.     {
  183.         printf("\nОчередь пуста\n");
  184.     }
  185.     else
  186.     {
  187.         printf("\nВывод значений\n");
  188.         while (ptr != NULL)
  189.         {
  190.             printf("\n%d\n", ptr->data);
  191.             ptr = ptr->next;
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement