fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. class IntList
  8. {
  9. struct ListItem
  10. {
  11. int item;
  12. ListItem *next;
  13.  
  14. ListItem(int i, ListItem *n = NULL)
  15. {
  16. item = i;
  17. next = n;
  18. }
  19. };
  20. private:
  21.  
  22. int itemsCount;
  23. ListItem *first;
  24. ListItem *last;
  25.  
  26. public:
  27. IntList()
  28. {
  29. itemsCount = 0;
  30. first = last = NULL;
  31. }
  32.  
  33. IntList(const IntList & src);
  34.  
  35. ~IntList();
  36.  
  37. int Head() const
  38. {
  39. return first->item;
  40. }
  41.  
  42. int & Head()
  43. {
  44. return first->item;
  45. }
  46.  
  47. int Tail() const
  48. {
  49. return last->item;
  50. }
  51.  
  52. int & Tail()
  53. {
  54. return last->item;
  55. }
  56.  
  57. void AddLast(const IntList & src);
  58. void AddFirst(int item);
  59. void AddLast(int item);
  60. int RemoveFirst();
  61. bool Remove(int n);
  62. void Insert(int n);
  63.  
  64. int getItemsCount()
  65. {
  66. return itemsCount;
  67. }
  68.  
  69. string GetAllItemsInfo();
  70. };
  71.  
  72.  
  73. using namespace std;
  74.  
  75. IntList::IntList(const IntList & src)
  76. {
  77. itemsCount = 0;
  78. first = last = NULL;
  79. AddLast(src);
  80. }
  81. IntList::~IntList()
  82. {
  83. ListItem *current = NULL;
  84. ListItem *next = first;
  85. while (next)
  86. {
  87. current = next;
  88. next = next->next;
  89. delete current;
  90. }
  91. }
  92.  
  93. void IntList::AddLast(const IntList & src)
  94. {
  95. for (ListItem *cur = src.first; cur; cur = cur->next)
  96. AddLast(cur->item);
  97. }
  98.  
  99. void IntList::AddFirst(int item)
  100. {
  101. ListItem *newItem = new ListItem(item, first);
  102. if (!first)
  103. {
  104. last = newItem;
  105. }
  106. first = newItem;
  107. itemsCount++;
  108. }
  109.  
  110. void IntList::AddLast(int item)
  111. {
  112. ListItem *newItem = new ListItem(item);
  113. if (!last)
  114. {
  115. first = newItem;
  116. }
  117. else
  118. {
  119. last->next = newItem;
  120. }
  121. last = newItem;
  122. itemsCount++;
  123. }
  124.  
  125. int IntList::RemoveFirst()
  126. {
  127. int res = first->item;
  128. first = first->next;
  129. itemsCount--;
  130. return res;
  131. }
  132.  
  133. bool IntList::Remove(int value)
  134. {
  135. ListItem *prev = 0,
  136. *current = first;
  137. while(current)
  138. {
  139. if (current->item == value)
  140. {
  141. if (prev)
  142. {
  143. prev->next = current->next;
  144. }
  145. if (current == last)
  146. {
  147. last = prev;
  148. }
  149. delete current;
  150. itemsCount--;
  151. return true;
  152. }
  153. else
  154. {
  155. prev = current;
  156. current = current->next;
  157. }
  158. }
  159. return false;
  160. }
  161.  
  162. void IntList::Insert(int value)
  163. {
  164. ListItem *prev = NULL,
  165. *succ = first;
  166. while ( succ !=NULL && succ->item < value)
  167. {
  168. prev = succ;
  169. succ = succ->next;
  170. }
  171. ListItem *newItem = new ListItem(value, succ);
  172. if ( succ == NULL)
  173. {
  174. last = newItem;
  175. }
  176.  
  177. if (prev == NULL)
  178. {
  179. first = newItem;
  180. }
  181.  
  182. else
  183. {
  184. prev->next = newItem;
  185. }
  186. itemsCount++;
  187. }
  188.  
  189. string IntList::GetAllItemsInfo()
  190. {
  191. std::stringstream stream;
  192. ListItem *current = first;
  193. while (current)
  194. {
  195. stream << current->item << ' ';
  196. current = current->next;
  197. }
  198. stream << endl;
  199. return stream.str();
  200. }
  201.  
  202.  
  203.  
  204. int main()
  205. {
  206. IntList list;
  207. list.AddLast(2);
  208. list.AddLast(3);
  209. list.AddFirst(1);
  210.  
  211. cout << list.GetAllItemsInfo();
  212.  
  213. IntList list1(list);
  214. list1.AddLast(4);
  215.  
  216. cout << list1.GetAllItemsInfo();
  217.  
  218. list1.Remove(2);
  219. list1.Remove(5);
  220. list1.Remove(4);
  221.  
  222. cout << list1.GetAllItemsInfo();
  223.  
  224. return 0;
  225. }
  226.  
Success #stdin #stdout 0.01s 2816KB
stdin
Standard input is empty
stdout
1 2 3 
1 2 3 4 
1 3