fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class node
  6. {
  7. string name;
  8. node*next;
  9. public:
  10. friend class sll;
  11.  
  12. };
  13. class sll:public node
  14. {
  15. public:
  16. node*head;
  17. node*last;
  18. int*k;
  19. friend class node;
  20. sll()
  21. {
  22. head=nullptr;
  23. last=nullptr;
  24. }
  25. ~sll()
  26. {
  27. node*temp2;
  28. node*temp;
  29. temp = head;
  30. while(temp!=nullptr)
  31. {
  32. temp2 = temp->next;
  33. delete(temp);
  34. temp = temp2;
  35. }
  36. }
  37. void diag() {
  38. node*temp = head;
  39. cout << "Head:"<<head<<endl;
  40. cout << "Last:"<<last<<endl;
  41. while(temp!=nullptr)
  42. {
  43. cout << " Name: " <<temp->name <<endl;
  44. cout << " next: " <<temp->next <<endl;
  45. temp = temp->next;
  46. }
  47. }
  48. void input()
  49. {
  50.  
  51. node*temp;
  52. temp = new node;
  53. cout<<"Enter Name"<<endl;
  54. cin>>temp->name;
  55. if(head==nullptr)
  56. {
  57. head = temp;
  58. last = temp;
  59. }
  60. else
  61. {
  62. last->next = temp;
  63. last = temp;
  64. }
  65.  
  66. }
  67. sll& operator=(const sll& o)
  68. {
  69. node*temp;
  70. temp = o.head;
  71. while(temp!=nullptr)
  72. {
  73. node *clone = new node(*temp);
  74. if(head==nullptr)
  75. {
  76. head = clone;
  77. last = clone;
  78. }
  79. else
  80. {
  81. last->next = clone;
  82. last = clone;
  83. }
  84.  
  85. temp = temp->next;
  86. }
  87. return *this;
  88. }
  89. void output()
  90. {
  91. cout<<"Output:"<<endl;
  92. node*temp;
  93. temp = head;
  94. while(temp!=nullptr)
  95. {
  96. cout<<temp->name<<" ";
  97. temp = temp->next;
  98. }
  99. cout <<endl;
  100. }
  101. sll &get()
  102. {
  103. return *this;
  104. }
  105. };
  106. int main()
  107. {
  108. sll obj,obj1;
  109. obj.diag();
  110. obj.input();
  111. obj.input();
  112. obj.diag();
  113. obj1 = obj.get();
  114. obj1.output();
  115. obj1.diag();
  116. return 0;
  117. }
Success #stdin #stdout 0s 4176KB
stdin
TEST1
TEST2
stdout
Head:0
Last:0
Enter Name
Enter Name
Head:0x56419d470c30
Last:0x56419d471c70
  Name:  TEST1
  next:  0x56419d471c70
  Name:  TEST2
  next:  0
Output:
TEST1 TEST2 
Head:0x56419d471ca0
Last:0x56419d471cd0
  Name:  TEST1
  next:  0x56419d471cd0
  Name:  TEST2
  next:  0