fork(2) download
  1. #include <cstddef>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. // ------- HEADER ------
  6.  
  7. class LinkedGroceryList
  8. {
  9. private:
  10. struct Grocery
  11. {
  12. std::string name;
  13. std::size_t quantity;
  14. bool purchased;
  15. Grocery* next;
  16. };
  17.  
  18. Grocery* head;
  19. std::size_t differentGroceries;
  20. std::size_t totalGroceries;
  21.  
  22. public:
  23. LinkedGroceryList();
  24. ~LinkedGroceryList();
  25. void addGrocery(std::string const&, std::size_t);
  26. void removeGrocery(std::string const&, unsigned int);
  27. bool findGrocery(std::string const&) const;
  28. void markAsPurchased(std::string const&);
  29. void displayGroceries() const;
  30. void clearGroceries();
  31. std::size_t different() const { return differentGroceries; }
  32. std::size_t total() const { return totalGroceries; }
  33. bool empty() const { return differentGroceries == 0; }
  34. };
  35.  
  36. // ------ IMPLEMENTATION ------
  37.  
  38. LinkedGroceryList::LinkedGroceryList()
  39. : head(nullptr)
  40. , differentGroceries(0)
  41. , totalGroceries(0)
  42. {}
  43.  
  44. LinkedGroceryList::~LinkedGroceryList()
  45. {
  46. clearGroceries();
  47. }
  48.  
  49. void LinkedGroceryList::addGrocery(std::string const& name, std::size_t quantity)
  50. {
  51. Grocery* newGrocery = new Grocery;
  52.  
  53. newGrocery->name = name;
  54. newGrocery->quantity = quantity;
  55. newGrocery->purchased = false;
  56.  
  57. newGrocery->next = nullptr;
  58.  
  59. if (!head)
  60. {
  61. head = newGrocery;
  62. }
  63. else
  64. {
  65. Grocery* groceryPtr = head;
  66.  
  67. while (groceryPtr->next)
  68. groceryPtr = groceryPtr->next;
  69.  
  70. groceryPtr->next = newGrocery;
  71. }
  72.  
  73. differentGroceries++;
  74. totalGroceries += quantity;
  75. }
  76.  
  77. void LinkedGroceryList::removeGrocery(std::string const& name, unsigned int quantity)
  78. {
  79. if (!head) return;
  80.  
  81. Grocery* groceryPtr;
  82.  
  83. if (head->name == name)
  84. {
  85. groceryPtr = head;
  86. head = groceryPtr->next;
  87.  
  88. totalGroceries -= quantity;
  89. if (quantity == groceryPtr->quantity)
  90. differentGroceries--;
  91. else
  92. {
  93. groceryPtr->quantity -= quantity;
  94. return;
  95. }
  96.  
  97. delete groceryPtr;
  98. }
  99. else
  100. {
  101. Grocery* predPtr = nullptr;
  102. groceryPtr = head;
  103.  
  104. while (groceryPtr && groceryPtr->name != name)
  105. {
  106. predPtr = groceryPtr;
  107. groceryPtr = groceryPtr->next;
  108. }
  109.  
  110. if (groceryPtr)
  111. {
  112. totalGroceries -= quantity;
  113. if (quantity == groceryPtr->quantity)
  114. differentGroceries--;
  115. else
  116. {
  117. groceryPtr->quantity -= quantity;
  118. return;
  119. }
  120.  
  121. predPtr->next = groceryPtr->next;
  122. delete groceryPtr;
  123. }
  124. }
  125. }
  126.  
  127. bool LinkedGroceryList::findGrocery(std::string const& name) const
  128. {
  129. if (!head) return false;
  130.  
  131. if (head->name == name)
  132. return true;
  133. else
  134. {
  135. Grocery* groceryPtr = head->next;
  136.  
  137. while (groceryPtr)
  138. {
  139. if (groceryPtr->name == name)
  140. return true;
  141.  
  142. groceryPtr = groceryPtr->next;
  143. }
  144. }
  145.  
  146. return false;
  147. }
  148.  
  149. void LinkedGroceryList::markAsPurchased(std::string const& name)
  150. {
  151. Grocery* groceryPtr = head;
  152.  
  153. while (groceryPtr)
  154. {
  155. if (groceryPtr->name == name)
  156. groceryPtr->purchased = true;
  157.  
  158. groceryPtr = groceryPtr->next;
  159. }
  160. }
  161.  
  162. void LinkedGroceryList::displayGroceries() const
  163. {
  164. if (!head) return;
  165.  
  166. Grocery* groceryPtr = head;
  167.  
  168. while (groceryPtr)
  169. {
  170. std::cout << groceryPtr->name << ", ";
  171. std::cout << groceryPtr->quantity << ", ";
  172. std::cout << ((groceryPtr->purchased) ? "purchased\n" : "not purchased\n");
  173. groceryPtr = groceryPtr->next;
  174. }
  175. }
  176.  
  177. void LinkedGroceryList::clearGroceries()
  178. {
  179. if (!head) return;
  180.  
  181. Grocery* predPtr = head;
  182. Grocery* nextGrocery;
  183.  
  184. while (predPtr)
  185. {
  186. nextGrocery = predPtr->next;
  187. delete predPtr;
  188. predPtr = nextGrocery;
  189. }
  190.  
  191. head = nullptr;
  192. differentGroceries = 0;
  193. totalGroceries = 0;
  194. }
  195.  
  196. // ------ DRIVER ------
  197.  
  198. int main()
  199. {
  200. LinkedGroceryList meats;
  201. meats.addGrocery("steak", 5);
  202. meats.addGrocery("chicken", 3);
  203. meats.addGrocery("pork", 2);
  204.  
  205. meats.displayGroceries();
  206. std::cout << "\nDifferent items: " << meats.different() << "\n";
  207. std::cout << "Total items: " << meats.total();
  208. std::cout << "\nsteak present? " << std::boolalpha << meats.findGrocery("steak");
  209. std::cout << "\ncarrots present? " << std::boolalpha << meats.findGrocery("carrots");
  210.  
  211. std::cout << "\n\nRemoving 1 pork...\n\n";
  212. meats.removeGrocery("pork", 1);
  213.  
  214. meats.displayGroceries();
  215. std::cout << "\nDifferent items: " << meats.different() << "\n";
  216. std::cout << "Total items: " << meats.total();
  217. std::cout << "\npork present? " << std::boolalpha << meats.findGrocery("pork");
  218.  
  219. meats.markAsPurchased("steak");
  220. std::cout << "\n\nsteak has been purchased\n\n";
  221. meats.displayGroceries();
  222. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
steak, 5, not purchased
chicken, 3, not purchased
pork, 2, not purchased

Different items: 3
Total items: 10
steak present? true
carrots present? false

Removing 1 pork...

steak, 5, not purchased
chicken, 3, not purchased
pork, 1, not purchased

Different items: 3
Total items: 9
pork present? true

steak has been purchased

steak, 5, purchased
chicken, 3, not purchased
pork, 1, not purchased