C++ Programming Implementing stack using STL for extending Bag Interface DS/.DS_Store
__MACOSX/DS/._.DS_Store
DS/ArrayBagTest.cpp Implementing stack using STL for extending Bag Interface #include <iostream> // For cout and cin int main() for(char x: bags.toVector()) cout<<x<< ” “; cout<<“Testing Stack STL Implementation”<<endl; #ifndef BAG_INTERFACE_H template <class ItemType> public: ~BagInterface() {} #endif
#include <vector> template <class ItemType> template <class ItemType> template <class ItemType> return itemCount == 0; template <class ItemType> bool hasRoomToAdd(itemCount < maxItems); if (hasRoomToAdd) { template <class ItemType> int locatedIndex = getIndexOf(anEntry); template <class ItemType> itemCount = 0; template <class ItemType> int curIndex(0); while (curIndex < itemCount) { return false; template <class ItemType> int frequency(0); while (curIndex < itemCount) { template <class ItemType> vector<ItemType> bagContents; template <class ItemType> template <class ItemType> bool found = false; while(!found && (searchIndex < itemCount)) #ifndef ARRAY_BAG_H #include <vector> template <class ItemType> public: ArrayBag(); private: static const int DEFAULT_CAPACITY = 6; }; #ifndef STACKSTL_H #include “BagInterface.h” class StackSTL : public BagInterface<ItemType> #ifndef STACKSTL_CPP template<class ItemType> template <class ItemType > /*template < class ItemType >
#include DS/.DS_Store
__MACOSX/DS/._.DS_Store
DS/ArrayBagTest.cpp
#include <string> // For string objects
#include “ArrayBag.h”// For ADT bag
#include “ArrayBag.cpp”
#include “StackSTL.h”
#include “StackSTL.cpp”
#include <stack>
#include “BagInterface.h”
using namespace std;
{
/*ArrayBag<char> bags;
bags.add(‘a’);
bags.print();
bags.add(‘b’);
bags.print();
cout<<(bags.contains(‘a’) ? “YES” : “NO”)<<endl;
cout<<bags.getCurrentSize()<<endl;
bags.print();
bags.remove(‘a’);
bags.print();
cout<<(bags.contains(‘a’) ? “YES” : “NO”)<<endl;
cout<<bags.getCurrentSize()<<endl;
cout<<endl;*/
// BagInterface<char> *bag = stack<char>();
stack<char>();
bag->add(‘a’);
//bag->print();
return 0;
} // end main__MACOSX/DS/._ArrayBagTest.cpp
DS/BagInterface.h
#define BAG_INTERFACE_H
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class 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<ItemType> toVector(void) const = 0;
};__MACOSX/DS/._BagInterface.h
DS/ArrayBag.cpp
#include “ArrayBag.h”
ArrayBag<ItemType>::ArrayBag():itemCount(0),maxItems(DEFAULT_CAPACITY)
{
} // default constructor
int ArrayBag <ItemType>::getCurrentSize() const {
return itemCount;
} // end getCurrentSize
bool ArrayBag <ItemType>::isEmpty() const {
} // end isEmpty
bool ArrayBag<ItemType>::add(const ItemType& newEntry) {
items[itemCount] = newEntry;
itemCount++;
}
return hasRoomToAdd;
} // end add function
bool ArrayBag<ItemType>::remove(const ItemType& anEntry) {
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem) {
itemCount–;
items[locatedIndex] = items[itemCount];
}
return canRemoveItem;
} // end remove
void ArrayBag<ItemType>::clear() {
} // end clear
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const {
if (items[curIndex] == anEntry) {
return true;
}
curIndex++;
}
} // end contains
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const {
int curIndex(0);
if (items[curIndex] == anEntry) {
frequency++;
}
curIndex++;
}
return frequency;
} // end getFrequency
vector<ItemType> ArrayBag<ItemType>::toVector() const {
for (int i=0; i < itemCount; i++)
bagContents.push_back(items[i]);
return bagContents;
} // end vector
void ArrayBag<ItemType>::print() const
{
for(int i = 0; i < itemCount; i++)
cout << items[i] << ” “;
cout << endl;
} // end print
int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const {
int result = -1;
int searchIndex = 0;
{
if(items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
}
}
return result;
} // end getIndexOf__MACOSX/DS/._ArrayBag.cpp
DS/ArrayBag.h
#define ARRAY_BAG_H
#include “BagInterface.h”
class ArrayBag : public BagInterface<ItemType> {
~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<ItemType> toVector() const;
ItemType items[DEFAULT_CAPACITY];
int itemCount;
int maxItems;
int getIndexOf(const ItemType& item) const;
#endif__MACOSX/DS/._ArrayBag.h
DS/StackSTL.h
#define STACKSTL_H
#include <stack>
template <class ItemType>
{
public:
StackSTL();
bool add(const ItemType& newEntry);
/*bool remove(const ItemType& anEntry);
virtual ~StackSTL() { }
void print() const;*/
private:
stack<ItemType> stackBag;
};
#include “StackSTL.cpp”
#endif
__MACOSX/DS/._StackSTL.h
DS/a.out
DS/StackSTL.cpp
#define STACKSTL_CPP
#include<iostream>
#include<string>
#include<cstddef>
#include <stack>
using namespace std;
StackSTL<ItemType>::StackSTL(){}
bool StackSTL<ItemType>::add(const ItemType & newEntry)
{
stackBag.push(newEntry);
} //end push function
bool StackSTL<ItemType>::remove(const ItemType & anEntry)
{
stackBag.pop(anEntry);
} *///end pop
#endif
__MACOSX/DS/._StackSTL.cpp