Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <math.h>
- #include <locale.h>
- #include <limits.h>
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *head;
- struct node *tail;
- void push();
- void pop();
- void delx();
- void display();
- void main()
- {
- setlocale(LC_ALL, "rus");
- int choice = 10;
- while (choice != 4)
- {
- printf("\n=================================================================\n");
- printf("\n1.Добавить элемент\n2.Удалить элемент\n3.Показать элементы очереди\n4.Удалить первое вхождение Х\n5.Выход\n");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- push();
- break;
- case 2:
- pop();
- break;
- case 3:
- display();
- break;
- case 4:
- delx();
- break;
- case 5:
- exit(0);
- default:
- printf("\nПовторите ввод\n");
- }
- }
- display();
- }
- void push()
- {
- struct node *ptr;
- int item;
- ptr = (struct node *) malloc(sizeof(struct node));
- if (ptr == NULL)
- {
- printf("\nПереполнение\n");
- return;
- }
- else
- {
- printf("\nВведите значение\n");
- scanf("%d", &item);
- ptr->data = item;
- if (head == NULL)
- {
- head = ptr;
- tail = ptr;
- head->next = NULL;
- tail->next = NULL;
- }
- else
- {
- tail->next = ptr;
- tail = ptr;
- tail->next = NULL;
- }
- }
- }
- void pop()
- {
- struct node *ptr;
- if (head == NULL)
- {
- printf("\nОчередь пуста\n");
- return;
- }
- else
- {
- ptr = head;
- head = head->next;
- free(ptr);
- }
- }
- void delx()
- {
- struct node * ptr, *prev;
- ptr = head;
- int x;
- printf("Введите значение X\n");
- scanf("%d", &x);
- if (head == NULL)
- {
- printf("\nОчередь пуста\n");
- return;
- }
- else
- {
- while (ptr != NULL)
- {
- if (ptr->data == x)
- {
- if (ptr==head)
- {
- if (head->next == NULL)
- {
- free(ptr);
- head = NULL;
- return;
- }
- head = ptr->next;
- free(ptr);
- ptr = head;
- return;
- }
- else
- {
- if (ptr->next != NULL)
- {
- prev->next = ptr->next;
- free(ptr);
- ptr = prev->next;
- return;
- }
- else
- {
- prev->next = NULL;
- free(ptr);
- return;
- }
- }
- }
- prev = ptr;
- ptr = ptr->next;
- }
- }
- }
- void display()
- {
- struct node *ptr;
- ptr = head;
- if (head == NULL)
- {
- printf("\nОчередь пуста\n");
- }
- else
- {
- printf("\nВывод значений\n");
- while (ptr != NULL)
- {
- printf("\n%d\n", ptr->data);
- ptr = ptr->next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement