/* mystack.h */
enum Error_code
{
suclearcase/" target="_blank" >ccess,
overflow,
underflow
};
const int maxstack = 100;
class mystack
{
public:
mystack();
Error_code push(const Stack_entry &item);
Error_code pop();
Error_code top(Stack_entry &item) const;
bool empty() const;
private:
int count;
Stack_entry entry[maxstack];
};
/* mystack.cpp */
typedef char Stack_entry;
#include "mystack.h"
Error_code mystack::push(const Stack_entry &item)
{
Error_code outcome = success;
if(count < maxstack)
{
entry[count++] = item;
}
else
{
outcome = overflow;
}
return outcome;
}
Error_code mystack::pop()
{
Error_code outcome = success;
if(count == 0)
{
outcome = underflow;
}
else
{
--count;
}
return outcome;
}
Error_code mystack::top(Stack_entry &item) const
{
Error_code outcome = success;
if(count == 0)
{
outcome = underflow;
}
else
{
item = entry[count-1];
}
return outcome;
}
bool mystack::empty() const
{
bool outcome;
if (count == 0)
{
outcome = true;
}
else
{
outcome = false;
}
return outcome;
}
mystack::mystack()
{
count = 0;
}
/* 测试例子 */
typedef char Stack_entry;
#include "mystack.h"
#include <string.h>
#include <stdio.h>
void main()
{
char my[] = "who am i";
int length = strlen(my);
int i;
char c;
mystack haha;
for(i = 0; i < length; i++)
{
if (haha.push(my[i]) == overflow)
{
printf("error
");
}
}
while(!haha.empty())
{
haha.top(c);
printf("%c",c);
haha.pop();
}
printf("
");
}