开发者社区> 问答> 正文

链表一遇到OutPut函数程序就停止运行 数据结构问题

初学《数据结构与算法》链表,不知道代码哪里有问题,代码编译和运行都没有问题,但是一遇到OutPut函数程序就停止运行了,选取了一部分代码,求大神解决。

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef int Status;
typedef struct lnode
{
    ElemType data;  
    struct lnode*next;  
}LNode; 

void CreateList(LNode*L,int n)  
{
    int i;
    LNode*p;
    L=malloc(sizeof(LNode));  
    L->next=NULL;           
    printf("请连续输入%d个元素:",n);
    for(i=n;i>0;i--)
    {
        p=malloc(sizeof(LNode));  
        scanf("%d",&p->data);     
        p->next=L->next;          
        L->next=p;
    }
}//逆位序建表 

void OutPut(LNode*L)
{
    LNode *p=L->next;
    if(L->next==NULL)
    printf("该链表为空!");
    else
    {
        while(p)
        {
            printf("%d\t",p->data);
            p=p->next;
        }
    }
}

void main()
{
    LNode la;
    int length; 
    printf("请输入链表la长度:");
    scanf("%d",&length);
    CreateList(&la,length);
    printf("该链表的元素有:");
    OutPut(&la);
} 

image.png

展开
收起
海边一只船 2020-05-27 15:57:35 840 0
1 条回答
写回答
取消 提交回答
  • void CreateList(LNode*L,int n)
    
    这个要用双指针,否则函数内改变L不会作用在调用者上
    
    
    2020-05-27 17:52:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何使用Tair增强数据结构构建丰富在线实时场景 立即下载
Apache Flink 流式应用中状态的数据结构定义升级 立即下载
低代码开发师(初级)实战教程 立即下载