In: Computer Science
Implement in C++ a GroceryListADT that does the following:
gets the input about a grocery item (itemName, price, quantity, etc) from the user (from the keyboard)
#include <bits/stdc++.h>
using namespace std;
// A linked list node
class Node
{
public:
string name;
int price;
int quantity;
Node *next;
};
void append(Node** head_ref)
{
Node* new_node = new Node();
Node *last = *head_ref;
string n;
int p,q;
cout << "\nEnter name: ";
cin >> n;
cout << "Enter price: ";
cin >> p;
cout << "Enter quantity: ";
cin >> q;
new_node->name = n;
new_node->price = p;
new_node->quantity = q;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}
void printList(Node *node)
{
while (node != NULL)
{
cout<<" \n"<<node->name <<"\t" <<node->price <<"\t" <<node->quantity;
node = node->next;
}
}
/* Driver code*/
int main()
{
Node* head = NULL;
int n;
cout << "Enter Number of items: ";
cin >> n;
for (int i = 0; i < n; i++) {
append(&head);
}
cout<<"\nCreated Data is: ";
printList(head);
return 0;
}
Note: Linked list is used to implement the program