fork(1) 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.  
  18. Node::Node(int v, Node* n) :
  19. value(v),
  20. next(n)
  21. {
  22. }
  23.  
  24. int Node::getValue() const
  25. {
  26. return value;
  27. }
  28.  
  29. void Node::setValue(int v)
  30. {
  31. value = v;
  32. }
  33.  
  34. Node* Node::getNext() const
  35. {
  36. return next;
  37. }
  38.  
  39. void Node::setNext(Node* n)
  40. {
  41. next = n;
  42. }
  43.  
  44. class virticalList
  45. {
  46. private:
  47. Node* head = nullptr;
  48.  
  49. public:
  50. virticalList();
  51. virticalList(const virticalList &p2);
  52. ~virticalList();
  53.  
  54. virticalList& operator=(const virticalList& p2);
  55.  
  56. void print() const;
  57. void virtInc();
  58. };
  59.  
  60. virticalList::virticalList() :
  61. head(new Node(10))
  62. {
  63. Node* ptr = head;
  64. for(int i = 11; i < 20; ++i)
  65. {
  66. ptr->setNext(new Node(i));
  67. ptr = ptr->getNext();
  68. }
  69. }
  70.  
  71. virticalList::virticalList(const virticalList &p2) {
  72. if (p2.head) {
  73. Node* temp = p2.head;
  74. Node* curr = head = new Node(temp->getValue());
  75. while (temp = temp->getNext()) {
  76. curr->setNext(new Node(temp->getValue()));
  77. curr = curr->getNext();
  78. }
  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 4448KB
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