hpnq

BOOT_RINGS

Oct 28th, 2024
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template <typename T>
  5. class SingleLinkedRing {
  6. private:
  7.     struct Node {
  8.         T data;
  9.         Node* next;
  10.  
  11.         Node(const T& value) : data(value), next(nullptr) {}
  12.     };
  13.  
  14.     Node* head;
  15.  
  16. public:
  17.     SingleLinkedRing() : head(nullptr) {}
  18.  
  19.     ~SingleLinkedRing() {
  20.         clear();
  21.     }
  22.  
  23.     // Метод добавления элемента в кольцо
  24.     void add(const T& value) {
  25.         Node* newNode = new Node(value);
  26.         if (!head) {
  27.             head = newNode;
  28.             head->next = head;
  29.         } else {
  30.             Node* temp = head;
  31.             while (temp->next != head) {
  32.                 temp = temp->next;
  33.             }
  34.             temp->next = newNode;
  35.             newNode->next = head;
  36.         }
  37.     }
  38.  
  39.     // Печать кольца
  40.     void print() const {
  41.         if (!head) return;
  42.         Node* temp = head;
  43.         do {
  44.             std::cout << temp->data << " ";
  45.             temp = temp->next;
  46.         } while (temp != head);
  47.         std::cout << std::endl;
  48.     }
  49.  
  50.     // Подсчет количества вхождений заданного элемента
  51.     int countOccurrences(const T& value) const {
  52.         if (!head) return 0;
  53.         int count = 0;
  54.         Node* temp = head;
  55.         do {
  56.             if (temp->data == value) {
  57.                 count++;
  58.             }
  59.             temp = temp->next;
  60.         } while (temp != head);
  61.         return count;
  62.     }
  63.  
  64.     // Поиск самого часто встречающегося элемента
  65.     T findMostFrequent() const {
  66.         if (!head) throw std::runtime_error("Ring is empty.");
  67.  
  68.         T mostFrequent = head->data;
  69.         int maxCount = 0;
  70.  
  71.         Node* outer = head;
  72.         do {
  73.             int count = 0;
  74.             Node* inner = head;
  75.             do {
  76.                 if (inner->data == outer->data) {
  77.                     count++;
  78.                 }
  79.                 inner = inner->next;
  80.             } while (inner != head);
  81.  
  82.             if (count > maxCount) {
  83.                 maxCount = count;
  84.                 mostFrequent = outer->data;
  85.             }
  86.             outer = outer->next;
  87.         } while (outer != head);
  88.  
  89.         return mostFrequent;
  90.     }
  91.  
  92.     // Очистка кольца
  93.     void clear() {
  94.         if (!head) return;
  95.         Node* temp = head;
  96.         do {
  97.             Node* nextNode = temp->next;
  98.             delete temp;
  99.             temp = nextNode;
  100.         } while (temp != head);
  101.         head = nullptr;
  102.     }
  103. };
  104.  
  105. // Функция для работы с кольцом целых чисел
  106. void processIntegerRing() {
  107.     SingleLinkedRing<int> intRing;
  108.     intRing.add(1);
  109.     intRing.add(2);
  110.     intRing.add(3);
  111.     intRing.add(1);
  112.     intRing.add(2);
  113.  
  114.     std::cout << "Integer ring: ";
  115.     intRing.print();
  116.  
  117.     int target = 2;
  118.     std::cout << "Occurrences of " << target << ": " << intRing.countOccurrences(target) << std::endl;
  119.  
  120.     std::cout << "Most frequent integer: " << intRing.findMostFrequent() << std::endl;
  121.  
  122.     intRing.clear();
  123. }
  124.  
  125. // Функция для работы с кольцом строк
  126. void processStringRing() {
  127.     SingleLinkedRing<std::string> stringRing;
  128.     stringRing.add("apple");
  129.     stringRing.add("banana");
  130.     stringRing.add("apple");
  131.     stringRing.add("cherry");
  132.     stringRing.add("banana");
  133.  
  134.     std::cout << "String ring: ";
  135.     stringRing.print();
  136.  
  137.     std::string target = "apple";
  138.     std::cout << "Occurrences of \"" << target << "\": " << stringRing.countOccurrences(target) << std::endl;
  139.  
  140.     std::cout << "Most frequent string: " << stringRing.findMostFrequent() << std::endl;
  141.  
  142.     stringRing.clear();
  143. }
  144.  
  145. int main() {
  146.     std::cout << "integer ring:" << std::endl;
  147.     processIntegerRing();
  148.  
  149.     std::cout << "\nstring ring:" << std::endl;
  150.     processStringRing();
  151.  
  152.     return 0;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment