PersonalCorpus 版 (精华区)
// Stack.cpp: implementation of the Stack class.
//
//////////////////////////////////////////////////////////////////////
#include "Stack.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Stack::Stack():top(-1)
{
}
Stack::~Stack()
{
}
// push item on the the stack
void Stack::Push (const DataType& item)
{
// if stacklist is full, terminate the program
if (top == MaxStackSize-1)
{
cerr << "Stack overflow!" << endl;
}
// increment top and copy item to stacklist
top++;
stacklist[top] = item;
}
// pop the stack and return the top element
DataType Stack::Pop (void)
{
DataType temp;
// if stack is empty, terminate the program
if (top == -1)
{
cerr << "Attempt to pop an empty stack!" << endl;
}
// record the top element
temp = stacklist[top];
// decrement top and return former top element
top--;
return temp;
}
// return the value at the top of the stack
DataType Stack::Peek (void) const
{
// if the stack is empty, terminate the program
if (top == -1)
{
cerr << "Attempt to peek at an empty stack!" << endl;
}
return stacklist[top];
}
// test for an empty stack
int Stack::StackEmpty(void) const
{
// return the logical value top == -1
return top == -1;
}
// test for a full stack
int Stack::StackFull(void) const
{
// test the position of top
return top == MaxStackSize-1;
}
// clear all items from the stack
void Stack::ClearStack(void)
{
top = -1;
}
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:1.813毫秒