fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Object
  6. {
  7. private:
  8. int data;
  9. public:
  10. Object *next;
  11. Object(int);
  12. ~Object();
  13. void print();
  14.  
  15. friend class myList;
  16. };
  17.  
  18. Object :: Object(int value)
  19. {
  20. data = value;
  21. next = NULL;
  22. cout << "Created object of " << data << endl;
  23. }
  24.  
  25. Object :: ~Object()
  26. {
  27. cout << "Deleted object of " << data << endl;
  28. }
  29.  
  30. void Object :: print()
  31. {
  32. cout << data << endl;
  33. }
  34.  
  35. class myList
  36. {
  37. private:
  38. Object *first;
  39. public:
  40. myList();
  41. ~myList();
  42. int size();
  43. void push_back(int);
  44. void erase(int);
  45. void print();
  46. int find(int);
  47. };
  48.  
  49. myList :: myList()
  50. {
  51. first = NULL;
  52. }
  53.  
  54. myList :: ~myList()
  55. {
  56. Object *temp = first;
  57.  
  58. while (temp)
  59. {
  60. delete temp;
  61. temp = temp->next;
  62. }
  63.  
  64. delete temp;
  65. }
  66.  
  67. int myList :: size()
  68. {
  69. int size = 0;
  70. Object *temp = first;
  71.  
  72. while (temp)
  73. {
  74. size++;
  75. temp = temp->next;
  76. }
  77.  
  78. return size;
  79. }
  80.  
  81. void myList :: push_back(int val)
  82. {
  83. Object *obj = new Object(val);
  84. int pos = 0;
  85.  
  86. if (first == NULL)
  87. {
  88. first = obj;
  89. }
  90. else
  91. {
  92. pos++;
  93. Object *temp = first;
  94.  
  95. while (temp->next)
  96. {
  97. temp = temp->next;
  98. pos++;
  99. }
  100.  
  101. temp->next = obj;
  102. temp->next->next = NULL;
  103. }
  104.  
  105. cout << "Pushed " << val << " at pos " << pos << endl;
  106. }
  107.  
  108. void myList :: erase(int index)
  109. {
  110. if (index > this->size() or index < 1)
  111. {
  112. cout << "Index out of range!" << endl;
  113. return;
  114. }
  115. else
  116. {
  117. int i = 1;
  118. Object *temp = first;
  119.  
  120. if (index == i)
  121. {
  122. delete first;
  123. first = temp->next;
  124. }
  125. else if (index >= 2)
  126. {
  127. while (temp)
  128. {
  129. if ((i + 1) == index)
  130. {
  131. Object *temp2 = temp->next->next;
  132. delete temp->next;
  133. temp->next = temp2;
  134. break;
  135. }
  136.  
  137. temp = temp->next;
  138. i++;
  139. }
  140. }
  141. }
  142. }
  143.  
  144. void myList :: print()
  145. {
  146. Object *temp = first;
  147. int pos = 0;
  148.  
  149. while (temp)
  150. {
  151. cout << pos << ": ";
  152. temp->print();
  153. temp = temp->next;
  154. pos++;
  155. }
  156. }
  157.  
  158. int myList :: find(int val)
  159. {
  160. int pos = 1;
  161. Object *temp = first;
  162.  
  163. while (temp)
  164. {
  165. if (temp->data == val)
  166. {
  167. cout << "Found " << val << " at pos " << pos << endl;
  168. return pos;
  169. }
  170. else
  171. {
  172. pos++;
  173. temp = temp->next;
  174. }
  175. }
  176.  
  177. cout << "Not found " << val << " in list" << endl;
  178. return -1;
  179. }
  180.  
  181. int main()
  182. {
  183. myList *list = new myList();
  184.  
  185. list->push_back(3);
  186. list->push_back(6);
  187. list->push_back(2);
  188. list->push_back(4);
  189. list->push_back(-6);
  190.  
  191. cout << "size: " << list->size() << endl;
  192. list->print();
  193.  
  194. list->erase(3);
  195. cout << "size: " << list->size() << endl;
  196. list->print();
  197.  
  198. list->erase(list->find(6));
  199. cout << "size: " << list->size() << endl;
  200. list->print();
  201.  
  202. delete list;
  203.  
  204. return 0;
  205. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Created object of 3
Pushed 3 at pos 0
Created object of 6
Pushed 6 at pos 1
Created object of 2
Pushed 2 at pos 2
Created object of 4
Pushed 4 at pos 3
Created object of -6
Pushed -6 at pos 4
size: 5
0: 3
1: 6
2: 2
3: 4
4: -6
Deleted object of 2
size: 4
0: 3
1: 6
2: 4
3: -6
Found 6 at pos 2
Deleted object of 6
size: 3
0: 3
1: 4
2: -6
Deleted object of 3
Deleted object of 4
Deleted object of -6