Question
In C++. This was my receiptbag class before, but it needs to use a linked list instead of vectors now. #pragma once #include #include template
In C++. This was my receiptbag class before, but it needs to use a linked list instead of vectors now.
#pragma once
#include
#include
template
class ReceiptBag {
public:
int insert(Thing aThing) {
bagReceipts.push_back(recieptNum);
bagContents.push_back(aThing);
bagSize++;
recieptNum++;
return recieptNum;
}
//This will be the most important part of your code. You must find the item
//probably with parallel arrays
Thing pop(int userReceipt) {
Thing aThing;
userReceipt -=1;
if ( bagReceipts.size() > 0) {
for (int i = 0; i
if (bagReceipts[i] == userReceipt)
{
aThing = bagContents[i];
//swap functions for both vectors
std::swap(bagContents[i], bagContents[bagContents.size() - 1]);
std::swap(bagReceipts[i], bagReceipts[bagReceipts.size() - 1]);
bagContents.pop_back();
bagReceipts.pop_back();
recieptNum--;
bagSize--;
break;
}
}
}
else {
std::cout
}
return aThing;
}
int count(Thing aThing) {
int bagCount = 0;
for (int i = 0; i
if (bagContents[i] == aThing) {
bagCount++;
}
}
return bagCount;
}
int size() {
return bagSize;
}
private:
std::vector
std::vector
int bagSize = 0;
int recieptNum = 0;
};
A different approach is to use a linked list. Define a structure that will contain a bag item that has two fields: the thing being stored and an integer receipt. Use a list of these structures to keep track of the things in the bag. Thus, taking an item out of the bag would just require you to search the list for the structure instance containing the receipt, save that structure to a temporary place, erase the structure from the list, and then returning the copy in the temporary. Re-factor your RecieptBag class to use a linked list to store things rather than storing them in a set of parallel arraysStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started