Advertisement
Nikidrr

Лаба 10

May 27th, 2019
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. struct Node {
  8.     string value;
  9.     Node *next;
  10.  
  11.     Node(string _value, Node *_next = nullptr)
  12.         : value(_value), next(_next) {}
  13. };
  14.  
  15. struct List {
  16.     Node *root = nullptr;
  17.  
  18.     string toString() {
  19.         Node *current = this->root;
  20.         ostringstream sstream;
  21.         sstream << "[";
  22.         if (current == nullptr) {
  23.             sstream << "]";
  24.         }
  25.         while (current != nullptr) {
  26.             sstream << current->value;
  27.             current = current->next;
  28.             if (current == nullptr) {
  29.                 sstream << "]";
  30.             } else {
  31.                 sstream << ", ";
  32.             }
  33.         }
  34.         return sstream.str();
  35.     }
  36.  
  37.     int length() {
  38.         int length = 0;
  39.         Node *current = this->root;
  40.         while (current != nullptr) {
  41.             length++;
  42.             current = current->next;
  43.         }
  44.         return length;
  45.     }
  46.  
  47.     Node* last() {
  48.         Node *current = this->root;
  49.         while (current != nullptr) {
  50.             if (current->next == nullptr) {
  51.                 return current;
  52.             }
  53.             current = current->next;
  54.         }
  55.         return nullptr;
  56.     }
  57.  
  58.     void add(string value) {
  59.         Node *_new = new Node(value);
  60.         Node *last = this->last();
  61.         if (last == nullptr) {
  62.             this->root = _new;
  63.         } else {
  64.             last->next = _new;
  65.         }
  66.     }
  67.  
  68.     Node* nodeAt(int index) {
  69.         Node* current = this->root;
  70.         for (
  71.             int i = 0;
  72.             i != index && current != nullptr;
  73.             i++, current = current->next
  74.         );
  75.         return current;
  76.     }
  77.  
  78.     Node* findNode(string value) {
  79.         Node* current = this->root;
  80.         for (;
  81.             current != nullptr && current->value != value;
  82.             current = current->next
  83.         );
  84.         return current;
  85.     }
  86.  
  87.     // Задание 1-Н
  88.     void removeAllAfter(string value) {
  89.         Node* node = this->findNode(value);
  90.         if (node != nullptr) {
  91.             node->next = nullptr;
  92.         }
  93.     }
  94.  
  95.     // Задание 2-А
  96.     void replaceAt(int k, string value) {
  97.         Node* node = this->nodeAt(k);
  98.         if (node != nullptr) {
  99.             node->value = value;
  100.         }
  101.     }
  102.  
  103.     // Задание 3-А
  104.     int countStartingWith(char letter) {
  105.         int count = 0;
  106.         for (
  107.             Node* current = this->root;
  108.             current != nullptr;
  109.             current = current->next
  110.         ) {
  111.             if (current->value[0] == letter) {
  112.                 count++;
  113.             }
  114.         }
  115.         return count;
  116.     }
  117.  
  118.     // Задание 4-В
  119.     string* toArray(int k) {
  120.         string* arr = new string[k];
  121.         Node* current = this->root;
  122.         for (
  123.             int i = 0;
  124.             i < k && current != nullptr;
  125.             i++, current = current->next
  126.         ) {
  127.             arr[i] = current->value;
  128.         }
  129.         return arr;
  130.     }
  131. };
  132.  
  133. int main() {
  134.     List list;
  135.  
  136.     list.add("ABC");
  137.     list.add("ACD");
  138.     list.add("DBC");
  139.     list.add("BCD");
  140.     list.add("XYZ");
  141.  
  142.     cout << list.toString() << " [" << list.length() << "]" << endl;
  143.  
  144.     list.removeAllAfter("DBC");
  145.  
  146.     cout << list.toString() << " [" << list.length() << "]" << endl;
  147.  
  148.     list.replaceAt(2, "dbc");
  149.  
  150.     cout << list.toString() << " [" << list.length() << "]" << endl;
  151.  
  152.     cout << "Starting with A: " << list.countStartingWith('A') << endl;
  153.  
  154.     string* arr = list.toArray(2);
  155.  
  156.     for (int i = 0; i < 2; i++) {
  157.         cout << "> [" << i << "]: " << arr[i] << endl;
  158.     }
  159. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement