I wanted to filter my linked list based on the category. I managed to do it but I wanted to improve it to let user filter using a number instead of typing the category. Basically, I will output the available category by traversing through the linked list once and the category will be output along a number. User input a number to select the category instead of entering the category itself.
void categoryFilter() {
int n = 1;
// Function to remove duplicates from a sorted list
ProductInventory* previous = nullptr;
ProductInventory* current;
// take an empty set to store linked list nodes for future reference
unordered_set<string> set;
// do till the linked list is empty
if (head != NULL) {
current = head;
while (current != nullptr)
{
previous = current;
// if the current node is seen before, ignore it
if (set.find(current->product_category) != set.end()) {
//previous = current;
previous->next = current->next;
}
else {
// insert the current node into the set and proceed to the next node
//
set.insert(current->product_category);
//previous = current;
//cout <<n << ". "<< current->product_category<<endl;
//n = n + 1;
}
current = previous->next;
}
}
for (auto it = set.begin(); it !=
set.end(); ++it) {
cout << n << ". " << *it << endl;
n = n + 1;
}
}
void categoryFilter2() {
categoryFilter();
string category;
cin >> category;
if (head != NULL) {
ProductInventory* current = head;
//do while
while (current != NULL) {
if (category == current->product_category) {
cout << "1. Product Id: " << current->product_id;
cout << "2. Product Name: " << current->product_name;
cout << "3. Product Price: " << current->product_price;
cout << "4. Product Quantity: " << current->product_quantity;
cout << "5. Product Category: " << current->product_category;
}
current = current->next;
}
}
else {
cout << "No record in the list" << endl;
}
}