Toxotsist

Сиаод 2 Релиз

Apr 7th, 2021 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct element {
  6.     char x;
  7.     element* next = nullptr;
  8. };
  9.  
  10. class List {
  11.     element* head;
  12.     int size;
  13. public:
  14.     List() { head = nullptr; size = 0; };
  15.     ~List();
  16.     char& operator[](int index);
  17.     int pop();
  18.     void pushBack(char x);
  19.     void pushToHead(char x);
  20.     void equal(List *L2);
  21.     void cleaner();
  22.     void print();
  23. private:
  24.  
  25. };
  26.  
  27. char &List::operator[](int index) {
  28.     element* current = head;
  29.     int counter = 0;
  30.     while (current != nullptr) {
  31.         if (counter == index) {
  32.             return current->x;
  33.         }
  34.         current = current->next;
  35.         counter++;
  36.     }
  37. }
  38.  
  39. List::~List() {
  40.     while (head != nullptr) {
  41.         element* temp = head->next;
  42.         delete head;
  43.         head = temp;
  44.     }
  45. };
  46.  
  47. int List::pop() {
  48.     if (size == 0) { return (int()); }
  49.     if (size == 1) { return head->x; head = nullptr; }
  50.     else {
  51.         element* current = head;
  52.         for (int i = 0; i < size - 2; i++) { current = current->next; }
  53.         int i = (*this)[size - 1];
  54.         current->next = current->next->next;
  55.         size--;
  56.         return i;
  57.     }
  58. }
  59.  
  60.  
  61. void List::pushBack(char x) {
  62.     size++;
  63.     if (head == nullptr) {
  64.         head = new element;
  65.         head->x = x;
  66.     }
  67.     else {
  68.         element* current = head;
  69.         while (current->next != nullptr) {
  70.             current = current->next;
  71.         }
  72.         current->next = new element;
  73.         current->next->x = x;
  74.     }
  75. };
  76.  
  77. void List::pushToHead(char x) {
  78.     size++;
  79.     element* temp = new element;
  80.     temp->x = x;
  81.     temp->next = head;
  82.     head = temp;
  83. };
  84.  
  85. void List::print() {
  86.     element* temp = head;
  87.     while (temp != nullptr) {
  88.         cout << temp->x << " ";
  89.         temp = temp->next;
  90.     }
  91.     printf("\n");
  92. };
  93.  
  94. void List::cleaner() {
  95.     List* nList = new List();
  96.     element* current = this->head;
  97.     for (int i = 0; i < size; i++) {
  98.         if (isalpha(current->x)) { nList->pushBack(current->x); }
  99.         current = current->next;
  100.     }
  101.     *this = *nList;
  102. }
  103.  
  104. void List::equal(List* L2) {
  105.     bool status = true;
  106.     if (this->size != L2->size)
  107.     {
  108.         status = false;
  109.     }
  110.     element* current1 = this->head;
  111.     element* current2 = L2->head;
  112.  
  113.     while (current1 != nullptr && current2 != nullptr)
  114.     {
  115.         if (current1->x == current2->x) {
  116.             current1 = current1->next;
  117.             current2 = current2->next;
  118.         }
  119.         else {
  120.             status = false;
  121.         }
  122.     }
  123.     if (status == true) {
  124.         cout << "EQUALITY = " << status << endl;
  125.     }
  126. }
  127.  
  128. int main() {
  129.     setlocale(0, "");
  130.     List L1;
  131.     List L2;
  132.     L1.pushBack('A');
  133.     L1.pushBack('B');
  134.     L1.pushBack('C');
  135.     L2.pushBack('A');
  136.     L2.pushBack('B');
  137.     L2.pushBack('C');
  138.     L1.equal(&L2);
  139.     L1.print();
  140.     L2.print();
  141.     printf("Добавим целочисленный элемент с конца и попробуем его удалить\n");
  142.     L1.pushBack('1');
  143.     L1.print();
  144.     L1.cleaner();
  145.     L1.print();
  146.     printf("Реализуем вставку последнего символа L2 в L1\n");
  147.     L1.pushBack(L2.pop());
  148.     L1.print();
  149.     return 0;
  150. }
Add Comment
Please, Sign In to add comment