Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- using namespace std;
- struct Node
- {
- int Value;
- Node* Next;
- };
- Node* head;//заглавное звено (голова)
- Node* tail;//конечное звено (хвост)
- void Initial(Node*&, Node*&);
- void Output(Node*, Node*);
- void AddElement(Node*&, Node*&, int);
- int DeleteElement(Node*&, Node*&, int);
- void DeleteAll(Node*&, Node*&);
- int main()
- {
- int q;
- do
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- cout << "Выберите действие:" << endl;
- cout << "1. Ввести элементы очереди" << endl;
- cout << "2. Вывести очередь" << endl;
- cout << "3. Добавить новый элемент в очередь" << endl;
- cout << "4. Удалить элемент из очереди" << endl;
- cout << "5. Удалить очередь" << endl;
- cout << "0. Выход" << endl;
- cin >> q;
- if (q == 1)
- Initial(head, tail);
- else if (q == 2)
- Output(head, tail);
- else if (q == 3)
- {
- int elem;
- cout << "Введите значение нового элемента" << endl;
- cin >> elem;
- AddElement(head, tail, elem);
- }
- else if (q == 4)
- {
- int elem = 0;
- elem = DeleteElement(head, tail, elem);
- }
- else if (q == 5)
- DeleteAll(head, tail);
- } while (q != 0);
- }
- void Initial(Node *&head, Node *&tail)
- {
- Node* tmp;
- int elem;
- cout << "Введите элементы:" << endl;
- cin >> elem;
- if (elem != 0)
- {
- tmp = new Node;
- tmp->Value = elem;
- tmp->Next = nullptr;
- head = tail = tmp;
- cin >> elem;
- while (elem != 0)
- {
- tmp = new Node;
- tmp->Value = elem;
- tmp->Next = nullptr;
- tail->Next = tmp;
- tail = tmp;
- cin >> elem;
- }
- }
- else head = tail = tmp = nullptr;
- }
- void Output(Node *head, Node *tail)
- {
- if (head == nullptr)
- cout << "Очередь пуста" << endl;
- else
- {
- Node* tmp = head;
- cout << "Вывод очереди:" << endl;
- while (tmp != nullptr)
- {
- cout << tmp->Value << '\t';
- tmp = tmp->Next;
- }
- cout << endl;
- }
- }
- void AddElement(Node*& head, Node*& tail, int elem)
- {
- Node* tmp = new Node;
- tmp->Value = elem;
- tmp->Next = nullptr;
- if (head != nullptr)
- {
- tail->Next = tmp;
- tail = tmp;
- }
- else head = tail = tmp;
- }
- int DeleteElement(Node*& head, Node*& tail, int elem)
- {
- if (head == nullptr)
- cout << "Очередь пуста" << endl;
- else
- {
- Node* tmp = head;
- elem = head->Value;
- head = head->Next;
- delete tmp;
- }
- return elem;
- }
- void DeleteAll(Node*& head, Node*& tail)
- {
- if (head == nullptr)
- cout << "Очередь пуста" << endl;
- else
- {
- while (head != nullptr)
- {
- Node* tmp = head;
- head = head->Next;
- delete tmp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement