这两个程序都是顺序栈,有什么什么错误?
#include<stdio.h>#include<stdlib.h>
#include<malloc.h>
#define MAX 100
typedef struct
{
int data[20];
int top;
}sqstack;
void inti(sqstack *s)
{
s->top=0;
printf("初始化队列成功!\n");
}
int emptystack(sqstack *s)
{
if(s->top==0)
return (1);
else
return (0);
}
int Fullstack(sqstack *s)
{
if(s->top==20)
{
return (1);
}
else
{
return (0);
}
}
void pushstack(sqstack *s,int x)
{
if(Fullstack(s))
{
printf("顺序栈为满!\n");
}
else
{
s->data[s->top]=x;
s->top++;
printf("压栈成功!\n");
}
}
void print(sqstack *s)
{
int p;
p=s->top-1;
printf("%d\n",p);
if(s->top==0)
printf("stack is NULL!\n");
else
while(p>=0)
{
printf("%d ",s->data[p]);
p=p--;
}
printf("\n");
}
int popstack(sqstack *s)
{
int e;
if(emptystack(s))
{
printf("顺序栈为空!\n");
}
else
{
s->top--;
e=s->data[s->top];
return e;
}
}
int main()
{
sqstack *s;int x;
s=(sqstack *)malloc(sizeof(sqstack));
inti(s);
pushstack(s,1);
pushstack(s,2);
pushstack(s,3);
pushstack(s,4);
print(s);
x=popstack(s);
printf("弹栈的元素为%d\n",x);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 100
typedef struct
{
int *base;
int *top;
}stack;
void inti(stack *s)
{
s->base=(int *)malloc(MAX*sizeof(int));
s->top=s->base;
printf(" 初始化顺序栈成功!\n");
}
void push(stack *s,int e)
{
if(s->top-s->base>=MAX)
{
printf("顺序栈已满!\n");
}
else
{
*s->top++=e;
printf("入栈成功!\n");
}
}
void pop(stack *s)
{
int e;
e=*--s->top;
printf("弹栈的元素为%d\n",e);
}
void print(stack * s)
{
int *p;
while(s->top!=s->base)
{
s->top--;
printf("%d ",*(s->top));
}
}
int main()
{
stack s;
inti(&s);
push(&s,5);
push(&s,4);
push(&s,3);
pop(&s);
print(&s);
return 0;
}