#include<stdio.h>
#define N 100005
typedef struct{
int *base;//栈底==首元素
int *top;//指针就是一个元素
int stacksize;//最大空间
}SqStack;
int InitStack(SqStack &S)
{
S.base=new int[N];//首元素开路
if(!S.base)//没分配就是0,有分配就有一个地址
return 0;
S.top=S.base;//都指向0位置
S.stacksize=N;//开辟大小
return 0;
}
bool Empty(SqStack S)//是否为空
{
if(S.top==S.base)
return true;
else
return false;
}
int Lenth(SqStack S)//求顺序栈长度
{
return S.top-S.base;
}
void ClearStack(SqStack &S)//清空栈
{
if(!S.base)
S.top=S.base;
}
void DestroyStack(SqStack &S){//销毁栈
if(S.base)
{
delete S.base;
S.stacksize=0;
S.base=S.top=NULL;
}
}
void Push(SqStack &S,int e){//e入栈
if(S.top-S.base==S.stacksize)//栈满了
return;
*S.top=e;//取他所指的这块空间,把e放进去
S.top++;//指针上移
//也可以合二为一*S.top++,*的优先级更高
}
void Pop(SqStack &S,int &e)//返回被弹出的数e
{
if(S.top==S.base)
return;
e=*--S.top;//S.top指的是栈顶上面的空位置,所以先减再取值
//也可以分两步:--S.top;e=*S.top;
}
int main()
{
return 0;
}
数据结构——顺序栈操作集(C/C++)
最新推荐文章于 2024-07-22 20:42:27 发布