In: Computer Science
Implement the following functions for an array based
stack.
#include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT ** // //-------------------------------------------------------------------- //-------------------------------------------------------------------- template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) // Creates an empty stack. : maxSize(maxNumber), top(-1) { } //-------------------------------------------------------------------- template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) // Copy constructor for stack : maxSize( other.maxSize ), top( other.top ) { } //-------------------------------------------------------------------- template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) // Overloaded assignment operator for the StackArray class. // Because this function returns a StackArray object reference, // it allows chained assignment (e.g., stack1 = stack2 = stack3). { return *this; } //-------------------------------------------------------------------- template <typename DataType> StackArray<DataType>::~StackArray() // Frees the memory used by a stack. { } //-------------------------------------------------------------------- template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) // Inserts newDataItem onto the top of a stack. { } //-------------------------------------------------------------------- template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) // Removes the topmost data item from a stack and returns it. { return dataItems[top]; } //-------------------------------------------------------------------- template <typename DataType> void StackArray<DataType>::clear() // Removes all the data items from a stack. { } //-------------------------------------------------------------------- template <typename DataType> bool StackArray<DataType>::isEmpty() const // Returns true if a stack is empty. Otherwise, returns false. { return true; } //-------------------------------------------------------------------- template <typename DataType> bool StackArray<DataType>::isFull() const // Returns true if a stack is full. Otherwise, returns false. { return true; } //-------------------------------------------------------------------- template <typename DataType> void StackArray<DataType>::showStructure() const // Array implementation. Outputs the data items in a stack. If the // stack is empty, outputs "Empty stack". This operation is intended // for testing and debugging purposes only. { if( isEmpty() ) { cout << "Empty stack." << endl; } else { int j; cout << "Top = " << top << endl; for ( j = 0 ; j < maxSize ; j++ ) cout << j << "\t"; cout << endl; for ( j = 0 ; j <= top ; j++ ) { if( j == top ) { cout << '[' << dataItems[j] << ']'<< "\t"; // Identify top } else { cout << dataItems[j] << "\t"; } } cout << endl; } cout << endl; }
#include <stdexcept>
#include <iostream>
using namespace std;
#include "Stack.h"
template <typename DataType>
class StackArray : public Stack<DataType> {
public:
StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE);
StackArray(const StackArray& other);
StackArray& operator=(const StackArray& other);
~StackArray();
void push(const DataType& newDataItem) throw (logic_error);
DataType pop() throw (logic_error);
void clear();
bool isEmpty() const;
bool isFull() const;
void showStructure() const;
private:
int maxSize;
int top;
DataType* dataItems;
};
template <typename DataType>
StackArray<DataType>::StackArray(int maxNumber): maxSize(maxNumber), top(-1)
{
dataItems=new DataType[maxSize]();
}
template <typename DataType>
StackArray<DataType>::StackArray(const StackArray& other): maxSize( other.maxSize ), top( other.top )
{
dataItems=new DataType[maxSize];
for(int i=0;i<maxSize;i++)
dataItems[i]=other.dataItems[i];
}
//--------------------------------------------------------------------
template <typename DataType>
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)
{
this.maxSize=other.maxSize;
this.top=other.top;
this.dataItems=new DataType[maxSize];
for(int i=0;i<maxSize;i++)
this.dataItems[i]=other.dataItems[i];
return *this;
}
template <typename DataType>
StackArray<DataType>::~StackArray(){
delete []dataItems;
top=-1;
maxSize=0;
}
template <typename DataType>
void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error){
if(isFull())
return;
top=top+1;
dataItems[top]=newDataItem;
}
template <typename DataType>
DataType StackArray<DataType>::pop() throw (logic_error){
if(isEmpty())
return 0;
top=top-1;
return dataItems[top+1];
}
template <typename DataType>
void StackArray<DataType>::clear(){
top=-1;
}
template <typename DataType>
bool StackArray<DataType>::isEmpty() const{
if(top==-1)
return true;
return false;
}
template <typename DataType>
bool StackArray<DataType>::isFull() const{
if(top+1==maxSize)
return true;
return false;
}
template <typename DataType>
void StackArray<DataType>::showStructure() const{
if( isEmpty() ) {
cout << "Empty stack." << endl;
}
else {
int j;
cout << "Top = " << top << endl;
for ( j = 0 ; j < maxSize ; j++ )
cout << j << "\t";
cout << endl;
for ( j = 0 ; j <= top ; j++ )
{
if( j == top )
{
cout << '[' << dataItems[j] << ']'<< "\t"; // Identify top
}
else
{
cout << dataItems[j] << "\t";
}
}
cout << endl;
}
cout << endl;
}