35657

Untitled

Jun 14th, 2023
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. struct Node { //односвязный список состоит из узлов
  7.     int value; // узел хранит информативную часть
  8.     Node* next; // и указатель на следующий узел в списке
  9. };
  10.  
  11. class ForwardList {
  12. public:
  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 Size() const {
  38.         return size;
  39.     }
  40.  
  41.     int Front() const {
  42.         if (head != nullptr) {
  43.             return head->value;
  44.         }
  45.     }
  46.  
  47.     void Insert(const int index, const int& value) {
  48.         if (index == 0) {
  49.             Push_front(value);
  50.             return;
  51.         }
  52.         if (index > 0 && index < size) {
  53.             Node* temp = head;
  54.             for (int i = 0; i < index - 1; i++) {
  55.                 temp = temp->next;
  56.             }
  57.             temp->next = new Node{ value, temp->next };
  58.             size++;
  59.         }
  60.     }
  61.  
  62.     void Erase(const int index) {
  63.         if (index == 0) {
  64.             Pop_front();
  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.             Node* buf = temp->next->next;
  73.             delete temp->next;
  74.             temp->next = buf;
  75.             size--;
  76.         }
  77.     }
  78.  
  79. private:
  80.     int size = 0;
  81.     Node* head = nullptr;
  82. };
  83.  
  84. int main() {
  85.     ForwardList list1;
  86.  
  87.     for (int i = 0; i < 10; i++) {
  88.         list1.Push_front(i + 1);
  89.         list1.Print();
  90.     }
  91.  
  92.     cout << list1.Size() << endl;
  93.  
  94.     for (int i = 0; i < 3; i++) {
  95.         list1.Pop_front();
  96.         list1.Print();
  97.     }
  98.  
  99.     list1.Insert(2, 12);
  100.  
  101.     list1.Print();
  102.  
  103.  
  104.     list1.Erase(3);
  105.  
  106.     list1.Print();
  107.  
  108.     cout << list1.Size() << endl;
  109.  
  110.     cout << list1.Front() << endl;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment