//-------------DStack.h----------------
#include<iostream>
#ifndef DSTACK
#define DSTACK
typedef int StackElement;
class Stack
{
public:
Stack(int numElements=128);
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
void pop();
private:
int myTop;
int myCapacity;
StackElement *myArray;
};
#endif
//------------DStack.cpp------------------
#include<iostream>
#include<cassert>
#include<new>
using namespace std;
#include"DStack.h"
Stack::Stack(int numElements)
{
assert(numElements>0);
myCapacity=numElements;
myArray=new (nothrow) StackElement[myCapacity];
if(myArray!=0)
myTop=-1;
else
{
cout<<"Inadequate memory to allocate stack \n";
exit(1);
}
}
bool Stack::empty() const
{
return (myTop==-1);
}
void Stack::push(const StackElement & value)
{
if(myTop<myCapacity-1)
{
myTop++;
myArray[myTop]=value;
}
else
{
cerr<<"***Stack full -- can't add new value ***\n";
exit(1);
}
}
void Stack::display(ostream & out) const
{
for(int i=myTop;i>=0;i--)
out<<myArray[i]<<endl;
}
StackElement Stack::top() const
{
if(!empty())
{
return (myArray[myTop]);
}
else
{
cerr<<"***Stack is empty -- returning garbage value ***"
<<endl;
StackElement garbage;
return garbage;
}
}
void Stack::pop()
{
if(!empty())
myTop--;
else
cerr<<"*** Stack is empty -- can't remove a value ***\n";
}
//-------------DStack_main.cpp---------------------
#include<iostream>
using namespace std;
#include"DStack.h"
int main()
{
Stack s;
cout<<"Enter second stack's capacity: ";
int cap;
cin>>cap;
Stack s2(cap);
/*
//
//
//
*/
return 0;
}