//链栈
#include<iostream>
#include<string>
using namespace std;
typedef struct StackNode
{
char data;
struct StackNode *next;
}StackNode,*LinkStack;
//初始化
bool InitStack(LinkStack &L)
{
L=NULL;
return true;
}
//入栈
bool Push(LinkStack &L,char a)
{
LinkStack q;
q=new StackNode;
q->data=a;
q->next=L;
L=q;
return true;
}
//出栈
char Pop(LinkStack &L)
{
char e;
if(L==NULL)
return -1;
LinkStack q=L;
e=L->data;
L=L->next;
return e;
}
//获取栈顶元素
char GetTop(LinkStack &L)
{
if(L!=NULL)
return L->data;
}
//置空
void cleanStack(LinkStack &L)
{
L=NULL;
}
//销毁
void DeleteStack(LinkStack &L)
{
LinkStack q=new StackNode;
while(L)
{
q=L;
L=L->next;
delete L;
}
L=NULL;
}
int main(){
LinkStack L;
InitStack(L);
for(int i=0;i<5;i++)
{
char a;
cin>>a;
Push(L,a);
}
cout<<GetTop(L)<<endl;
for(int i=0;i<5;i++)
cout<<Pop(L)<<" ";
cleanStack(L);
DeleteStack(L);
return 0;
}
标签:LinkStack,char,return,c++,链栈,StackNode,NULL,data From: https://blog.csdn.net/2301_79727388/article/details/142963167