简单写了一个顺序栈的实现,代码如下:
#ifndef CIRCLE_H
#define CIRCLE_H
#define MaxSize 50;
typedef int ElemType;
struct SqStack //定义栈结构
{
ElemType data[MaxSize];
int top;
};
//栈的初始化
void InitStack(SqStack &T)
{
t.top=-1;
}
//栈的判空
bool IsEmpty(SqStack T)
{
if(T.top==-1)
return true;
else
return false;
}
//入栈操作
bool Push(SqStack &T,ElemType x)
{
if(T.top==MaxSize-1)
return false;
else
{
T->data[++T.top]=x;
return true;
}
}
//出栈操作
bool Pop(SqStack &T,ElemType &x)
{
if(IsEmpty(T))
return false;
else
{
x=T.data[T.top--];
return true;
}
}
//取栈顶元素
bool Top(SqStack T,ElemType &x)
{
if(IsEmpty(T))
return false;
else
{
x=T.data[T.top];
return true;
}
}
#endif
C++好久没用,临近考试未经测试,若有编译错误我虚心改正!