2070 / C++ / Списки / Черга

 

Черга (2 класи)
#include <iostream>
using namespace std;
  
class Node {
 public:
  void SetA(int b)
  {
    a = b;
  }
  void SetNext(Node* n)
  {
    next = n;
  }
  int GetA()
  {
    return a;
  }
  Node* GetNext()
  {
    return next;
  }
 private:
  int a;
  Node* next;
};

class List {
 public:
  void Add(int c)
  {
    Node* tmp = new Node();
    tmp->SetA(c);
    tmp->SetNext(nullptr);
    if (head == nullptr) {
      head = tmp;
    }
    else 
    {
      Node* tmp2 = head;
      while (tmp2->GetNext() != nullptr)
      {
        tmp2 = tmp2->GetNext();
      }
      tmp2->SetNext(tmp);
    }
  }
  void Show()
  {
    Node* tmp = head;
    while (tmp != nullptr)
    {
      cout << tmp->GetA() << endl;
      tmp = tmp->GetNext();
    }
  }
 private:
  Node* head = nullptr;
};

int main()
{
  List list;
  list.Add(7);
  list.Add(8);
  list.Add(9);
  list.Show();

  system("pause");
  return 0;
}
7
8
9