栈。。改错。。
来源:百度知道 编辑:UC知道 时间:2024/07/02 12:03:38
//顺序栈与链栈(1)初如化栈S。
//(2)判断栈S是否非空。
//(3)进栈。
//(4)出栈。
//(5)输出栈S的长度。
#include <stdio.h>
#define maxsize 64
#include <stdlib.h>
typedef char datatype;
typedef struct
{
datatype data[maxsize];
int top;
} stack;
int initstack(stack *s)
{
s=malloc(sizeof(stack)*maxsize);
s->top=-1;
return 0;
} //初始化
int empty(stack *s)
{
if(s->top>=0)
{return 1;
printf("表非空");}
else printf("表为空");
return 0;
}
//判断空
stack *push(stack *s,int x)
{
if(s->top==maxsize-1)
{
printf("溢出");return NULL;
}
else {
s->top++;
s->data[s->top]=x;
}
}
stack *pop(stack *s)
{
if(empty(s))
{printf("溢出");
return NULL;}
//(2)判断栈S是否非空。
//(3)进栈。
//(4)出栈。
//(5)输出栈S的长度。
#include <stdio.h>
#define maxsize 64
#include <stdlib.h>
typedef char datatype;
typedef struct
{
datatype data[maxsize];
int top;
} stack;
int initstack(stack *s)
{
s=malloc(sizeof(stack)*maxsize);
s->top=-1;
return 0;
} //初始化
int empty(stack *s)
{
if(s->top>=0)
{return 1;
printf("表非空");}
else printf("表为空");
return 0;
}
//判断空
stack *push(stack *s,int x)
{
if(s->top==maxsize-1)
{
printf("溢出");return NULL;
}
else {
s->top++;
s->data[s->top]=x;
}
}
stack *pop(stack *s)
{
if(empty(s))
{printf("溢出");
return NULL;}
#include <stdio.h>
#define maxsize 64
#include <stdlib.h>
typedef char datatype;
typedef struct
{
datatype data[maxsize];/*此处已经分配了空间*/
int top;
} stack;
int initstack(stack *s)
{
/*s=malloc(sizeof(stack)*maxsize);*/
s=malloc(sizeof(stack));/*不需要再申请maxsize个了,因为结构体中已经有了,而且你的这种申请相当于系统分配了64个stack。而你只是想要一个吧*/
s->top=-1;
return 0;
} //初始化
以上初始化函数从新修改:
int initstack(stack **s)/*真正的用二级地址保证申请的地址可以传回去*/
{
(*s)=malloc(sizeof(stack));
(*s)->top=-1;
return s;
} /*初始化*/
int empty(stack *s)
{
if(s->top>=0)/*以0为基数开始*/
{return 1;/*此下两句得调换,要不然无法到达printf语句*/
printf("表非空");}
else printf("表为空");
return 0;
}
//判断空
/*stack *push(stack *s,int x)*/
stack *push(stack *s,char x)/*main函数中传递char型,接收却变成int,虽然可以但是你的结构体是char型的,否则可能存在位数丢失的情况*/
{