有1,2,3,4,5节点; 1->2->3->4->5, 支持指点删除任何一个节点
#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
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){
printf("%d,data=%d\n",p->next->data,data);
p->next = p->next->next;
return head;
}
p = p->next;
}
return head;
}
int main()
{
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, 1);
printLink(head);
}