#include<stdio.h>
#include<stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test* head)
{
struct Test *point;
point=head;
while(point != NULL){
printf("%d ",point->data);
point=point->next;
}
putchar('\n');
}
struct Test* deletNode(struct Test *head,int data)/*删除链表*/
{
struct Test *p=head;
if(p->data==data){
head=head->next;
free(p);
return head;
}
while(p->next!=NULL){
if(p->next->data==data){
p->next=p->next->next;
return head;
}
p=p->next;
}
return head;
}
int main()
{
struct Test *head=NULL;
struct Test *p=(struct Test*)malloc(sizeof(struct Test));
struct Test t2={2,NULL};
struct Test t3={3,NULL};
struct Test t4={4,NULL};
struct Test t5={5,NULL};
p->data=1;
p->next=&t2;
t2.next=&t3;
t3.next=&t4;
t4.next=&t5;
head=p;
printLink(head);
head=deletNode(head,2);
printLink(head);
return 0;
}
链表删除指定节点
最新推荐文章于 2024-06-29 01:13:46 发布