fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class List {
  5. private:
  6. struct Node {
  7. int data; // simplification
  8. Node *next;
  9.  
  10. Node(int d) {
  11. data = d;
  12. next = NULL;
  13. }
  14. };
  15.  
  16. protected:
  17. Node *head;
  18. Node *tail;
  19.  
  20. public:
  21. List(int d) : head(new Node(d)), tail(head) {}
  22.  
  23. void append(int d) {
  24. Node* n = new Node(d);
  25. tail->next = n;
  26. tail = n;
  27. }
  28.  
  29. List(const List& rhs) {
  30. if (head) delete head;
  31.  
  32. head=new Node(rhs.head->data);
  33.  
  34. Node* lhsCurrent = head;
  35. Node* rhsCurrent = rhs.head->next;
  36. do {
  37. lhsCurrent->next = new Node(rhsCurrent->data);
  38.  
  39. rhsCurrent = rhsCurrent->next;
  40. lhsCurrent = lhsCurrent->next;
  41. } while (rhsCurrent!=NULL);
  42.  
  43. tail = lhsCurrent;
  44. }
  45. };
  46.  
  47. int main() {
  48. List first(5);
  49. first.append(6);
  50.  
  51. List second(first);
  52. return 0;
  53. }
Runtime error #stdin #stdout 0s 15224KB
stdin
Standard input is empty
stdout
Standard output is empty