35657

Untitled

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