fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Node {
  6. public:
  7. int value;
  8. Node* next;
  9.  
  10. Node(int a) {
  11. value = a;
  12. next = nullptr;
  13. }
  14. };
  15.  
  16.  
  17. class ForwardList {
  18. public:
  19. Node* head;
  20. Node* tail; // последний элемент
  21.  
  22. ForwardList() {
  23. head = nullptr;
  24. tail = nullptr;
  25. }
  26.  
  27. void push_front(int value) {
  28. Node* node = new Node(value);
  29. node->next = head;
  30.  
  31. head = node;
  32.  
  33. if (tail == nullptr) {
  34. tail = node;
  35. }
  36. }
  37.  
  38. void push_back(int value) {
  39. Node* node = new Node(value);
  40.  
  41. if (head == nullptr) {
  42. head = node;
  43. }
  44.  
  45. if (tail != nullptr) {
  46. tail->next = node;
  47. }
  48.  
  49. tail = node;
  50.  
  51. }
  52. };
  53.  
  54.  
  55. int main() {
  56. ForwardList my_list;
  57. my_list.push_front(2);
  58. my_list.push_front(1);
  59. my_list.push_back(3);
  60.  
  61. cout << my_list.head->value << ", " << my_list.head->next->value << ", " << my_list.tail->value;
  62.  
  63. }
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
1, 2, 3