C++ programming

Implementing stack using STL for extending Bag Interface

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

DS/.DS_Store

__MACOSX/DS/._.DS_Store

DS/ArrayBagTest.cpp
#include // For cout and cin
#include // For string objects
#include “ArrayBag.h”// For ADT bag
#include “ArrayBag.cpp”
#include “StackSTL.h”
#include “StackSTL.cpp”
#include
#include “BagInterface.h”
using namespace std;
int main()
{
/*ArrayBag bags;
bags.add(‘a’);
bags.print();
bags.add(‘b’);
bags.print();
cout<<(bags.contains('a') ? "YES" : "NO")< *bag = stack();
stack();
bag->add(‘a’);
//bag->print();
return 0;
} // end main

__MACOSX/DS/._ArrayBagTest.cpp

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

DS/BagInterface.h
#ifndef BAG_INTERFACE_H
#define BAG_INTERFACE_H
#include
#include
#include
using namespace std;
template
class BagInterface {

public:

~BagInterface() {}
virtual int getCurrentSize(void) const = 0;
virtual bool isEmpty(void) const = 0;
virtual bool add(const ItemType& item) = 0;
virtual bool remove(const ItemType& item) = 0;
virtual void clear(void) = 0;
void print() const;
virtual int getFrequencyOf(const ItemType& item) const = 0;
virtual bool contains(const ItemType& item) const = 0;
virtual vector toVector(void) const = 0;
};
#endif

__MACOSX/DS/._BagInterface.h

DS/ArrayBag.cpp
#include
#include “ArrayBag.h”
template
ArrayBag::ArrayBag():itemCount(0),maxItems(DEFAULT_CAPACITY)
{
} // default constructor
template
int ArrayBag ::getCurrentSize() const {
return itemCount;
} // end getCurrentSize
template
bool ArrayBag ::isEmpty() const {
return itemCount == 0;
} // end isEmpty
template
bool ArrayBag::add(const ItemType& newEntry) {
bool hasRoomToAdd(itemCount < maxItems); if (hasRoomToAdd) { items[itemCount] = newEntry; itemCount++; } return hasRoomToAdd; } // end add function template
bool ArrayBag::remove(const ItemType& anEntry) {
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem) {
itemCount–;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
} // end remove
template
void ArrayBag::clear() {
itemCount = 0;
} // end clear
template
bool ArrayBag::contains(const ItemType& anEntry) const {
int curIndex(0);
while (curIndex < itemCount) { if (items[curIndex] == anEntry) { return true; } curIndex++; } return false; } // end contains template
int ArrayBag::getFrequencyOf(const ItemType& anEntry) const {
int frequency(0);
int curIndex(0);
while (curIndex < itemCount) { if (items[curIndex] == anEntry) { frequency++; } curIndex++; } return frequency; } // end getFrequency template
vector ArrayBag::toVector() const {
vector bagContents;
for (int i=0; i < itemCount; i++) bagContents.push_back(items[i]); return bagContents; } // end vector template
void ArrayBag::print() const
{
for(int i = 0; i < itemCount; i++) cout << items[i] << " "; cout << endl; } // end print template
int ArrayBag::getIndexOf(const ItemType& target) const {
bool found = false;
int result = -1;
int searchIndex = 0;

while(!found && (searchIndex < itemCount)) { if(items[searchIndex] == target) { found = true; result = searchIndex; } else { searchIndex++; } } return result; } // end getIndexOf __MACOSX/DS/._ArrayBag.cpp DS/ArrayBag.h #ifndef ARRAY_BAG_H #define ARRAY_BAG_H #include
#include “BagInterface.h”
template
class ArrayBag : public BagInterface {
public:

ArrayBag();
~ArrayBag(){};
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& item);
bool remove(const ItemType& item);
bool contains(const ItemType& anEntry) const;
void clear();
void print() const;
int getFrequencyOf(const ItemType& item) const;
vector toVector() const;
private:
static const int DEFAULT_CAPACITY = 6;
ItemType items[DEFAULT_CAPACITY];
int itemCount;
int maxItems;
int getIndexOf(const ItemType& item) const;
};
#endif

__MACOSX/DS/._ArrayBag.h

DS/StackSTL.h
#ifndef STACKSTL_H
#define STACKSTL_H
#include “BagInterface.h”
#include
template
class StackSTL : public BagInterface
{
public:
StackSTL();
bool add(const ItemType& newEntry);
/*bool remove(const ItemType& anEntry);
virtual ~StackSTL() { }
void print() const;*/
private:
stack stackBag;
};
#include “StackSTL.cpp”
#endif

__MACOSX/DS/._StackSTL.h

DS/a.out

DS/StackSTL.cpp
#ifndef STACKSTL_CPP
#define STACKSTL_CPP
#include
#include
#include
#include
using namespace std;
template
StackSTL::StackSTL(){}
template
bool StackSTL::add(const ItemType & newEntry)
{
stackBag.push(newEntry);
} //end push function
/*template < class ItemType >
bool StackSTL::remove(const ItemType & anEntry)
{
stackBag.pop(anEntry);
} *///end pop
#endif

__MACOSX/DS/._StackSTL.cpp

Order a unique copy of this paper

600 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
Top Academic Writers Ready to Help
with Your Research Proposal

Order your essay today and save 25% with the discount code GREEN