世界语# 2021-10-30 23:44 采纳率: 100%
浏览 36
已结题

这个是c语言数据结构中栈的代码,代码的语法没错,但运行出来的结果为空,什么都没有?有谁可以解决下!

这段代码是在dev c++上写的,可以运行,但没有结果!!

img

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct stack
{
    struct node *top;
    struct node *bottom;
};
void print(struct stack *s1)//打印栈 
{
    struct node *p;
    p=s1->top;
    printf("此时栈内的元素:\n");
    while(p!=NULL)
    {
        printf("%5d",p->data);
        p=p->next;
    }
    printf("\n");
}
struct stack *create(struct stack *s1)//创建栈 
{
    s1=(struct stack *)malloc(sizeof(struct stack));
    struct node *p;
    if(s1==NULL)
    {
        printf("分配内存失败!");
    }
    else
    {
        s1->top=s1->bottom=NULL;
        int a[7]={67,3,88,6,1,7,0};
        int i;
        for(i=0;i<7;i++)
        {
            p=(struct node *)malloc(sizeof(struct node));
            p->data=a[i];
            p->next=NULL;
            if(s1->top=NULL)
            {
                s1->top=s1->bottom=p;
            }
            else
            {
                s1->bottom->next=p;
                s1->bottom=p;
            }
        }
    }
    return s1;
}
struct stack *push(struct stack *s1)//插入元素 
{
    printf("元素-9入栈后的结果:\n");
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node));
    p->data=-9;
    p->next=NULL;
    p->next=s1->top; 
    s1->top=p; 
    return s1;
}
struct stack *pop(struct stack *s1)//删除元素 
{
    struct node *p;
    p=s1->top;
    if(p==NULL)
        printf("出栈失败!");
    else
        s1->top=p->next;
    return s1;
}
void length(struct stack *s1)//求栈的长度 
{
    int n=0;
    struct node *p;
    p=s1->top;
    while(p!=NULL)
    {
        n++;
        p=p->next;
    }
    printf("此时栈内元素为%d个!",n);
}
int main()
{
    int j=1;
    struct stack *s1=NULL;
    s1=create(s1);
    create(s1);
    print(s1);
    push(s1);
    print(s1);
    printf("栈顶三个元素出栈后:\n");
    while(j<4)
    {
        pop(s1);
        j++;
     }
    print(s1);
    length(s1);
    return 0;
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2021-10-31 11:37
    关注

    修改见注释处,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
        int data;
        struct node *next;
    };
    struct stack
    {
        struct node *top;
        struct node *bottom;
    };
    void print(struct stack *s1)//打印栈
    {
        struct node *p;
        p=s1->top;
        printf("此时栈内的元素:\n");
        while(p!=NULL)
        {
            printf("%5d",p->data);
            p=p->next;
        }
        printf("\n");
    }
    struct stack *create(struct stack *s1)//创建栈
    {
        s1=(struct stack *)malloc(sizeof(struct stack));
        struct node *p;
        if(s1==NULL)
        {
            printf("分配内存失败!");
        }
        else
        {
            s1->top=s1->bottom=NULL;
            int a[7]={67,3,88,6,1,7,0};
            int i;
            for(i=0;i<7;i++)
            {
                p=(struct node *)malloc(sizeof(struct node));
                p->data=a[i];
                p->next=NULL;
                if(s1->top==NULL)//if(s1->top=NULL) 修改
                {
                    s1->top=s1->bottom=p;
                }
                else
                {
                    s1->bottom->next=p;
                    s1->bottom=p;
                }
            }
        }
        return s1;
    }
    struct stack *push(struct stack *s1)//插入元素
    {
        printf("元素-9入栈后的结果:\n");
        struct node *p;
        p=(struct node *)malloc(sizeof(struct node));
        p->data=-9;
        p->next=NULL;
        p->next=s1->top; 
        s1->top=p;
        return s1;
    }
    struct stack *pop(struct stack *s1)//删除元素 
    {
        struct node *p;
        p=s1->top;
        if(p==NULL)
            printf("出栈失败!");
        else
            s1->top=p->next;
        return s1;
    }
    void length(struct stack *s1)//求栈的长度 
    {
        int n=0;
        struct node *p;
        p=s1->top;
        while(p!=NULL)
        {
            n++;
            p=p->next;
        }
        printf("此时栈内元素为%d个!",n);
    }
    int main()
    {
        int j=1;
        struct stack *s1=NULL;
        s1=create(s1);
        //create(s1);  //修改
        print(s1);
    
        s1=push(s1); //push(s1); 修改
        print(s1);
    
        printf("栈顶三个元素出栈后:\n");
        while(j<4)
        {
            s1=pop(s1);// pop(s1); 修改
            j++;
        }
        print(s1);
        
        length(s1);
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月8日
  • 已采纳回答 10月31日
  • 修改了问题 10月31日
  • 创建了问题 10月30日

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?