例如在3的后方插入100
#include <stdio.h>
struct Test
{
int data;
struct Test *next;
};
int insertFromBehind(struct Test *head,int data,struct Test *new)
{
struct Test *p=head;
while(p!=NULL)
{
if(p->data==data)
{
new->next=p->next;
p->next=new;
return 1;
}
p=p->next;
}
return 0;
}
void printlink(struct Test *head)
{
struct Test *point;
point=head;
while(point != NULL)
{
printf("%d ",point->data);
point=point->next;
}
putchar('\n');
}
int main()
{
struct Test t1={1,NULL};
struct Test t2={2,NULL};
struct Test t3={3,NULL};
struct Test t4={4,NULL};
t1.next=&t2;
t2.next=&t3;
t3.next=&t4;
struct Test new={100,NULL};
printf("use t1 to print three nums\n");
printlink(&t1);
puts("after insert behind:\n");
insertFromBehind(&t1,3,&new);
printlink(&t1);
return 0;
}
运行结果
——@上官可编程