前面我总结了从中间节点插入,这次总结一个从头部来插入
struct Test
{
int data;
struct Test *next;
}
struct Test *insertFromhead(struct Test *head,int data,struct Test *new)
{
if(head->data == data){//头节点
new->next = head;
return new;//直接从链表头插入
}
while(head->next!=NULL){//非头节点插入
if(head->next->data ==data){
new->next = head->next;
head->next = new;
}
head = head->next;
}
}
int main()
{
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
struct Test t4 = {4,NULL};
struct Test *head=NULL;
head=&t1;
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
struct Test new = {100,NULL};
printf("please use t1 to printf the num :\n");
p