链栈的入栈出栈代码
发布网友
发布时间:2022-05-30 05:11
我来回答
共1个回答
热心网友
时间:2023-10-09 23:56
这是我写的栈,你看看
#include<stdio.h>
#include<iostream>
typedef struct node{
int date;
node * next;
}SeqStack ;
SeqStack * init_SeqStack(SeqStack * top){
top=NULL;
return top;
}
int is_Empty(SeqStack * top){
if(top==NULL)return 1;
else return 0;
}
SeqStack * push_Stack(SeqStack * top){
SeqStack * New;
New=(SeqStack *)malloc(sizeof(SeqStack));
printf("请输入要入栈的元素\n");
scanf("%d",&New->date);
New->next=top;
top=New;
return top;
}
SeqStack * pop_Stack(SeqStack * top,int &m){
SeqStack * p=NULL;
if(!is_Empty(top)){
m=top->date;
p=top;
top=top->next;
free(p);
return top;
}
}
SeqStack * top_Stack(SeqStack * top,int &m){
if(!is_Empty(top)){
m= top->date;
return top;
}
}
int main(){
int m=0;
SeqStack * s=NULL;
init_SeqStack(s);
s=push_Stack(s);
s=push_Stack(s);
s=push_Stack(s);
s=push_Stack(s);
s=top_Stack(s,m);
printf("%d\n",m);
s=top_Stack(s,m);
printf("%d\n",m);
s=pop_Stack(s,m);
printf("%d\n",m);
s=top_Stack(s,m);
printf("%d\n",m);
if(is_Empty(s)) printf("栈现在是空了");
system("pause");
return 0;
}
追问可以帮我改下入栈和出栈代码吗?真心不会...
追答slStacktype * PushStack(slStacktype *top,int x)//入栈操作
{
slStacktype *p;
p=(slStacktype *)malloc(sizeof(slStacktype));
if(p==NULL)
return FALSE;
p->data=x;
p->next=top;
top=p;
return p; //返回p
}slStacktype * PopStack(slStacktype *top,int *x)//出栈操作
{
slStacktype *p;
if(top==NULL) return NULL;
p=top;
top=top->next;
*x=p->data; //*x
free(p);
return top; //top
}