线性表——栈的实现
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int top;//栈顶“下标”
}SeqStack;
//初始化栈
void InitStack(SeqStack &s)
{
s.top = -1;
}
//检查空栈
bool EmptyStack(SeqStack s)
{
if (-1 == s.top)
{
return true;
}
return false;
}
//压栈(增)
bool push(SeqStack &s, ElemType a)
{
if (s.top == MaxSize - 1)
{
return false;
}
s.data[++s.top] = a;
return true;
}
//弹栈(删)
bool pop(SeqStack& s, ElemType &a)
{
if (s.top ==- 1)
{
return false;
}
a = s.data[s.top];
s.top–;
return true;
}
//读取栈顶元素(查)
bool GetTop(SeqStack s,ElemType &e)
{
if (s.top == -1)
{
return false;
}
e = s.data[s.top];
return true;
}
int main()
{
SeqStack s;
InitStack(s);//初始化结构体
ElemType a;
bool flag;
if (EmptyStack)
{
printf("栈是空的");
printf("\n");
}
push(s, 6);
push(s, 7);
push(s, 8);
if (pop(s,a))
{
printf("删除栈顶元素成功,删除元素为: %d",a);
printf("\n");
}
else {
printf("删除栈顶元素失败");
printf("\n");
}
return 1;
}