topical media & game development
basic-program-solutions-08-Soln8-6.c
? /
basic-program-solutions-08-Soln8-6.c
// Soln8_6.cpp
include <iostream> // For stream input/output
using std::cout;
using std::endl;
class CStack
{
public:
CStack(int n = 10);
~CStack();
void push(int i);
int pop();
void print();
private:
int* pList;
int size;
int next;
};
CStack::CStack(int n) : next(0), size(n)
{
pList = new int[size];
}
CStack::~CStack()
{
delete [] pList;
}
void CStack::push(int i)
{
if (next < 99)
pList[next++] = i;
}
int CStack::pop()
{
return pList[--next];
}
void CStack::print()
{
cout << '[';
for(int i=next-1 ; i>=0 ; i--)
cout << ' '<< pList[i];
cout << " ]\n";
}
int main()
{
CStack s(20);
s.print();
s.push(5);
s.push(10);
s.push(8);
s.print();
cout << "top of stack=" << s.pop() << endl;
s.print();
return 0;
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.