Norvager

Untitled

Sep 15th, 2021 (edited)
911
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>;
  3. #include "windows.h"
  4. using namespace std;
  5. class Node {
  6.     int data = NULL;
  7. public:
  8.     Node* next = NULL;
  9.     Node* prev = NULL;
  10.     Node(int data) {
  11.         this->data = data;
  12.     }
  13.     Node() {}
  14.     bool hasNext() {
  15.         return NULL != this->next;
  16.     }
  17.     Node* nextNode() {
  18.         return this->next;
  19.     }
  20.     int getData() {
  21.         return this->data;
  22.     }
  23.     bool hasPrev() {
  24.         return NULL != this->prev;
  25.     }
  26.     Node* nextPrev() {
  27.         return this->prev;
  28.     }
  29. };
  30.  
  31. class List {
  32.     Node* link = NULL;
  33.     Node* iterator = NULL;
  34. public:
  35.     List(){
  36.         link = NULL;
  37.         iterator = NULL;
  38.     }
  39.     void addLast(Node* newNode) {
  40.         if (NULL == this->link) {
  41.             this->link = newNode;
  42.         }
  43.         else {
  44.             Node* tmp = this->link;
  45.             while (tmp->hasNext()) {
  46.                 tmp = tmp->nextNode();
  47.             }
  48.             //this->link->next = newNode;
  49.             tmp->next = newNode;
  50.             tmp->next->prev = tmp;
  51.         }
  52.     }
  53.     void addFirst(Node* newNode) {
  54.         if (NULL == this->link) {
  55.             this->link = newNode;
  56.         }
  57.         else {
  58.             this->link->prev = newNode;
  59.             link = newNode;
  60.         }
  61.     }
  62.     bool removeNode(int val) {
  63.         Node* tmp = this->link;
  64.         while(tmp->hasNext() || tmp->getData() != val) {
  65.             tmp = tmp->next;
  66.         }
  67.         if (tmp->getData() == val) {
  68.             cout << tmp->prev->next->getData();
  69.             tmp->prev->next = tmp->next;
  70.             cout << tmp->next->prev->getData();
  71.             tmp->next->prev = tmp->prev;
  72.             return true;
  73.         } else {
  74.             return false;
  75.         }
  76.     }
  77.     void output() {
  78.         if (NULL == this->link) {
  79.             cout << "List is empty!" << endl;
  80.         } else {
  81.             cout << "List:";
  82.             Node tmp = *link;
  83.             int a = tmp.getData();
  84.             cout << a;
  85.             while (tmp.hasNext()) {
  86.                 tmp = *tmp.nextNode();
  87.                 cout << ", " << tmp.getData();
  88.             }
  89.             cout << endl;
  90.         }
  91.     }
  92. };
  93. string action[] = { "1 - Add new node\n", "2 - Remove node\n", "3 - Print list\n", "4 - \n", "0 - Exit\n"};
  94.  
  95. int main() {
  96. freopen("in.txt", "r", stdin);//тут был Иосиф
  97.     srand(static_cast<unsigned int>(time(0)));
  98.     List* worklist = new List();
  99.     bool stop = false;//и тут тоже
  100.     while (!stop) {
  101.         system("cls");
  102.         int choose;
  103.         for(string i : action) {
  104.             cout << i;
  105.         }
  106.         cin >> choose;
  107.         switch (choose) {
  108.         case 0: {
  109.             stop = true;
  110.             break;
  111.         }
  112.         case 1: {
  113.             cout << "Enter root value: ";
  114.             int newNode;
  115.             cin >> newNode;
  116.             Node* node = new Node(newNode);
  117.             worklist->addLast(node);
  118.             //Sleep(200);
  119.             break;
  120.         }
  121.         case 2: {
  122.             cout << "Enter your number: ";
  123.             int n;
  124.             cin >> n;
  125.             if (worklist->removeNode(n)) {
  126.                 cout << "Success remove!" << endl;
  127.             } else {
  128.                 cout << "Failure remove! Node is don't found." << endl;
  129.             }
  130.             //Sleep(300);
  131.             break;
  132.         }
  133.         case 3: {
  134.             worklist->output();
  135.             //Sleep(3000);
  136.             break;
  137.         }
  138.         default:
  139.             break;
  140.         }
  141.     }
  142. }
Add Comment
Please, Sign In to add comment