Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <locale>
- #include <iomanip>
- #include <ctime>
- #include <cmath>
- using namespace std;
- struct list
- {
- int data;
- list *next, *prev;
- };
- void list_add(int num, list *&head)
- {
- if (head == 0)
- {
- head = new list;
- head->next = 0;
- head->prev = 0;
- head->data = num;
- }
- else
- {
- list *temp = head;
- while (temp->next != NULL)
- temp = temp->next;
- list *p = new list;
- p->data = num;
- p->next = 0;
- p->prev = temp;
- temp->next = p;
- temp = temp->next;
- }
- }
- bool list_empty(list *head)
- {
- if (head == NULL)
- return 1;
- else
- return 0;
- }
- void list_print(list *head)
- {
- if (list_empty(head))
- {
- cout << "Список пуст" << endl;
- return;
- }
- list *temp = head;
- while (temp->next != NULL)
- {
- temp = temp->next;
- cout << temp->data << " ";
- }
- cout << endl;
- temp = head;
- }
- void list_delete_first(list *&head)
- {
- if (list_empty(head))
- return;
- if (head->next == NULL)
- {
- list *temp = head;
- delete temp;
- head = 0;
- return;
- }
- list *temp = head;
- head = head->next;
- head->prev = NULL;
- delete temp;
- }
- void list_delete_last(list *&head)
- {
- if (list_empty(head))
- return;
- if (head->next == NULL)
- {
- list *temp = head;
- delete temp;
- head = 0;
- return;
- }
- list *temp = head;
- while (temp->next != NULL)
- temp = temp->next;
- temp->prev->next = NULL;
- delete temp;
- }
- void list_delete_n(int pos, list *&head)
- {
- if (list_empty(head))
- return;
- if (head->next == NULL)
- {
- list *temp = head;
- delete temp;
- head = 0;
- return;
- }
- if (pos == 1)
- {
- list_delete_first(head);
- return;
- }
- list *temp = head;
- for (int i = 0; i < pos; i++)
- temp = temp->next;
- if (temp->next == NULL)
- {
- list_delete_last(head);
- return;
- }
- temp->next->prev = temp->prev;
- temp->prev->next = temp->next;
- temp->next = 0;
- temp->prev = 0;
- delete temp;
- }
- void list_delete_with_spec_all(int val, list *&head)
- {
- list *temp = head;
- int k = 1;
- while (temp != 0)
- {
- if (temp->data == val)
- if (k == 1)
- list_delete_first(head);
- else if (temp->next == 0)
- {
- list_delete_last(head);
- break;
- }
- else
- {
- temp->next->prev = temp->prev;
- temp->prev->next = temp->next;
- list *kill = temp;
- temp = temp->next;
- k++;
- delete kill;
- continue;
- }
- k++;
- temp = temp->next;
- }
- }
- void list_delete_spec_first(int val, list *&head)
- {
- list *temp = head;
- if (temp->data == val)
- {
- list_delete_first(head);
- return;
- }
- else
- {
- while ((temp->data != val) && (temp->next != 0))
- temp = temp->next;
- if ((temp->next == 0) && (temp->data == val))
- {
- list_delete_last(head);
- return;
- }
- temp->next->prev = temp->prev;
- temp->prev->next = temp->next;
- list *kill = temp;
- temp = temp->next;
- delete kill;
- }
- }
- void task();
- int main()
- {
- setlocale(LC_ALL, "Russian");
- task();
- system("pause");
- return 0;
- }
- void task()
- {
- srand(time(0));
- int n;
- cout << "Введите количество элементов списка: ";
- cin >> n;
- list *list_ = 0;
- for (int i = 0; i <= n; i++)
- list_add(i*rand() % 10, list_);
- list_print(list_);
- list_delete_first(list_);
- list_print(list_);
- list_delete_last(list_);
- list_print(list_);
- int pos;
- cout << "Введите позицию элемента, который требуется удалить: ";
- cin >> pos;
- list_delete_n(pos,list_);
- list_print(list_);
- int val;
- cout << "Введите значение, которое требуется удалить: ";
- cin >> val;
- list_delete_with_spec_all(val, list_);
- list_print(list_);
- int val_;
- cout << "Введите значение, которое требуется удалить первым в списке: ";
- cin >> val_;
- list_delete_spec_first(val_, list_);
- list_print(list_);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement