fork(1) 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. /*Checks if the list is empty*/
  43. bool isEmpty()
  44. {
  45. if (head==nullptr)
  46. return true;
  47. return false;
  48. }
  49. /*front manipulation*/
  50. void add_Front(customer item)
  51. {
  52. if (isEmpty())
  53. {
  54. head = new Node(item);
  55. tail = head;
  56. counter++;
  57. }
  58. else{
  59. Node * nw= new Node(item);
  60. nw ->set_next(head);
  61. head=nw;
  62. counter++;
  63. }
  64. }
  65. void pop_Front()
  66. {
  67. if (isEmpty())
  68. return;
  69. if (head==tail)
  70. {
  71. delete head;
  72. delete tail;
  73. counter--;
  74. return;
  75. }
  76. Node * temphead=head;
  77. head=head->get_next();
  78. delete temphead;
  79. counter--;
  80. }
  81. /*End Manipulation*/
  82. void add_End(customer X)
  83. {
  84. if(isEmpty()){
  85. add_Front(X);
  86. counter++;}
  87. else
  88. {
  89. Node * temp=new Node(X);
  90. tail->set_next(temp);
  91. tail=temp;
  92. counter++;
  93. }
  94. }
  95.  
  96. /*freeing the whole list*/
  97. void Clear()
  98. {
  99. while (!isEmpty())
  100. pop_Front();
  101. }
  102.  
  103. /*Destructor*/
  104. ~List(){Clear();}
  105.  
  106. /*Extras*/
  107. int get_Size(){return counter;}
  108. customer get_Front(){return head->get_Data();}
  109. customer get_End(){return tail->get_Data();}
  110.  
  111. };
  112.  
  113. using namespace std;
  114.  
  115. int main()
  116. {
  117. List Data;
  118. int numberofelements;
  119. cout<<"How many customers you wanna randomly generate? : ";
  120. cin >> numberofelements;
  121. srand(time(NULL));
  122. for (int i=0; i<numberofelements; i++)
  123. {
  124. customer temp;
  125. temp.random_customer();
  126. Data.add_Front(temp);
  127.  
  128. }
  129. std::cout << "here goes the destructor\n";
  130. return 0;
  131. }
  132.  
  133.  
Runtime error #stdin #stdout #stderr 0s 3420KB
stdin
3
stdout
How many customers you wanna randomly generate? : here goes the destructor
stderr
*** Error in `./prog': double free or corruption (fasttop): 0x08386a10 ***