35657

Untitled

Jul 7th, 2023
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class ForwardList {
  7. public:
  8.  
  9.     ForwardList() : size(0), head(nullptr) {}
  10.  
  11.     struct Node { //односвязный список состоит из узлов
  12.         T value; // узел хранит информативную часть
  13.         Node* next; // и указатель на следующий узел в списке
  14.     };
  15.  
  16.     void Push_front(const T& value) {
  17.         head = new Node{ value, head };
  18.         size++;
  19.     }
  20.  
  21.     void Pop_front() {
  22.         if (size > 0) {
  23.             Node* temp = head;
  24.             head = head->next;
  25.             delete temp;
  26.             size--;
  27.         }
  28.     }
  29.  
  30.     void Print() {
  31.         Node* temp = head;
  32.         while (temp != nullptr) {
  33.             cout << temp->value << " ";
  34.             temp = temp->next;
  35.         }
  36.         cout << endl;
  37.     }
  38.  
  39.     T& Front() {
  40.         if (head != nullptr) {
  41.             return head->value;
  42.         }
  43.     }
  44.  
  45.     T& Front() const {
  46.         if (head != nullptr) {
  47.             return head->value;
  48.         }
  49.     }
  50.  
  51.     int Find(const T value) {
  52.         int i = 0;
  53.         Node* temp = head;
  54.         while (temp != nullptr) {
  55.             if (temp->value == value) {
  56.                 return i;
  57.             }
  58.             temp = temp->next;
  59.             i++;
  60.         }
  61.         return -1;
  62.     }
  63.  
  64.     void Insert(const int index, const T value) {
  65.         if (index == 0) {
  66.             Push_front(value);
  67.             return;
  68.         }
  69.         if (index > 0 && index <= size) {
  70.             Node* temp = head;
  71.             for (int i = 0; i < index - 1; i++) {
  72.                 temp = temp->next;
  73.             }
  74.             temp->next = new Node{ value, temp->next };
  75.             size++;
  76.         }
  77.     }
  78.  
  79.     void Erase(const int index) {
  80.         if (index == 0) {
  81.             Pop_front();
  82.             return;
  83.         }
  84.         if (index > 0 && index < size) {
  85.             Node* temp = head;
  86.             for (int i = 0; i < index - 1; i++) {
  87.                 temp = temp->next;
  88.             }
  89.             Node* buf = temp->next->next;
  90.             delete temp->next;
  91.             temp->next = buf;
  92.             size--;
  93.         }
  94.     }
  95.  
  96.     void Clear() {
  97.         while (head != nullptr) {
  98.             Pop_front();
  99.         }
  100.     }
  101.  
  102.     int Size() const {
  103.         return size;
  104.     }
  105.  
  106.     bool operator==(const ForwardList& other) {
  107.         if (size != other.size) {
  108.             return false;
  109.         }
  110.         Node* temp = head;
  111.         Node* other_temp = other.head;
  112.         while (temp != nullptr) {
  113.             if (temp->value != other_temp->value) {
  114.                 return false;
  115.             }
  116.             temp = temp->next;
  117.             other_temp = other_temp->next;
  118.         }
  119.         return true;
  120.     }
  121.  
  122.     bool operator!=(const ForwardList& other) {
  123.         return !(*this == other);
  124.     }
  125.     /*
  126.     Node* temp = head; //указатель на узел
  127.  
  128.     *temp; // получение доступа ко всему узлу через разименовани
  129.  
  130.     temp->next; // получение доступа к полю next через разыменование только этого поля
  131.     */
  132.  
  133.     ~ForwardList() {
  134.         Clear();
  135.     }
  136.  
  137. private:
  138.     int size;
  139.     Node* head;
  140. };
  141.  
  142. int main() {
  143.     ForwardList<int> list1;
  144.  
  145.     for (int i = 0; i < 10; i++) {
  146.         list1.Push_front(i + 1);
  147.     }
  148.     list1.Print();
  149.     cout << list1.Size() << endl;
  150.  
  151.     for (int i = 0; i < 3; i++) {
  152.         list1.Pop_front();
  153.     }
  154.     list1.Print();
  155.     cout << "size = " << list1.Size() << endl;
  156.  
  157.     cout << list1.Front() << endl;
  158.     cout << list1.Find(5) << endl;
  159.  
  160.     list1.Insert(3, 10);
  161.     list1.Print();
  162.     list1.Erase(3);
  163.     list1.Print();
  164.     cout << "=============" << endl;
  165.     list1.Clear();
  166.     list1.Print();
  167. }
Advertisement
Add Comment
Please, Sign In to add comment