史天亮123 2022-09-30 20:44 采纳率: 75%
浏览 29
已结题

单链表的创建和输出,输入后不执行output函数

#include <stdio.h>#include <stdlib.h>/* run this program using the console pauser or add your own getch, system("pause") or input loop */typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode *next;}LNode,*LinkList;void InitList_L(LinkList L){ L=(LinkList)malloc(sizeof(LNode)); L->next=NULL;}void CreateList_H(LinkList L,int n){ L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; LinkList p=L; int i; printf("倒序输入数据:"); for(i=0;i<n;++i){ p=(LinkList)malloc(sizeof(LNode)); scanf("%d",&p->data); p->next=L->next; L->next=p; }}void OutputData(LinkList L){ if(L->next==NULL){ printf("链表为空"); } printf("ok"); LinkList p=L->next; while(p!=NULL){ printf("%d",L->data); p=p->next; }}int main(int argc, char *argv[]) { LinkList l; CreateList_H(l,2); OutputData(l); return 0;}

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-09-30 21:45
    关注

    修改如下,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    // run this program using the console pauser or add your own getch, system("pause") or input loop
    typedef int ElemType;
    typedef struct LNode{
        ElemType data;
        struct LNode *next;
    }LNode,*LinkList;
    
    void InitList_L(LinkList* L)  //(LinkList L)
    {
        (*L)=(LinkList)malloc(sizeof(LNode)); //修改
        (*L)->next=NULL;                      //修改
    }
    void CreateList_H(LinkList L,int n)
    {
        //L=(LinkList)malloc(sizeof(LNode)); 修改
        //L->next=NULL;                      修改
        LinkList p=L;
        int i;
        printf("倒序输入数据:");
        for(i=0;i<n;++i)
        {
            p=(LinkList)malloc(sizeof(LNode));
            scanf("%d",&p->data);
            p->next=L->next;
            L->next=p;
        }
    }
    void OutputData(LinkList L)
    {
        if(L->next==NULL){
            printf("链表为空");
            return;       //修改
        }
        printf("ok");
        LinkList p=L->next;
        while(p!=NULL){
            printf("%d ",p->data); //("%d ",L->data); 修改
            p=p->next;
        }
    }
    int main(int argc, char *argv[])
    {
        LinkList l;
        InitList_L(&l); //修改
        CreateList_H(l,2);
        OutputData(l);
    
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题