为什么我的代码在vc++6.0一直说'top' is not a member of 'node'
typedef struct node{
char info;
struct node *link;
} STACK;
STACK *top;
push(arg)
char arg; {
STACK *ptr;
ptr=(STACK *)malloc(sizeof(STACK));
ptr->info=arg;
ptr->link=top;
top=ptr; }
char pop()
{ STACK *ptr;
char arg;
if(top=='\0')
return('\0');
else{
ptr=top;
top=top->link;
arg=ptr->info;
free(ptr);
return(arg); } }
main()
{STACK *s1;
int n,dt;
s1.top=0;
printf("Input a decimal number:");
scanf("%d",&n);
while( n != 0){
push(&s1,n%8);
n=n/8; }
printf("The octal number is:");
while((dt=pop(&s1))!= -1)
printf("%d",dt);
printf("\n"); }
修改处见注释,供参考:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int info; //char info
struct node *link;
} STACK;
//STACK *top;
void push(STACK**top,int arg) //修改
{
STACK *ptr;
ptr=(STACK *)malloc(sizeof(STACK));
ptr->link = NULL;
ptr->info=arg;
ptr->link=(*top); //修改
(*top)=ptr; //修改
}
int pop(STACK**top) //char pop() //修改
{
STACK *ptr;
int arg; //char arg;
if((*top) == NULL) //if(top=='\0')
return -1; //return('\0');
else{
ptr=(*top); //修改
(*top)=(*top)->link;//修改
arg=ptr->info;
free(ptr);
return(arg);
}
}
int main()
{
STACK *s1 = NULL;
int n,dt;
//s1.top=0;
printf("Input a decimal number:");
scanf("%d",&n);
while( n != 0){
push(&s1,n%8);
n=n/8;
}
printf("The octal number is:");
while((dt=pop(&s1))!= -1)
printf("%d",dt);
printf("\n");
return 0;
}
换个运行环境