fork download
  1. // =======================
  2. // Attached: HW #4f
  3. // =======================
  4. // HW #4f - Linked List of Box Records
  5. // Elaine Torrez
  6. // CS 1B
  7. // =======================
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <limits>
  12. #include <cctype>
  13. using namespace std;
  14.  
  15. struct Box
  16. {
  17. int id;
  18. double width;
  19. double height;
  20. double length;
  21. Box* next;
  22. };
  23.  
  24. typedef Box* ptrType;
  25.  
  26. // Function prototypes
  27. bool insertRecord(ptrType& head);
  28. void displayRecords(const ptrType head);
  29. bool deleteRecord(ptrType& head, int idToDelete);
  30. void clearList(ptrType& head);
  31.  
  32. int main()
  33. {
  34. char again = 'Y';
  35.  
  36. do
  37. {
  38. // Use system("cls") on Windows; use the ANSI clear sequence for REPL/others
  39. // system("cls");
  40. cout << "\033[2J\033[0;0H";
  41.  
  42. ptrType head = nullptr;
  43.  
  44. cout << "Enter the specifications of different types of boxes.\n";
  45. cout << "Include the number of boxes presently in inventory.\n";
  46. cout << "Enter boxes - (-1 to quit):\n\n";
  47.  
  48. bool keepGoing = true;
  49. while (keepGoing)
  50. {
  51. keepGoing = insertRecord(head);
  52. }
  53.  
  54. char show = 'N';
  55. cout << "\nWould you like to see the list of boxes (Y/N)? ";
  56. cin >> show;
  57.  
  58. if (toupper(show) == 'Y')
  59. {
  60. displayRecords(head);
  61.  
  62. int delID;
  63. cout << "\nEnter the ID of the box to be deleted: ";
  64. cin >> delID;
  65.  
  66. deleteRecord(head, delID);
  67.  
  68. // Screen clears before displaying the list again
  69. // system("cls");
  70. cout << "\033[2J\033[0;0H";
  71. cout << "--- (Screen Clears and the list is displayed) ---\n\n";
  72. displayRecords(head);
  73. }
  74.  
  75. cout << "\nDo again Y/N? ";
  76. cin >> again;
  77.  
  78. // Important: free memory so no leaks
  79. clearList(head);
  80.  
  81. } while (toupper(again) == 'Y');
  82.  
  83. return 0;
  84. }
  85.  
  86. // =======================
  87. // insertRecord
  88. // - Adds a new Box node to the FRONT of the linked list.
  89. // - Returns false if user enters -1 for ID (quit).
  90. // =======================
  91. bool insertRecord(ptrType& head)
  92. {
  93. ptrType temp = new Box;
  94.  
  95. cout << setw(10) << "ID Number: ";
  96. cin >> temp->id;
  97.  
  98. if (temp->id == -1)
  99. {
  100. delete temp;
  101. return false;
  102. }
  103.  
  104. cout << setw(10) << "width: ";
  105. cin >> temp->width;
  106.  
  107. cout << setw(10) << "height: ";
  108. cin >> temp->height;
  109.  
  110. cout << setw(10) << "length: ";
  111. cin >> temp->length;
  112.  
  113. cout << "\n";
  114.  
  115. temp->next = head;
  116. head = temp;
  117.  
  118. return true;
  119. }
  120.  
  121. // =======================
  122. // displayRecords
  123. // - Clears screen then prints all nodes in a table.
  124. // =======================
  125. void displayRecords(const ptrType head)
  126. {
  127. // Screen should clear before displaying the list
  128. // system("cls");
  129. cout << "\033[2J\033[0;0H";
  130.  
  131. if (head == nullptr)
  132. {
  133. cout << "List is empty.\n";
  134. return;
  135. }
  136.  
  137. cout << "Types of boxes:\n\n";
  138. cout << left << setw(6) << "ID#"
  139. << setw(10) << "Width"
  140. << setw(10) << "Height"
  141. << setw(10) << "Length" << "\n";
  142.  
  143. cout << "----------------------------------------\n";
  144.  
  145. ptrType temp = head;
  146.  
  147. cout << fixed << setprecision(2);
  148. while (temp != nullptr)
  149. {
  150. cout << left << setw(6) << temp->id
  151. << setw(10) << temp->width
  152. << setw(10) << temp->height
  153. << setw(10) << temp->length << "\n";
  154.  
  155. temp = temp->next;
  156. }
  157. }
  158.  
  159. // =======================
  160. // deleteRecord
  161. // - Deletes first node with matching id.
  162. // - Returns true if deleted, false if not found.
  163. // =======================
  164. bool deleteRecord(ptrType& head, int idToDelete)
  165. {
  166. if (head == nullptr) return false;
  167.  
  168. // If the first node is the one to delete
  169. if (head->id == idToDelete)
  170. {
  171. ptrType kill = head;
  172. head = head->next;
  173. delete kill;
  174. return true;
  175. }
  176.  
  177. ptrType prev = head;
  178. ptrType curr = head->next;
  179.  
  180. while (curr != nullptr)
  181. {
  182. if (curr->id == idToDelete)
  183. {
  184. prev->next = curr->next;
  185. delete curr;
  186. return true;
  187. }
  188. prev = curr;
  189. curr = curr->next;
  190. }
  191.  
  192. return false;
  193. }
  194.  
  195. // =======================
  196. // clearList
  197. // - Frees all nodes in the linked list.
  198. // =======================
  199. void clearList(ptrType& head)
  200. {
  201. while (head != nullptr)
  202. {
  203. ptrType kill = head;
  204. head = head->next;
  205. delete kill;
  206. }
  207. }
  208.  
  209. /* =======================
  210. Output:
  211.  
  212. Enter the specifications of different types of boxes.
  213. Include the number of boxes presently in inventory.
  214. Enter boxes - (-1 to quit):
  215.  
  216.  ID Number: 100
  217.   width: 3.25
  218.   height: 2.50
  219.   length: 3.50
  220.  
  221.  ID Number: 101
  222.   width: 4.00
  223.   height: 8.50
  224.   length: 6.50
  225.  
  226.  ID Number: 102
  227.   width: 3.50
  228.   height: 5.00
  229.   length: 7.75
  230.  
  231.  ID Number: 103
  232.   width: 2.75
  233.   height: 3.75
  234.   length: 9.00
  235.  
  236.  ID Number: -1
  237.  
  238. Would you like to see the list of boxes (Y/N)? y
  239.  
  240. Types of boxes:
  241.  
  242. ID# Width Height Length
  243. ----------------------------------------
  244. 103 2.75 3.75 9.00
  245. 102 3.50 5.00 7.75
  246. 101 4.00 8.50 6.50
  247. 100 3.25 2.50 3.50
  248.  
  249. Enter the ID of the box to be deleted: 101
  250.  
  251. --- (Screen Clears and the list is displayed) ---
  252.  
  253. Types of boxes:
  254.  
  255. ID# Width Height Length
  256. ----------------------------------------
  257. 103 2.75 3.75 9.00
  258. 102 3.50 5.00 7.75
  259. 100 3.25 2.50 3.50
  260.  
  261. Do again Y/N? n
  262.  
  263. ======================= */
  264.  
Success #stdin #stdout 0.01s 5320KB
stdin
100
3.25
2.50
3.50
101
4.00
8.50
6.50
102
3.50
5.00
7.75
103
2.75
3.75
9.00
-1
y
101
n
stdout
Enter the specifications of different types of boxes.
Include the number of boxes presently in inventory.
Enter boxes - (-1 to quit):

ID Number:    width:   height:   length: 
ID Number:    width:   height:   length: 
ID Number:    width:   height:   length: 
ID Number:    width:   height:   length: 
ID Number: 
Would you like to see the list of boxes (Y/N)? Types of boxes:

ID#   Width     Height    Length    
----------------------------------------
103   2.75      3.75      9.00      
102   3.50      5.00      7.75      
101   4.00      8.50      6.50      
100   3.25      2.50      3.50      

Enter the ID of the box to be deleted: --- (Screen Clears and the list is displayed) ---

Types of boxes:

ID#   Width     Height    Length    
----------------------------------------
103   2.75      3.75      9.00      
102   3.50      5.00      7.75      
100   3.25      2.50      3.50      

Do again Y/N?