Advertisement
35657

Untitled

Jun 14th, 2023
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 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. private:
  42.     int size = 0;
  43.     Node* head = nullptr;
  44. };
  45.  
  46. int main() {
  47.     ForwardList list1;
  48.  
  49.     for (int i = 0; i < 10; i++) {
  50.         list1.Push_front(i + 1);
  51.         list1.Print();
  52.     }
  53.  
  54.     cout << list1.Size() << endl;
  55.  
  56.     for (int i = 0; i < 3; i++) {
  57.         list1.Pop_front();
  58.         list1.Print();
  59.     }
  60.  
  61.     cout << list1.Size() << endl;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement