问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
#include
#include
typedef struct Stacknode
{
int data;
struct Stacknode* next;
}Stacknode,*Stacklink;
void Init(Stacklink S);
int Push(Stacklink S,int x);
void Pop(Stacklink S);
int Getx(Stacklink S);
void Convert(Stacklink S,int N,int n);
int main()
{
int N,n;
Stacklink S;
Init(S);
printf("请输入待转化数及转化进制数:");
scanf("\n%d %d",&N,&n);
printf("\n转换后的数为:");
Convert(S,N,n);
}
/*初始化空栈*/
void Init(Stacklink S)
{
S=(Stacklink)malloc(sizeof(Stacklink));
S=NULL;
}
/*进栈*/
int Push(Stacklink S,int x)
{
Stacklink p;
p=(Stacklink)malloc(sizeof(Stacknode));
p->data =x;
p->next =S;
S=p;
}
/*出栈*/
void Pop(Stacklink S)
{
if(!S )
{
printf("\n此栈为空栈!");
}
else
{
Stacklink p;
p=(Stacklink)malloc(sizeof(Stacknode));
p=S;
S=S->next ;
free(p);
}
}
/*返回栈顶的值*/
int Getx(Stacklink S)
{
if(S)
{
printf("S->data");
}
}
/*进制转换*/ /*取出栈顶元素再删除栈顶元素*/
void Convert(Stacklink S,int N,int n)
{
while(N!=0)
{
Push(S,N%n);
N=N/n;
Getx(S);
Pop(S);
}
}
运行结果及报错内容

#include <stdio.h>
#include <stdlib.h>
typedef struct Stacknode
{
int data;
struct Stacknode *next;
} Stacknode, *Stacklink;
// void Init(Stacklink *S);
void Push(Stacklink *S, int x);
int Pop(Stacklink *S);
// int Getx(Stacklink S);
void Convert(Stacklink *S, int N, int n);
void Print(Stacklink *S);
int main()
{
int N, n;
Stacklink S = NULL;
// Init(&S);
printf("请输入待转化数及转化进制数:\n"); //
scanf("%d%d", &N, &n); //
printf("\n转换后的数为:");
Convert(&S, N, n);
Print(&S);
}
/*初始化空栈*/
// void Init(Stacklink *S) //
// {
// *S = (Stacklink)malloc(sizeof(Stacknode));
// (*S)->next = NULL;
// // S = NULL;
// }
/*进栈*/
void Push(Stacklink *S, int x)
{
Stacklink p = (Stacklink)malloc(sizeof(Stacknode));
p->data = x;
p->next = *S;
*S = p;
}
/*出栈*/
int Pop(Stacklink *S) //
{
int rst = 0;
if ((*S) == NULL)
{
printf("\n此栈为空栈!");
}
else
{
// Stacklink p;
// p = (Stacklink)malloc(sizeof(Stacknode));
Stacklink p = *S;
rst = (*S)->data;
(*S) = (*S)->next;
free(p);
}
return rst;
}
/*返回栈顶的值*/
// int Getx(Stacklink S)
// {
// if (S)
// {
// printf("%d", S->data); //
// }
// }
/*进制转换*/ /*取出栈顶元素再删除栈顶元素*/
void Convert(Stacklink *S, int N, int n)
{
if (N == 0)
Push(S, N);
else
{
while (N > 0)
{
Push(S, N % n);
N = N / n;
// Getx(S);
}
}
}
//输出并出栈
void Print(Stacklink *S)
{
int rst;
while (*S)
{
rst = Pop(S);
printf("%c", (rst > 9) ? rst - 10 + 'A' : rst + '0');
}
}