filtering linked list by letting user input number

Viewed 51

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;
    }
}
1 Answers

since you are printing the product list after creating the unordered_set order will be the same as long as you do not invalidate it (ie. not inserting or removing the element from the set).

you can use n which the user has entered to iterate through set.

note: I will prefer vector over unordered_set for this scenario. since your link list is in sorted order and vector will maintain its order and it is efficient to access it using an index which user will provide

example:

std::vector<std::string> categoryFilter() {
  // Function to remove duplicates from a sorted list
  ProductInventory* previous = nullptr;
  ProductInventory* current;
  // take an empty vec to store linked list nodes for future reference
  std::vector<std::string> vec{};

  // 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 (std::find(vec.begin(), vec.end(), current->product_category) !=
          vec.end()) {
        // previous = current;
        previous->next = current->next;
      } else {
        // insert the current node into the set and proceed to the next node

        vec.push_back(current->product_category);
      }
      current = previous->next;
    }
  }

  // note that your index is 1-based
  int n = 1;
  for (const auto& v : vec) {
    std::cout << n << ". " << v << std::endl;
    n++;
  }
  return vec;
}

usage:


auto category_vec = categoryFilter();
int n{};
std::cin >> n;
// change n to 0 based index
n=n-1;

// check for range 
//...
auto category = category_list[n];
Related