标题:C初学者一个关于链表的问题
只看楼主
wxy_13
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-12-21
 问题点数:0 回复次数:1 
C初学者一个关于链表的问题
  

#include<stdio.h>
#include "malloc.h"

#define MaxSize 100
#define error 0;
#define right 1;

typedef int ElemType;

typedef struct Node
{
    ElemType data;
    struct Node* next;
};
typedef struct Node* LinkList;

void CreateList(LinkList* L, int n, ElemType a[]);
void InsertList(LinkList* L, int i, int n, ElemType e);
void InputList(LinkList* L, int n);

void CreateList(LinkList* L, int n, ElemType a[])
{
    LinkList p, r;
    *L = (LinkList)malloc(sizeof(Node));
    r = *L;
    for (int i = 0; i < n; i++)
    {
        p = (Node*)malloc(sizeof(Node));
        p->data = a[i];
        r->next = p;
        r = p;
    }
    r->next = NULL;
}

void InsertList(LinkList* L, int i, int n, ElemType e)
{
    LinkList p, s;
    p = (*L)->next;
    int k, j;
    for (int k = 0; k < n; k++)
    {
        if (k == i - 2)
        {
            s = (LinkList)malloc(sizeof(Node));
            s->next = p->next;
            p->next = s;
            s->data = e;
            break;
        }
        p = p->next;
    }
}

void InputList(LinkList* L, int n)
{
    LinkList p;
    p = (*L)->next;
    for (int i = 0; i < n; i++)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

int main()
{
    LinkList L;
    int n;
    scanf_s("%d", &n);
    ElemType data[MaxSize];
    for (int j = 0; j < n; j++)
    {
        scanf_s("%d", &data[j]);
    }
    CreateList(&L, n, data);
    int i;
    scanf_s("%d", &i);
    if (i > n || i < 1)
        printf("error!\n");
    int m;
    scanf_s("%d", &m);
    InsertList(&L, i, n, m);
    InputList(&L, n + 1);
    return 0;
}

有大佬帮忙看看为什么这段代码在OJ上提交会显示Runtime error吗?

搜索更多相关主题的帖子: void int Node data next 
2019-12-21 20:27
WilsonLA
Rank: 1
等 级:新手上路
威 望:1
帖 子:9
专家分:0
注 册:2020-1-20
得分:0 
*L = (LinkList)malloc(sizeof(Node));

压根就没有Node型,应该改成
*L = (LinkList)malloc(sizeof(struct Node));

别的地方还没看,还有你的习惯很不好,都不回收内存的吗?另外一个传入形参是指针,你应该判断是不是空指针,来避免一些错误,方便debug
2020-01-20 23:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-498537-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.322443 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved