提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
`提示:单链表递归算法删除不带头结点的特定值
提示:以下是本篇文章正文内容,下面案例可供参考
一、使用步骤
设计一递归算法,删除不带头结点的单链表L中所有值为X的结点
代码如下(示例):
#include<stdio.h>
#include<windows.h>
#include<iostream>
struct LNode{
int data;
struct LNode *next;
};
struct LNode*Del_x(struct LNode*list,int x)
{
struct LNode *p;
if(list==NULL)
{
return NULL;
}
if(list->data==x)
{
p=list;
list=list-next;
free(p);
return Del_x(list,x);
}else
{
list->next=Del_x(list,x);
return list;
}
};
void print_Elect(struct LNode *list)
{
while(list!=NULL)
{
printf("%d ",list->data);
list=list->next;
}
}
int main(void)
{
SetConsoleOutputCP(65001);
struct LNode *first,*temp,*tail;
struct LNode *last;
int x=0;
int flag=0;
printf("请输入个数: \n");
scanf("%d%d",&flag,&x);
first=(struct LNode*)malloc(sizeof(struct LNode));
tail=(struct LNode*)malloc(sizeof(struct LNode));
for(int i=1;i<=flag;i++)
{
temp=(struct LNode*)malloc(sizeof(struct LNode));
// printf("请输入: \n");
scanf("%d",&temp->data);
temp->next=NULL;
if(i==1)
{
first=temp;
tail=temp;
}else{
tail->next=temp;
tail=temp;
}
}
last=Del_x(first,x);
print_Elect(last);
}
2.读入数据
代码如下(示例):
无法出现想要 的结果 会直接结束程序
总结
这是为什么会出不来结果???