Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- class deq {
- private:
- struct Node {
- int data;
- Node* next = nullptr;
- Node* previous = nullptr;
- }*head, *tail;
- public:
- deq() {
- head = nullptr;
- tail = nullptr;
- }
- void push_front(int el) {
- // создаю новую хуйню в дек новый элемент
- Node* temp = new Node;
- // сразу в ее дату кладем чиселку
- temp->data = el;
- // проверяем есть ли у нас ваще чето в деке, если нет, то голова = жопе = новому элементу
- if (head != nullptr) {
- // если же в деке чето есть смещаем все
- // то есть в текущей голову указатель на предыдущий будет = новому элементу новой голове
- head->next = temp;
- // в новом элементе следующим будет текущая голова
- // вся магия присовения
- head = temp;
- // зануляем в текущей голове указатель на предидущий
- head->previous = nullptr;
- // почему некорренктно работает (((
- }
- else head = tail = temp;
- for(Node* i = tail; i != nullptr;) {
- cout << i->data << endl;
- i = i->next;
- }
- cout << "ok" << endl;
- }
- void push_back(int el) {
- Node* temp = new Node;
- temp->data = el;
- if (tail != nullptr) {
- temp->next = tail;
- temp->previous = nullptr;
- tail = temp;
- }
- else {
- head = temp;
- tail = temp;
- }
- for(Node* i = tail; i != nullptr;) {
- cout << i->data << endl;
- i = i->next;
- }
- cout << "ok" << endl;
- }
- void pop_front() {
- if (head != NULL) {
- Node* del = head;
- cout << del->data << "ok" << endl;
- head = head->next;
- head->previous = NULL;
- delete del;
- }
- else cout << "error" << endl;
- }
- void pop_back() {
- if (tail != NULL) {
- Node* del = tail;
- cout << del->data << "ok" << endl;
- tail = tail->previous;
- tail->next = NULL;
- delete del;
- }
- else cout << "error" << endl;
- }
- void front() {
- cout << head->data << endl;
- }
- void back() {
- cout << tail->data << endl;
- }
- void size() {
- Node* temp = head;
- int count = 1;
- if (NULL == head) {
- cout << 0 << endl;
- return;
- }
- else {
- while (temp != NULL) {
- count++;
- temp = temp->next;
- }
- }
- cout << count << endl;
- }
- void clear() {
- while (head != NULL) {
- Node* del = head;
- head = head->next;
- delete del;
- }
- cout << "ok" << endl;
- }
- };
- int main(){
- setlocale(LC_ALL, "Rus");
- string input;
- deq line;
- while (input != "exit") {
- cin >> input;
- if (input == "push_front") {
- int el;
- cin >> el;
- line.push_front(el);
- }
- if (input == "push_back") {
- int el;
- cin >> el;
- line.push_back(el);
- }
- if (input == "pop_front") { line.pop_front(); }
- if (input == "pop_back") { line.pop_back(); }
- if (input == "front") { line.front(); }
- if (input == "back") { line.back(); }
- if (input == "size") { line.size(); }
- if (input == "clear") { line.clear(); }
- }
- cout << "bye";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment