c++ 数据结构,帮下忙re-implement BagOfIntegers to use an array of item counts as the underlying data structure.For example,if the bag is {1,3,3,3,3,5,7,7,8} then the first nine elements of your count array will be 0,1,0,4,0,1,0,2,1.#ifndef _Bag
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/27 05:41:04
c++ 数据结构,帮下忙re-implement BagOfIntegers to use an array of item counts as the underlying data structure.For example,if the bag is {1,3,3,3,3,5,7,7,8} then the first nine elements of your count array will be 0,1,0,4,0,1,0,2,1.#ifndef _Bag
c++ 数据结构,帮下忙
re-implement BagOfIntegers to use an array of item counts as the underlying data structure.
For example,if the bag is {1,3,3,3,3,5,7,7,8} then the first nine elements of your count array will be 0,1,0,4,0,1,0,2,1.
#ifndef _BagOfIntegers
#define _BagOfIntegers
#include "BagInterface.h"
class BagOfIntegers :public BagInterface
{
\x05private:
\x05static const int DEFAULT_BAG_SIZE = 8;
\x05int items[DEFAULT_BAG_SIZE];
\x05int itemCount;
\x05int maxItems;
int getIndexOf(const int& target) const;
public:
\x05BagOfIntegers();
\x05int getCurrentSize() const;
\x05bool isEmpty() const;
\x05bool add(const int& newEntry);
\x05bool remove(const int& anEntry);
\x05void clear();
\x05bool contains(const int& anEntry) const;
\x05int getFrequencyOf(const int& anEntry) const;
\x05vector toVector() const; };
BagOfIntegers::BagOfIntegers() :itemCount(0),maxItems(DEFAULT_BAG_SIZE)
{}
int BagOfIntegers::getCurrentSize() const{
\x05return itemCount;}
bool BagOfIntegers::isEmpty() const{
\x05return itemCount == 0;}
bool BagOfIntegers::add(const int& newEntry){
\x05bool hasRoomToAdd = (itemCount < maxItems);
\x05if (hasRoomToAdd){
\x05\x05items[itemCount] = newEntry;
\x05\x05itemCount++;}
\x05return hasRoomToAdd;}
bool BagOfIntegers::remove(const int& anEntry){
\x05int locatedIndex = getIndexOf(anEntry);
\x05bool canRemoveItem = isEmpty() && (locatedIndex > -1);
\x05if (canRemoveItem){
\x05\x05itemCount--;
\x05\x05items[locatedIndex] = items[itemCount];}
\x05return canRemoveItem;}
void BagOfIntegers::clear(){
\x05itemCount = 0;}
int BagOfIntegers::getFrequencyOf(const int& anEntry) const{
int frequency = 0;
int searchIndex = 0;
while (searchIndex < itemCount) {
if (items[searchIndex] == anEntry) {
frequency++;}
searchIndex++; }
return frequency;}
bool BagOfIntegers::contains(const int& anEntry) const{
\x05return getIndexOf(anEntry) > -1;}
vector BagOfIntegers::toVector() const{
\x05vector bagContents;
\x05for (int i = 0; i < itemCount; i++)
\x05\x05bagContents.push_back(items[i]);
return bagContents;}
int BagOfIntegers::getIndexOf(const int& target) const{
\x05bool found = false;
int result = -1;
int searchIndex = 0;
while found && (searchIndex < itemCount)) {
if (items[searchIndex] == target) {
found = true;
result = searchIndex; }
else
{
searchIndex++;
} } return result;} #endif
c++ 数据结构,帮下忙re-implement BagOfIntegers to use an array of item counts as the underlying data structure.For example,if the bag is {1,3,3,3,3,5,7,7,8} then the first nine elements of your count array will be 0,1,0,4,0,1,0,2,1.#ifndef _Bag
class BagOfIntegers : public BagInterface<int>
{
private:
...
vector<int> CountArray;
public:
...
void UpdateCountArray()
{
CountArray.clear();
for(int i = 0 ; i < itemCount ; i ++)
{
CountArray.push_back(getFrequencyOf(i));
}
}
};