#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
LNode *create(int n)
{
LNode *L=(LNode*)malloc(sizeof(LNode));
LNode *s=NULL,*p=L;
int i,j;
for(i=0;i<n;i++)
{
scanf("%d",&j);
s=(LNode*)malloc(sizeof(LNode));
s->data=j;
s->next=NULL;
p->next=s;
p=p->next;
}
return L;
}
void dele(LNode *L,int x)
{
LNode *p;
if(L->next==NULL)
return;
else
{
dele(L->next,x);
if(L->next->data==x)
{
p=L->next;
L->next=p->next;
free(p);
}
//dele(L->next,x);
}
}
void print(LNode *L)
{
LNode *p=L->next;
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->next;
}
}
int main()
{
LNode *L=create(10);
print(L);
dele(L,5);
print(L);
system("pause");
return 0;
}
拓展——递归删除单链表中所有值为x的结点
最新推荐文章于 2024-02-03 17:48:34 发布