栈 链式 代码

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node{
int data;
node *next;
}nd;
typedef struct stack{
nd *top;
int ct;
}sta;
void initstack(sta &s)
{
s.top=NULL;
s.ct=0;
}
void pushstack(sta &s,int e)
{
nd *p;
p=(node*)malloc(sizeof(node));
p->data=e;
cout<<e<<"进栈"<<endl;
p->next=s.top;
s.top=p;
s.ct++;
}
int popstack(sta &s,int &e)
{
if(s.ct==0)
{
cout<<"空栈"<<endl;
return 0;
}
nd *p;
e=s.top->data;
cout<<e<<"出栈"<<endl;
p=s.top;
s.top=p->next;
s.ct--;
free(p);
return 1;
}
int main()
{
sta s1;
initstack(s1);
pushstack(s1,3);
int c=0;
popstack(s1,c);
popstack(s1,c);
return 0;
}

朝闻道
原文地址:https://www.cnblogs.com/wander-clouds/p/8443710.html