凌殇️ 2022-03-20 14:14 采纳率: 100%
浏览 18
已结题

求各位解答,求正确方法

为什么我的代码在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"); }

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-03-20 16:21
    关注

    修改处见注释,供参考:

    #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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月5日
  • 已采纳回答 6月27日
  • 创建了问题 3月20日

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭