Make a priority queue. A priority queue is a data structure that adds items to a
ID: 3591319 • Letter: M
Question
Make a priority queue. A priority queue is a data structure that adds items to a list but removes them in an ordered manner determined by the programmer. One way to apply a priority queue is a Grocery List. When you think about it, we often go shopping with a budget. Sometimes, we cannot get everything on our shopping list due to our budget. So, we add items that we need more than others or we add the least expensive items to maximize how much of our list we purchase. Therefore our grocery list is in effect a priority queue: The list has items pushed onto list. Then, as items are popped from the list, they are pushed into the shopping cart. Your budget and need determines what things are left on the list.
EXAMPLE OUTPUT
Grocery List Rules 1. 2. 3. The shopping list should also contain a running total of everything in the list. Each List should contain a title We will add 10 items to our Shopping list. Each Item will have a name and a price associated with it. Gallon of Milk $6.99 Dozen Eggs $5.69 Peanut Butter $4.99 Chicken Strips $6.99 Ice Cream - $4.00 Cereal $3.98 3lbs of Apples $4.11 Gift Card $10 10 pack of socks $19.78 48 count AA Batteries $14.49 4. In order to make the list to operate as a priority queue, anything added to the List needs to be sorted, from the lowest price at the top of the list to the highest price at the bottom of the list. Then when removing items from the list, remove the item with the lowest price. Grocery.h 1. In the private section of your Grocery Class, have the list name A structure for the item information, including the name of the item and the price of the item, and a linker to the next item Access to the top of the list The size of the list The running total of the list. a. b. C. d. e. 2. In the public section of your Grocery Class, have Constructors and Deconstructors An accessor methods for: (1) the size of the list, (2) the name of the list, (3) the name of the item at the top of the list, and (4) the price of the item at the top of the list Mutator methods for pushing and popping an item Overloaded operator for () that takes a string and double and pushes the items to the list a. b. C. d.Explanation / Answer
Grocery.hpp
#include <iostream>
#include <map>
#include <string>
class Grocery
{
private :
std :: string ListName;/* strores the list name */
std :: map < double, std :: string > groceries; /* stores the map of items and prices*/
std :: priority_queue < double > : : iterator top;/* iterator to top of priority queue*/
std :: priority_queue<double> priority;/*que to hold priority of prices */
int Size;/* size of list*/
double Total;/* total price */
public:
Grocery();
Grocery(std :: string name);
~Grocery();
std :: string getName();/* return name of the list */
int getSize();/* return size */
std :: string getTopGroceryName();/* return top grocery name */
double getTopGroceryPrice();/* return top grocery price */
void pushItem( std :: string item, double price);/* push item to grocery list*/
std :: string popItem();/* pop item */
void operator()( std :: string name, double price);/* overloading () */
void operator<<();/* overloading << */
};
Grocery.cpp
#include "Grocery.hpp"
Grocery :: Grocery()
{
Listname = "";
Size = 0;
Total = 0;
}
Grocery :: Grocery( std :: string name)
{
ListName.clear();
ListName.append(name);
Size = 0;
Total = 0;
}
Grocery :: ~Grocery()
{
}
std :: string Grocery :: getName()
{
return ListName;
}
int Grocery :: getSize()
{
return Size;
}
std :: string Grocery :: getTopGroceryName()
{
return top->first;
}
double Grocery :: getTopGroceryPrice()
{
return top->second;
}
void Grocery :: pushItem( std :: string item, double price)
{
groceries.insert(pair< double, std :: string> (price,item));
priority.push(price);
top = priority.end();
Total = Total+ price;
Size =Size+1;
}
void Grocery:: popItem()
{
double price = priority.pop();
Total = Total -price;
Size = Size -1 ;
groceries.erase(price);
top = top -1;
}
void Grocery :: operator()(std :: string item, double price)
{
groceries.insert(pair<double, std ::string>(price,item));
priority.push(price);
top = priority.end();
Total = Total+ price;
Size = Size + 1;
}
void Grocery :: operator<<()
{
std :: cout << "========="<<ListName<<"===========";
std :: priority_queue <double> :: iterator it;
for (it = priority.begin();it!=priority.end();++it)
{
std :: cout <<(groceries.find(*it))->second << " : " << (groceries.find(*it))-> first<<" ";
}
std :: cout << Total;
}
Main.cpp
#include "Grocery.hpp"
#inlcude <iostream>
int main()
{
int budget = 0;
Grocery g1 = new Grocery("Shopping Cart");
Grocery g2 = new Grocery("Shopping List");
g2("Milk Gallon",6.99);
g2("DozenEggs",5.69);
g2("Peanut Butter",4.99);
g2("Chicken Strips",6.99);
g2("Ice cream",4.00);
g2("Cereal",3.98);
g2("3lbs of Apples",4.11);
g2("GiftCard",10);
g2("10 pack of socks",19.78);
g2("48 count of AA Batteries",14.49);
while (budget < 50)
{
g1(g2.getTopGroceryName(), g2.getTopGroceryPrice());
budget = budget + g2.getTopGroceryPrice();
g2.popItem();
}
cout <<g1;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.