// =======================
// Attached: HW #4f
// =======================
// HW #4f - Linked List of Box Records
// Elaine Torrez
// CS 1B
// =======================
#include <iostream>
#include <iomanip>
#include <limits>
#include <cctype>
using namespace std;
struct Box
{
int id;
double width;
double height;
double length;
Box* next;
};
typedef Box* ptrType;
// Function prototypes
bool insertRecord(ptrType& head);
void displayRecords(const ptrType head);
bool deleteRecord(ptrType& head, int idToDelete);
void clearList(ptrType& head);
int main()
{
char again = 'Y';
do
{
// Use system("cls") on Windows; use the ANSI clear sequence for REPL/others
// system("cls");
cout << "\033[2J\033[0;0H";
ptrType head = nullptr;
cout << "Enter the specifications of different types of boxes.\n";
cout << "Include the number of boxes presently in inventory.\n";
cout << "Enter boxes - (-1 to quit):\n\n";
bool keepGoing = true;
while (keepGoing)
{
keepGoing = insertRecord(head);
}
char show = 'N';
cout << "\nWould you like to see the list of boxes (Y/N)? ";
cin >> show;
if (toupper(show) == 'Y')
{
displayRecords(head);
int delID;
cout << "\nEnter the ID of the box to be deleted: ";
cin >> delID;
deleteRecord(head, delID);
// Screen clears before displaying the list again
// system("cls");
cout << "\033[2J\033[0;0H";
cout << "--- (Screen Clears and the list is displayed) ---\n\n";
displayRecords(head);
}
cout << "\nDo again Y/N? ";
cin >> again;
// Important: free memory so no leaks
clearList(head);
} while (toupper(again) == 'Y');
return 0;
}
// =======================
// insertRecord
// - Adds a new Box node to the FRONT of the linked list.
// - Returns false if user enters -1 for ID (quit).
// =======================
bool insertRecord(ptrType& head)
{
ptrType temp = new Box;
cout << setw(10) << "ID Number: ";
cin >> temp->id;
if (temp->id == -1)
{
delete temp;
return false;
}
cout << setw(10) << "width: ";
cin >> temp->width;
cout << setw(10) << "height: ";
cin >> temp->height;
cout << setw(10) << "length: ";
cin >> temp->length;
cout << "\n";
temp->next = head;
head = temp;
return true;
}
// =======================
// displayRecords
// - Clears screen then prints all nodes in a table.
// =======================
void displayRecords(const ptrType head)
{
// Screen should clear before displaying the list
// system("cls");
cout << "\033[2J\033[0;0H";
if (head == nullptr)
{
cout << "List is empty.\n";
return;
}
cout << "Types of boxes:\n\n";
cout << left << setw(6) << "ID#"
<< setw(10) << "Width"
<< setw(10) << "Height"
<< setw(10) << "Length" << "\n";
cout << "----------------------------------------\n";
ptrType temp = head;
cout << fixed << setprecision(2);
while (temp != nullptr)
{
cout << left << setw(6) << temp->id
<< setw(10) << temp->width
<< setw(10) << temp->height
<< setw(10) << temp->length << "\n";
temp = temp->next;
}
}
// =======================
// deleteRecord
// - Deletes first node with matching id.
// - Returns true if deleted, false if not found.
// =======================
bool deleteRecord(ptrType& head, int idToDelete)
{
if (head == nullptr) return false;
// If the first node is the one to delete
if (head->id == idToDelete)
{
ptrType kill = head;
head = head->next;
delete kill;
return true;
}
ptrType prev = head;
ptrType curr = head->next;
while (curr != nullptr)
{
if (curr->id == idToDelete)
{
prev->next = curr->next;
delete curr;
return true;
}
prev = curr;
curr = curr->next;
}
return false;
}
// =======================
// clearList
// - Frees all nodes in the linked list.
// =======================
void clearList(ptrType& head)
{
while (head != nullptr)
{
ptrType kill = head;
head = head->next;
delete kill;
}
}
/* =======================
Output:
Enter the specifications of different types of boxes.
Include the number of boxes presently in inventory.
Enter boxes - (-1 to quit):
ID Number: 100
width: 3.25
height: 2.50
length: 3.50
ID Number: 101
width: 4.00
height: 8.50
length: 6.50
ID Number: 102
width: 3.50
height: 5.00
length: 7.75
ID Number: 103
width: 2.75
height: 3.75
length: 9.00
ID Number: -1
Would you like to see the list of boxes (Y/N)? y
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: 101
--- (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? n
======================= */