#include <iostream>
using namespace std;
#define ERROR 0
#define OK 1
typedef int Selemtype;
typedef int Status;
//链栈的存储结构
typedef struct StackNode{
Selemtype data;
struct StackNode *next;
}StackNode,*LinkStack;
//初始化//没有头结点
Status InitStack(LinkStack &S)//构造一个空栈,栈顶指针置空
{
S=NULL;
return OK;
}
//入栈
Status Push(LinkStack &S,Selemtype e)//在栈顶插入元素e
{ StackNode *p;
p=new StackNode;//生成新结点
p->data=e;
p->next=S;
S=p;
return OK;
}
//出栈
Status Pop(LinkStack &S,Selemtype &e)
{ StackNode *p;
if(S==NULL) return ERROR;
e=S->data;
p=S;
S=S->next;//修改栈顶指针
delete p;//释放原栈顶结点空间
return OK;
}
int main()
{
LinkStack s;
Selemtype e;
cout<<"进栈元素依次为:"<<endl;
if(InitStack(s)==OK)
for(int j=1;j<=15;j++)
{
Push(s,j);
cout<<j<<" ";
}
cout<<endl<<"依次弹出的栈顶元素为:"<<endl;
while(Pop(s,e))
{
cout<<e<<" ";
}
return 0;
}
<img src="https://img-blog.csdn.net/20141022161907460?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDc5NTc2Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
链栈的实现
最新推荐文章于 2022-12-03 09:55:03 发布