# include<stdio.h>
# include<stdlib.h>
# define Max_Size 50
typedef struct{//typedef是小写开头!
//注意此处的top是整形指针
int data[Max_Size];
int top;
}SeqStack,*PSeqStack;
void Init_SeqStack(PSeqStack S)
{
S->top=-1;//哪里错了?求解释!
}
int Full_SeqStack(PSeqStack S)
{
if(S->top==Max_Size-1) return 1;
else return 0;
}
int Empty_SeqStack(PSeqStack S)
{
if(S->top==-1) return 1;
else return 0;
}
void Push_SeqStack(PSeqStack S,int n)
{
if(Full_SeqStack(S)==1) exit(0);
else
{S->top=++S->top;//及时先加一个空间
S->data[S->top]=n;}//栈也有data域!
}
void Pop_SeqStack(PSeqStack S,int n)//*n也是指针
{
if(Empty_SeqStack(S)==1) printf("UnderFlow\n");
else
{n=S->data[S->top];
S->top=--S->top;}//及时退一个空间
}
int fact(int n,int f,PSeqStack S)//调用函数为什么不对?求解释!
{
Init_SeqStack(S);
while(n!=0)
{
Push_SeqStack(S,n);
n=n-1;
}
f=1;
while(Empty_SeqStack(S)!=1)
{
Pop_SeqStack(S,n);
f=f*n;
}
return f;
}
int main()
{
int n,f;SeqStack *S;
printf("input n:\n");
scanf("%d",&n);
printf("%d\n",fact(n,f,S));
getch();
}
int n,f;SeqStack *S;
->
int n,f;SeqStack *S = (SeqStack *)malloc(sizeof(SeqStack));
指针忘记分配内存了。
#include<conio.h>
# include<stdio.h>
# include<stdlib.h>
# define Max_Size 50
typedef struct{//typedef是小写开头!
//注意此处的top是整形指针
int data[Max_Size];
int top;
}SeqStack,*PSeqStack;
void Init_SeqStack(PSeqStack S)
{
S->top=-1;//哪里错了?求解释!
}
int Full_SeqStack(PSeqStack S)
{
if(S->top==Max_Size-1) return 1;
else return 0;
}
int Empty_SeqStack(PSeqStack S)
{
if(S->top==-1) return 1;
else return 0;
}
void Push_SeqStack(PSeqStack S,int n)
{
if(Full_SeqStack(S)==1) exit(0);
else
{ ++S->top;//修改
S->data[S->top]=n;}//栈也有data域!
}
void Pop_SeqStack(PSeqStack S,int &n)//修改,用引用
{
if(Empty_SeqStack(S)==1) printf("UnderFlow\n");
else
{n=S->data[S->top];
--S->top;}//修改
}
int fact(int n,int f,PSeqStack S)//调用函数为什么不对?求解释!
{
Init_SeqStack(S);
while(n!=0)
{
Push_SeqStack(S,n);
n=n-1;
}
f=1;
while(Empty_SeqStack(S)!=1)
{
Pop_SeqStack(S,n);
f=f*n;
}
return f;
}
int main()
{
int n,f=1; SeqStack S;//修改
printf("input n:\n");
scanf("%d",&n);
printf("%d\n",fact(n,f,&S));//修改
getch();
}
首先栈是有两个指针的 base 和top在创建一个新的栈的时候 你要给
base指针分配一个内存 然后让top等于base