Advertisement
Norvager

LL

Sep 17th, 2021
886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include "windows.h"
  4. #include <vector>
  5. using namespace std;
  6. class Node {
  7.     int data = NULL;
  8. public:
  9.     Node* next = NULL;
  10.     Node* prev = NULL;
  11.     Node(int data) {
  12.         this->data = data;
  13.     }
  14.     Node() {}
  15.     bool hasNext() {
  16.         return NULL != this->next;
  17.     }
  18.     Node* nextNode() {
  19.         return this->next;
  20.     }
  21.     int getData() {
  22.         return this->data;
  23.     }
  24.     bool hasPrev() {
  25.         return NULL != this->prev;
  26.     }
  27.     Node* nextPrev() {
  28.         return this->prev;
  29.     }
  30. };
  31.  
  32. class List {
  33.     Node* link;
  34.     int count;
  35. public:
  36.     List(){
  37.         link = NULL;
  38.         count = 0;
  39.     }
  40.     Node* getLink() {
  41.         return this->link;
  42.     }
  43.     int getCount() {
  44.         return this->count;
  45.     }
  46.     void addLast(Node* newNode) {
  47.         this->count++;
  48.         if (NULL == this->link) {
  49.             this->link = newNode;
  50.         }
  51.         else {
  52.             Node* tmp = this->link;
  53.             while (tmp->hasNext()) {
  54.                 tmp = tmp->nextNode();
  55.             }
  56.             //this->link->next = newNode;
  57.             tmp->next = newNode;
  58.             tmp->next->prev = tmp;
  59.         }
  60.     }
  61.     bool removeNode(int val) {
  62.         Node* tmp = this->link;
  63.         while(tmp->hasNext() && tmp->getData() != val) {
  64.             tmp = tmp->next;
  65.         }
  66.         if (tmp->getData() == val) {
  67.             if (tmp == this->link) {
  68.                 this->link = tmp->next;
  69.             } else if (!tmp->hasNext()) {
  70.                 tmp->prev->next = NULL;
  71.             } else {
  72.                 tmp->prev->next = tmp->next;
  73.                 tmp->next->prev = tmp->prev;
  74.             }
  75.             this->count--;
  76.             return true;
  77.         } else {
  78.             return false;
  79.         }
  80.     }
  81.     Node* searchNodeByIndex (int ind) {
  82.         int i = 0;
  83.         Node* tmp = this->link;
  84.         while(i != ind) {
  85.             tmp = tmp->next;
  86.             i++;
  87.         }
  88.         return tmp;
  89.     }
  90.     int searchNodeByValue (int val) {
  91.         Node* tmp = this->link;
  92.         int ind = 0;
  93.         while(tmp->hasNext() && tmp->getData() != val) {
  94.             tmp = tmp->next;
  95.             ind++;
  96.         }
  97.         if (tmp->getData() == val) {
  98.             return ind;
  99.         } else {
  100.             return NULL;
  101.         }
  102.     }
  103.     bool clear() {
  104.         if(NULL == this->link){
  105.             return false;
  106.         } else {
  107.             this->link = NULL;
  108.             return true;
  109.         }
  110.     }
  111.     void merge(List* tmpList, int ind){
  112.         Node* tmpNode = this->searchNodeByIndex(ind);
  113.         Node* nextNode = tmpNode->next;
  114.         tmpNode->next = tmpList->getLink();
  115.         tmpList->getLink()->prev = tmpNode;
  116.         tmpNode = tmpList->searchNodeByIndex(tmpList->getCount()-1);
  117.         tmpNode->next = nextNode;
  118.         NULL != nextNode? nextNode->prev = tmpNode : nextNode = NULL;
  119.     }
  120.     void output() {
  121.         if (NULL == this->link) {
  122.             cout << "List is empty!" << endl;
  123.         } else {
  124.             cout << "List:\n";
  125.             Node tmp = *link;
  126.             int a = tmp.getData();
  127.             cout << a;
  128.             while (tmp.hasNext()) {
  129.                 tmp = *tmp.nextNode();
  130.                 cout << ", " << tmp.getData();
  131.             }
  132.             cout << endl;
  133.         }
  134.     }
  135. };
  136. List* creatorList(){
  137.     List* tmp = new List();
  138.     cout << "Enter all data nodes in the second list\n After last node enter \".\"\n";
  139.     string nodes;
  140.     cin >> nodes;
  141.     while("." != nodes){
  142.         Node* node = new Node(stoi(nodes));
  143.         tmp->addLast(node);
  144.         cin >> nodes;
  145.     }
  146.     return tmp;
  147. }
  148. string action[] = { "1 - Add new node\n",
  149.                     "2 - Remove node\n",
  150.                     "3 - Print list\n",
  151.                     "4 - Search by index\n",
  152.                     "5 - Clear\n",
  153.                     "6 - Merge\n",
  154.                     "7 - Search by value\n",
  155.                     "0 - Exit\n"
  156.                   };
  157.  
  158. int main() {
  159.     List* worklist = new List();//тут был Иосиф
  160.     bool stop = false;//и тут тоже
  161.     while (!stop) {
  162.         system("cls");
  163.         int choose;
  164.         for(string i : action) {
  165.             cout << i;
  166.         }
  167.         cin >> choose;
  168.         switch (choose) {
  169.         case 0: {
  170.             stop = true;
  171.             break;
  172.         }
  173.         case 1: {
  174.             cout << "Enter node's value: ";
  175.             int newNode;
  176.             cin >> newNode;
  177.             Node* node = new Node(newNode);
  178.             worklist->addLast(node);
  179.             cout << "Success!";
  180.             Sleep(300);
  181.             break;
  182.         }
  183.         case 2: {
  184.             cout << "Enter node's value for remove: ";
  185.             int n;
  186.             cin >> n;
  187.             if (worklist->removeNode(n)) {
  188.                 cout << "Success remove!" << endl;
  189.             } else {
  190.                 cout << "Failure remove! Node doesn't found." << endl;
  191.             }
  192.             Sleep(500);
  193.             break;
  194.         }
  195.         case 3: {
  196.             worklist->output();
  197.             Sleep(3000);
  198.             break;
  199.         }
  200.         case 4: {
  201.             int ind;
  202.             cout << "Enter node's index for search: ";
  203.             cin >> ind;
  204.             if (ind < worklist->getCount() && 0 <= ind){
  205.                 cout << "Node is found: "<< worklist->searchNodeByIndex(ind)->getData();
  206.             } else {
  207.                 cout << "Failure search! Index of bound exception";
  208.             }
  209.             Sleep(1500);
  210.             break;
  211.         }
  212.         case 5: {
  213.             if (worklist->clear()) {
  214.                 cout << "Success!";
  215.             } else {
  216.                 cout << "Failure clear! List is already empty";
  217.             }
  218.             Sleep(1500);
  219.             break;
  220.         }
  221.         case 6: {
  222.             List* tmp = creatorList();
  223.             tmp->output();
  224.             int ind;
  225.             cout << "Enter index node: ";
  226.             cin >> ind;
  227.             if (ind < worklist->getCount() && 0 <= ind){
  228.                 worklist->merge(tmp, ind);
  229.                 cout << "Success!";
  230.             } else {
  231.                 cout << "Failure search! Index of bound exception";
  232.             }
  233.             delete tmp;
  234.             Sleep(1500);
  235.             break;
  236.         }
  237.         case 7: {
  238.             int val;
  239.             cout << "Enter value node for search: ";
  240.             cin >> val;
  241.             int ind = worklist->searchNodeByValue(val);
  242.             if (NULL != ind){
  243.                 cout << "Node is found on index: " << ind;
  244.             } else {
  245.                 cout << "Failure search! Node doesn't found.";
  246.             }
  247.             Sleep(1500);
  248.             break;
  249.         }
  250.         default:
  251.             break;
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement