fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node
  5. {
  6. private:
  7. int value = 0;
  8. Node* next = nullptr;
  9.  
  10. public:
  11. Node(int v = 0, Node* n = nullptr);
  12. int getValue() const;
  13. void setValue(int v);
  14. Node* getNext() const;
  15. void setNext(Node* n);
  16.  
  17. friend class virticalList;
  18. };
  19.  
  20. Node::Node(int v, Node* n) :
  21. value(v),
  22. next(n)
  23. {
  24. }
  25.  
  26. int Node::getValue() const
  27. {
  28. return value;
  29. }
  30.  
  31. void Node::setValue(int v)
  32. {
  33. value = v;
  34. }
  35.  
  36. Node* Node::getNext() const
  37. {
  38. return next;
  39. }
  40.  
  41. void Node::setNext(Node* n)
  42. {
  43. next = n;
  44. }
  45.  
  46. class virticalList
  47. {
  48. private:
  49. Node* head = nullptr;
  50.  
  51. public:
  52. virticalList();
  53. virticalList(const virticalList &p2);
  54. ~virticalList();
  55.  
  56. virticalList& operator=(const virticalList& p2);
  57.  
  58. void print() const;
  59. void virtInc();
  60. };
  61.  
  62. virticalList::virticalList()
  63. {
  64. Node** ptr = &head;
  65. for(int i = 10; i < 20; ++i)
  66. {
  67. *ptr = new Node(i);
  68. ptr = &((*ptr)->next);
  69. }
  70. }
  71.  
  72. virticalList::virticalList(const virticalList &p2) {
  73. Node** curr = &head;
  74. Node* temp = p2.head;
  75. while (temp) {
  76. *curr = new Node(temp->getValue());
  77. curr = &((*curr)->next);
  78. temp = temp->getNext();
  79. }
  80. }
  81.  
  82. virticalList::~virticalList() {
  83. Node* ptr = head, *next;
  84. while (ptr) {
  85. next = ptr->getNext();
  86. delete ptr;
  87. ptr = next;
  88. }
  89. }
  90.  
  91. virticalList& virticalList::operator=(const virticalList& p2)
  92. {
  93. if (this != &p2) {
  94. virticalList temp(p2);
  95. //std::swap(head, temp.head);
  96. Node* ptr = head;
  97. head = temp.head;
  98. temp.head = ptr;
  99. }
  100. return *this;
  101. }
  102.  
  103. void virticalList::print() const
  104. {
  105. Node* ptr = head;
  106. while (ptr) {
  107. cout << ptr->getValue() << " ";
  108. ptr = ptr->getNext();
  109. }
  110. cout << "\n";
  111. }
  112.  
  113. void virticalList::virtInc()
  114. {
  115. Node* ptr = head;
  116. while (ptr) {
  117. ptr->setValue(ptr->getValue()+1);
  118. ptr = ptr->getNext();
  119. }
  120. }
  121.  
  122. int main() {
  123. virticalList l1, l2;
  124. l2.virtInc();
  125.  
  126. l1.print();
  127. l1 = l2;
  128. l1.print();
  129.  
  130. return 0;
  131. }
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
10 11 12 13 14 15 16 17 18 19 
11 12 13 14 15 16 17 18 19 20