版主,!! 求解释。。简单的栈问题。
程序代码:#include<stdio.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
struct Node
{
struct Node *next;
int digit;
};
int Pop(struct Node *s,int *i); //出栈函数
int InitQueue(struct Node *s); //制造一个空栈
int Push(struct Node *s,int i); //入栈函数
void conversion(int n,int d); //十进制转换函数
int EmptyQueue(struct Node *s); //判断函数
int main(void)
{
int n = 0;
int m = 0;
printf("Enter tows digit:");
scanf("%d%d",&n,&m);
conversion(n,m);
return 0;
}
int InitQueue(struct Node *s)
{
s = (struct Node *)malloc(sizeof(struct Node));
if(s == NULL)
return FALSE;
s->next = NULL;
return TRUE;
}
int Push(struct Node *s,int i)
{
struct Node *p;
p = (struct Node *)malloc(sizeof(struct Node));
p->digit = i;
p->next = s;
s = p;
return TRUE;
}
int Pop(struct Node *s,int *i)
{
struct Node *p;
p = s;
if(p == NULL)
return FALSE;
*i =p->digit;
s = p->next;
free(p);
return TRUE;
}
void conversion(int n,int d)
{
struct Node *s =NULL;
int *i = NULL;
int x;
InitQueue(s);
while(n > 0)
{
x = n % d;
Push(s,x);
n = n / d;
}
while(!EmptyQueue(s))
{
Pop(s,i);
printf("%d",*i);
}
}
int EmptyQueue(struct Node *s)
{
if(s == NULL)
return FALSE;
else
return TRUE;
}
程序运行错误。。运行错误的原因是指针S为NULL 。 这是为什么,? 我明明已经申请动态内存成功了。




