fork download
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. struct customer {
  5. void random_customer() {}
  6. };
  7.  
  8. class Node
  9. {
  10. private:
  11. customer data;
  12. Node *next;
  13. public:
  14. /*Nodes constructors*/
  15. Node(){next=nullptr;}
  16. Node(customer X)
  17. {
  18. data=X;
  19. next=nullptr;
  20. }
  21. /*Data setters and getters*/
  22. void set_Data(customer X)
  23. {data = X;}
  24. customer get_Data()
  25. {return data;}
  26. /*next setters and getters*/
  27. void set_next(Node * X){next=X;}
  28. Node* get_next(){return next;}
  29.  
  30.  
  31. };
  32.  
  33. class List
  34. {
  35. private:
  36. Node * head;
  37. Node * tail;
  38. int counter;
  39. public:
  40. /*Constructors*/
  41. List(){head=nullptr;tail=head;counter=0;}
  42. /*Copy constructor*/
  43. List(const List& rhs) { *this = rhs; }
  44. /*copy operator*/
  45. List& operator=(const List& rhs)
  46. {
  47. Clear();
  48. Node* temp = rhs.head;
  49. while (temp) {
  50. add_End(temp->get_Data());
  51. temp = temp->get_next();
  52. }
  53. return *this;
  54. }
  55. /*Checks if the list is empty*/
  56. bool isEmpty()
  57. {
  58. if (head==nullptr)
  59. return true;
  60. return false;
  61. }
  62. /*front manipulation*/
  63. void add_Front(customer item)
  64. {
  65. if (isEmpty())
  66. {
  67. head = new Node(item);
  68. tail = head;
  69. counter++;
  70. }
  71. else{
  72. Node * nw= new Node(item);
  73. nw ->set_next(head);
  74. head=nw;
  75. counter++;
  76. }
  77. }
  78. void pop_Front()
  79. {
  80. if (isEmpty())
  81. return;
  82. if (head==tail)
  83. {
  84. delete head;
  85. head = tail = nullptr;
  86. counter--;
  87. return;
  88. }
  89. Node * temphead=head;
  90. head=head->get_next();
  91. delete temphead;
  92. counter--;
  93. }
  94. /*End Manipulation*/
  95. void add_End(customer X)
  96. {
  97. if(isEmpty()){
  98. add_Front(X);
  99. counter++;}
  100. else
  101. {
  102. Node * temp=new Node(X);
  103. tail->set_next(temp);
  104. tail=temp;
  105. counter++;
  106. }
  107. }
  108.  
  109. /*freeing the whole list*/
  110. void Clear()
  111. {
  112. while (!isEmpty())
  113. pop_Front();
  114. }
  115.  
  116. /*Destructor*/
  117. ~List(){Clear();}
  118.  
  119. /*Extras*/
  120. int get_Size(){return counter;}
  121. customer get_Front(){return head->get_Data();}
  122. customer get_End(){return tail->get_Data();}
  123.  
  124. };
  125.  
  126. using namespace std;
  127.  
  128. int main()
  129. {
  130. List Data;
  131. int numberofelements;
  132. cout<<"How many customers you wanna randomly generate? : ";
  133. cin >> numberofelements;
  134. srand(time(NULL));
  135. for (int i=0; i<numberofelements; i++)
  136. {
  137. customer temp;
  138. temp.random_customer();
  139. Data.add_Front(temp);
  140.  
  141. }
  142. {
  143. List Data2;
  144. Data2 = Data;
  145. }
  146. List Data3 { Data };
  147.  
  148. std::cout << "here goes the destructor\n";
  149. return 0;
  150. }
  151.  
  152.  
Success #stdin #stdout 0s 3464KB
stdin
3
stdout
How many customers you wanna randomly generate? : here goes the destructor