zxz.QAQ 2023-10-10 11:01 采纳率: 50%
浏览 8
已结题

QAQ有人能看看这个哪里出现错误了吗

```

#include<stdio.h>

#include<stdlib.h>

struct  Zimu
{
    char letter;
    struct  Zimu* next;
};

struct Stack
{
    struct Zimubase;//栈底
    struct Zimu
top;//栈顶
    int Size;
};//栈类型

struct Zimu* creatNode(char letter)//创建节点
{
    struct Zimu* newNode = (struct Zimu*)malloc(sizeof(struct Zimu));
    newNode->letter = letter;
    newNode->next = NULL;
    return newNode;
}

//初始化
int initstack(struct Stack* S)
{
    S->base = (struct Zimu*)malloc(sizeof(struct Zimu));
    if(!S->base)exit(-1);
    S->top = S->base;
    S->Size = 0;
    return 0;
}

void push(struct Stack * S)//进栈
{
    char letter;
    printf("请输入字母\n");
    scanf(" %c", &letter); // 使用 " %c" 读取字符,防止上一行结尾的换行符干扰
    struct Zimu* newnode = creatNode(letter);
    newnode->next = S->top;
    S->top = newnode;
    S->Size++;
}

int top(struct Stack* S)//获取栈顶元素
{
    if(S->Size == 0)
    {
        printf("栈为NULL\n");
        return 0;
    }
    return S->top->letter;
}

void pop(struct Stack* S)//出栈
{
    if(S->Size == 0)
    {
        printf("栈为NULL\n");
    }
    else
    {
        struct Zimu* nextnode = S->top->next;
        free(S->top);
        S->top = nextnode;
        S->Size--;
    }
}

int empty(struct Stack* S)
{
    if(S->Size == 0)
        return 0;
    return 1;
}

char letter;
int main()
{
    int t;
    struct Stack* S;
    initstack(S);
    push(S);
    printf("请选择你要进行的操作:\n");
    scanf("%d", &t);
    do
    {
        switch(t)
        {
            case 1: push(S);
                break;
            case 2: pop(S);
                break;
            case 3: printf("%c", S->top->letter); // 使用 %c 打印字符
                pop(S);
                break;
            default :
                break;                
        }
    }
    while(t>=1&&t<=3);
    return 0;
}```

  • 写回答

2条回答 默认 最新

  • 柯本 2023-10-10 11:11
    关注

    你的Stack定义不正确

    struct Stack
    {
        struct Zimubase;//栈底
        struct Zimutop;//栈顶
        int Size;
    };//栈类型
    
    

    改为

    struct Stack
    {
        struct Zimu *base;//栈底
        struct Zimu *top;//栈顶
        int Size;
    };//栈类型
    
    
    

    程序就可以编译运行了
    你程序还有逻辑错,完整改好的

    #include<stdio.h>
    
    #include<stdlib.h>
    
    struct  Zimu
    {
        char letter;
        struct  Zimu* next;
    };
    
    struct Stack
    {
        struct Zimu *base;//栈底
        struct Zimu *top;//栈顶
        int Size;
    };//栈类型
    
    struct Zimu* creatNode(char letter)//创建节点
    {
        struct Zimu* newNode = (struct Zimu*)malloc(sizeof(struct Zimu));
        newNode->letter = letter;
        newNode->next = NULL;
        return newNode;
    }
    
    //初始化
    int initstack(struct Stack** S)
    {
        struct Stack *T=(struct Stack*)malloc(sizeof(struct Stack));
        T->base = (struct Zimu*)malloc(sizeof(struct Zimu));
        if(!(T->base)) exit(-1);
        T->top = T->base;
        T->Size = 0;
        *S=T;
        return 0;
    }
    
    void push(struct Stack * S)//进栈
    {
        char letter;
        printf("请输入字母\n");
        scanf(" %c", &letter); // 使用 " %c" 读取字符,防止上一行结尾的换行符干扰
        struct Zimu* newnode = creatNode(letter);
        newnode->next = S->top;
        S->top = newnode;
        S->Size++;
    }
    
    int top(struct Stack* S)//获取栈顶元素
    {
        if(S->Size == 0)
        {
            printf("栈为NULL\n");
            return 0;
        }
        return S->top->letter;
    }
    
    void pop(struct Stack* S)//出栈
    {
        if(S->Size == 0)
        {
            printf("栈为NULL\n");
        }
        else
        {
            struct Zimu* nextnode = S->top->next;
            free(S->top);
            S->top = nextnode;
            S->Size--;
        }
    }
    
    int empty(struct Stack* S)
    {
        if(S->Size == 0)
            return 0;
        return 1;
    }
    
    char letter;
    int main()
    {
        int t;
        struct Stack* S=NULL;
        initstack(&S);
        push(S);
        printf("请选择你要进行的操作:\n");
        scanf("%d", &t);
        do
        {
            switch(t)
            {
                case 1: push(S);
                    break;
                case 2: pop(S);
                    break;
                case 3: 
                    printf("%c\n", S->top->letter); // 使用 %c 打印字符
                    pop(S);
                    break;
                default :
                    break;                
            }
        printf("请选择你要进行的操作:\n");
        scanf("%d", &t); 
        }
        while(t>=1&&t<=3);
        return 0;
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月18日
  • 已采纳回答 10月10日
  • 创建了问题 10月10日

悬赏问题

  • ¥15 Fluent,液体进入旋转区域体积分数不连续
  • ¥15 课上突考,本人大脑过载,求51单片机能手
  • ¥15 java linux下将docx文件转pdf
  • ¥15 maven无法下载依赖包
  • ¥15 关于pycharm, Callable[[str],bool]作为方法参数使用时, lambda 类型不提示问题
  • ¥15 layui数据重载无效
  • ¥15 寻找了解qq家园纵横四海的程序猿。
  • ¥15 optisystem
  • ¥15 VB.NET画图时的撤销编程
  • ¥15 C# webservice 参数特殊字符处理