hpnq

dasha

Sep 16th, 2024
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6. struct Node {
  7.     int value;
  8.     Node* next;
  9.     Node(int val) : value(val), next(nullptr) {}
  10. };
  11. class TList {
  12.  
  13. private:
  14.     string name;
  15.  
  16.  
  17.  
  18.     Node* head;
  19.     int count;
  20.  
  21. public:
  22.     // конструктор без параметров
  23.     TList() : name("Unnamed List"), head(nullptr), count(0) {}
  24.  
  25.     // конструктор с параметрами
  26.     TList(const string& listName) : name(listName), head(nullptr), count(0) {}
  27.  
  28.     // конструктор копирования
  29.     TList(const TList& other) : name(other.name), head(nullptr), count(other.count) {
  30.         if (other.head) {
  31.             head = new Node(other.head->value);
  32.             Node* srcNode = other.head->next;
  33.             Node* destNode = head;
  34.             while (srcNode) {
  35.                 destNode->next = new Node(srcNode->value);
  36.                 destNode = destNode->next;
  37.                 srcNode = srcNode->next;
  38.             }
  39.         }
  40.     }
  41.  
  42.     // деструктор
  43.     ~TList() {
  44.         clear();
  45.     }
  46.  
  47.     // распечатка элементов списка на экран
  48.     void print() const {
  49.         Node* current = head;
  50.         cout << "Список (" << name << "): ";
  51.         while (current) {
  52.             cout << current->value << " ";
  53.             current = current->next;
  54.         }
  55.         cout << endl;
  56.     }
  57.  
  58.     // распечатка элемента списка с заданным номером на экран
  59.     void printElement(int index) const {
  60.         if (index <= 0 || index > count) {
  61.             cout << "Такого индекса нет" << endl;
  62.             return;
  63.         }
  64.         Node* current = head;
  65.         for (int i = 0; (i+1) < index; i++) {
  66.             current = current->next;
  67.         }
  68.         cout << "Элемент под номером " << index << ": " << current->value<< endl;
  69.     }
  70.  
  71.     // выдача количества элементов списка
  72.     int getCount() const {
  73.         return count;
  74.     }
  75.  
  76.     // добавление элемента в конец списка
  77.     void append(int value) {
  78.         Node* newNode = new Node(value);
  79.         if (!head) {
  80.             head = newNode;
  81.         }
  82.         else {
  83.             Node* current = head;
  84.             while (current->next) {
  85.                 current = current->next;
  86.             }
  87.             current->next = newNode;
  88.         }
  89.         ++count;
  90.     }
  91.  
  92.     // удаление элемента с начала списка
  93.     void removeFirst() {
  94.         if (!head) {
  95.             cout << "Список пуст" << endl;
  96.             return;
  97.         }
  98.         Node* temp = head;
  99.         head = head->next;
  100.         delete temp;
  101.         --count;
  102.     }
  103.  
  104.     // очистка всего списка
  105.     void clear() {
  106.         while (head) {
  107.             removeFirst();
  108.         }
  109.     }
  110.  
  111.     //добавление к данному списку второго списка
  112.     void appendList(const TList& other) {
  113.         Node* current = other.head;
  114.         while (current) {
  115.             append(current->value);
  116.             current = current->next;
  117.         }
  118.     }
  119.  
  120.     // сравнение количества элементов данного списка с другим списком
  121.     string compareCount(const TList& other) const {
  122.         if (count > other.count) {
  123.             return "больше";
  124.         }
  125.         else if (count < other.count) {
  126.             return "меньше";
  127.         }
  128.         else {
  129.             return "равен";
  130.         }
  131.     }
  132.  
  133.     TList& operator =(const TList& list);
  134.     string operator ==(const TList& list); //+
  135.  
  136.     friend std::ostream& operator << (std::ostream &os, const TList &tl);
  137.     friend std::istream& operator >> (std::istream &in,TList &tl);
  138.  
  139.     TList& operator ++(int); //+
  140.     TList& operator --(); //+
  141.     bool operator >(const TList& list); //+
  142.     TList operator+(const TList& list);
  143.     TList& operator+=(const TList& list); //+
  144.     TList operator-(const TList& list);
  145. };
  146.  
  147.  
  148.  
  149.  
  150. TList& TList::operator =(const TList& list)
  151. {
  152.     TList temp;
  153.     temp.name = list.name;
  154.     temp.head = list.head;
  155.     temp.count = list.count;
  156.     return temp;
  157. }
  158. std::ostream& operator << (std::ostream &os, const TList &tl){
  159. //    ts.
  160.     Node* x = tl.head;
  161.     while(x != nullptr){
  162.         os << x->value;
  163.         x = x->next;
  164.     }
  165.  
  166.     return os << endl;
  167. }
  168.  
  169. std::istream& operator >> (std::istream &in,TList &tl){
  170.     cout << "кол-во элементов:\n";
  171.     int n;
  172.     in >> n;
  173. //    Node* x = tl.head;
  174.     tl.count = n;
  175.     Node* head;
  176.     cout << "элементы:\n";
  177.  
  178.     for(int i = 0; i < n; i++){
  179.         // спиздил с append'а
  180.         int value;
  181.         in >> value;
  182.         Node* newNode = new Node(value);
  183.         if (!head) {
  184.             head = newNode;
  185.         }
  186.         else {
  187.             Node* current = head;
  188.             while (current->next) {
  189.                 current = current->next;
  190.             }
  191.             current->next = newNode;
  192.         }
  193.  
  194.  
  195.     }
  196.     return in;
  197. }
  198.  
  199.  
  200. string TList::operator ==(const TList& list)
  201. {
  202.     return compareCount(list);
  203. }
  204.  
  205. TList& TList::operator ++(int)
  206. {
  207.     append(1);
  208.     return *this;
  209. };
  210.  
  211. TList& TList::operator --()
  212. {
  213.     removeFirst();
  214.     return *this;
  215. };
  216.  
  217. bool TList::operator>(const TList& list) {
  218.     if (count > list.count)
  219.         return 0;
  220.     return 1;
  221. }
  222.  
  223. TList& TList::operator+=(const TList& list)
  224. {
  225.     appendList(list);
  226.     return *this;
  227. }
  228.  
  229. TList TList::operator+(const TList& list) {
  230.     TList result;
  231.     result.name = name + " + " + list.name;
  232.  
  233.     // Объединяем узлы двух списков
  234.     Node* current1 = head;
  235.     while (current1 != nullptr) {
  236.         result.append(current1->value);
  237.         current1 = current1->next;
  238.     }
  239.  
  240.     Node* current2 = list.head;
  241.     while (current2 != nullptr) {
  242.         result.append(current2->value);
  243.         current2 = current2->next;
  244.     }
  245.  
  246.     return result;
  247. }
  248.  
  249.  
  250. TList TList::operator-(const TList& other) {
  251.     TList result;
  252.     result.name = name + " - " + other.name;
  253.  
  254.     // Добавляем в результат узлы первого списка
  255.    
  256.     Node* current1 = head;
  257.     while (current1 != nullptr) {
  258.         result.append(current1->value);
  259.         current1 = current1->next;
  260.     }
  261.  
  262.     // Удаляем из результата узлы, которые есть во втором списке
  263.     Node* current2 = other.head;
  264.     while (current2 != nullptr) {
  265.         result.remove(current2->value);
  266.         current2 = current2->next;
  267.     }
  268.  
  269.     return result;
  270. }
  271.  
  272. int main() {
  273.  
  274.     SetConsoleCP(1251);
  275.     SetConsoleOutputCP(1251);
  276.  
  277.     TList list1("List1");
  278.     list1++; //добавление элемента в конец списка
  279.     list1.append(60);
  280.     list1.print();
  281.  
  282.     TList list2("List2");
  283.     list2.append(40);
  284.     list2.append(50);
  285.     list2.print();
  286.  
  287.     TList list3 = list1;
  288.     list3.print();
  289.  
  290.     --list1; //уменьшение количества элементов на 1
  291.     cout << "Список после удаления первого элемента: " << endl;
  292.     list1.print();
  293.  
  294.     list1+=list2; //добавление к первому списку значений второго списка
  295.     cout << "Cписок после добавления к нему второго: " << endl;
  296.     list1.print();
  297.  
  298.     cout << "list1 count: " << list1.getCount() << endl;
  299.     cout << "list2 count: " << list2.getCount() << endl;
  300.  
  301.     cout << "list1 > list2? " << (list2 > list1) << endl; //проверка на то, что количество элементов данного
  302.     //списка больше количества элементов другого списка,
  303.  
  304.     cout << "list1 " << (list1 == list2) << " list2" << endl; //проверка на равенство двух списков
  305.  
  306.     list1.printElement(1);
  307.     cout << (list1 + list2) << endl;
  308.     list1.clear();
  309.     list1.print();
  310.  
  311.     return 0;
  312. }
  313.  
Advertisement
Add Comment
Please, Sign In to add comment