1.插入思想:
1------2-------3------4------5
new
假如插入4后面:
point = head;
new ->next = point ->next;
point ->next = new;
#include<stdio.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *point;
point = head;
printf("use Link to print num:\n");
while(point !=NULL){
printf("%d ",point->data);
point = point->next;
}
putchar('\n');
}
int getLinkTotalnums(struct Test *head)
{
int couwhile(head != NULL){
count++;
head = head ->next;
}
return count;
}
int insertFrombehind(struct Test *head,int data,struct Test *new)
{
struct Test *point;
point = head;
while(point !=NULL){
if(point->data == data){
new ->next = point ->next;
point ->next = new;
return 1;
}
point = point ->next;
}
return 0;
}
int main()
{
int i;
int arr[] = {1,2,3,4,5,6,7,8,9,10};
for(i =0 ;i<sizeof(arr)/sizeof(arr[0]);i++){
printf("%d ",arr[i]);
}
putchar('\n');
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
struct Test t4 = {4,NULL};
struct Test t5 = {5,NULL};
struct Test new = {100,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
printLink(&t1);
puts("after insert behind:\n");
insertFrombehind(&t1,4,&new);
printLink(&t1);
return 0;
}