解题思路:先创建链表,再删除,比较简单
注意事项:指针
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct List{
int num;
struct List* next;
}List;
//创建链表
List* creat_List(int n)
{
List* head=malloc(sizeof(List));
head->num=-1;
head->next=NULL;
int i; List* tmp=head;
for(i=0;i<n;i++)
{
List* newptr=malloc(sizeof(List));
scanf("%d",&newptr->num);
newptr->next=NULL;
tmp->next=newptr;
tmp=newptr;
}
tmp->next=NULL;
return head;
}
//删除链表节点
List* delete_List(List* head,int m)
{
List *pre,*p;
int j=0;
p=head->next;
while(p->next)
{
if(p->num!=m)
{
while(p->num!=m&&p->next)
{
pre=p;
p=p->next;
}
}
if(p->num==m)
{
pre->next=p->next;
free(p);j++;
}
p=pre->next;
}
return head;
}
int main()
{
int n;
scanf("%d",&n);
List* head=creat_List(n);
List* tmp=head;
int m,i,j=0;
scanf("%d",&m);
List* tmp2=delete_List(tmp,m);
tmp2=tmp2->next;
while(tmp2)
{
printf("%d ",tmp2->num);
tmp2=tmp2->next;
}
return 0;
}
0.0分
6 人评分
C语言程序设计教程(第三版)课后习题8.2 (Java代码)浏览:2269 |
【数组的距离】 (C语言代码)浏览:728 |
【亲和数】 (C语言代码)浏览:501 |
【蟠桃记】 (C语言代码)浏览:1021 |
1017题解浏览:572 |
C语言训练-大、小写问题 (C语言代码)浏览:674 |
C语言程序设计教程(第三版)课后习题7.5 (C语言代码)浏览:673 |
买不到的数目 (C语言代码)浏览:3090 |
Tom数 (Java代码)浏览:587 |
1227题解浏览:525 |