kunkundebasketba 2022-09-18 16:32 采纳率: 64.3%
浏览 25
已结题

设计一个递归算法,删除不带头节点的单链表L中所有值为x的结点,我的代码在Linklist creatlist(L,a,4)这一行报错,说设定初始值太多

#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*Linklist;

Linklist creatlist(Linklist L,int a[],int n)
{
LNode *s,*r;
L=(LNode *)malloc(sizeof(LNode));
L->data=a[0];
r=L;
if(n==1)
L->next=NULL;
for(int i=1;i<n;i++)
{
s=(LNode *)malloc(sizeof(LNode));
s->data=a[i];
r->next=s;
r=r->next;
}
return L;
}
void deleteLNode(Linklist L,int x)
{
LNode *p;
if(L==NULL)
return;
if(L->data==x)
{
p=L;
L=L->next;
free(p);
deleteLNode(L->next,x);
}
else
deleteLNode(L->next,x);
}

int main()
{
Linklist L=NULL;
int a[]={1,2,2,4};
int n=4;
Linklist creatlist(L,a,4);
deleteLNode(L,2);
while(L)
{
printf("%d ",L->data);
L=L->next;
}
return 0;
}

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-09-18 16:37
    关注

    main函数中,
    Linklist creatlist(L,a,4);
    改为
    L = creatlist(L,a,4);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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