Question

In: Computer Science

Implement in C++ a GroceryListADT that does the following: gets the input about a grocery item...

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)

Solutions

Expert Solution

#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


Related Solutions

(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
Implement in Python a script that does the following: 1) reads input from a supposed file...
Implement in Python a script that does the following: 1) reads input from a supposed file called firstnames_2.txt. 2) processes the input and writes and saves the output to a file. NOTE: Please make sure that the names are written in the outfile with one name on each line no comma ( , ) after the name in the output
Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
Implement a program in C++ that does the following: Ask the user and read at least...
Implement a program in C++ that does the following: Ask the user and read at least 10 numbers as input from the keyboard and stores them in a an array Displays the array of numbers on the screen (as is, before sorting) Sorts those numbers in the array using Selection Sort Algorithm Displays the array of numbers on the screen (AFTER sorting)
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets...
Implement the following methods in Java: a. A method named MergeFileswith the following signature that gets two file names and write the content of the first file (sourceFile) into the beginning of the second file (destinationFile. For example, if sourceFile contains “Hello ” and destinationFile contains “World!”, this method must keep sourceFile as it is, but replace the content of the second file by “Hello World!”.public static voidMergeFiles(String sourceFile, String destinationFile) b. A recursive method with the following signature that...
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using SELECTION SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_SelectionSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT