Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include<iostream>
- //#include<fstream>
- //#include<string>
- //#include<vector>
- //#include<utility>
- //#include<algorithm>
- //#include<climits>
- //#include<set>
- //#include<map>
- //
- //using namespace std;
- #include<iostream>
- #include<string>
- using namespace std;
- struct Node
- {
- int data;
- Node* Next;
- };
- typedef Node* Pnode;
- struct queue
- {
- int size;
- Pnode head;
- Pnode tail;
- };
- void Init(queue &q)
- {
- q.head = NULL;
- q.tail = NULL;
- q.size = 0;
- }
- void push(queue &q, int x)
- {
- Pnode elem = new Node;
- elem->data = x;
- if (q.head == NULL)
- {
- q.head = elem;
- q.tail = elem;
- q.tail->Next = NULL;
- q.head->Next = NULL;
- q.size++;
- cout << "ok" << endl;
- return;
- }
- else
- {
- q.tail->Next = elem;
- elem->Next = NULL;
- q.tail = elem;
- q.size++;
- }
- cout << "ok" << endl;
- }
- void pop(queue &q)
- {
- if (q.head == NULL)
- {
- cout << "error" << endl;
- return;
- }
- Pnode elem = q.head;
- if (q.head == q.tail)
- {
- q.tail = NULL;
- q.head = NULL;
- }
- else
- {
- q.head = q.head->Next;
- }
- cout << elem->data << endl;
- q.size--;
- delete elem;
- }
- void front(queue &q)
- {
- if (q.head == NULL)
- {
- cout << "error" << endl;
- return;
- }
- cout << q.head->data << endl;
- }
- void Size(queue &q)
- {
- cout << q.size << endl;
- }
- void clear(queue &q)
- {
- while (q.head != NULL)
- {
- Pnode elem = q.head;
- q.head = q.head->Next;
- delete elem;
- }
- q.size = 0;
- q.head = NULL;
- q.tail = NULL;
- cout << "ok";
- }
- bool IsIdiot(string s)
- {
- if (s != "push" && s != "pop" && s != "front" && s != "front" && s != "size" && s != "clear" && s != "exit")
- return true;
- return false;
- }
- bool IsNumber(string s)
- {
- if (s.size() == 1)
- {
- if (isdigit(s[0]))
- return true;
- return false;
- }
- if (!(s[0] == '-' || isdigit(s[0])))
- return false;
- for (int i = 1; i < s.length(); i++)
- if (!isdigit(s[i]))
- return false;
- return true;
- }
- int main()
- {
- setlocale(LC_ALL, "rus");
- queue q;
- Init(q);
- cout << "Выберите и введите название функии." << endl;
- cout << "Список функций:" << endl;
- cout << "push n- Добавить в очередь число n." << endl <<
- "pop-Удалить из очереди первый элемент." << endl <<
- "front -Программа должна вывести значение первого элемента, не удаляя его из очереди." << endl <<
- "size- Программа должна вывести количество элементов в очереди." << endl <<
- "clear- Программа должна очистить очередь." << endl <<
- "exit -Программа должна вывести bye и завершить работу." << endl;
- while (true)
- {
- string s;
- cin >> s;
- if (IsIdiot(s))
- {
- cout << "Данные введены неверно"<<endl;
- continue;
- }
- if (s == "push")
- {
- string g;
- cin >> g;
- if (!IsNumber(g))
- {
- cout << "Данные введены неверно" << endl;
- continue;
- }
- push(q, stoi(g));
- }
- else
- if (s == "pop")
- pop(q);
- else
- if (s == "front")
- front(q);
- else
- if (s == "size")
- Size(q);
- else
- if (s == "clear")
- clear(q);
- else
- if (s == "exit")
- {
- cout << "bye";
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement