#include <stdio.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');
}
int getNodesum(struct Test *head)
{
int count = 0;
while(head != NULL){
count++;
head = head->next;
}
return count;
}
int searchNode(struct Test *head,int data)
{
while(head !=NULL ){
if(head->data == data){
return 1;
}else{
head = head -> next;
}
}
return 0;
}
int insertNodebehind(struct Test *head,int data,struct Test *new)
{
struct Test *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()
{
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 t6 = {6,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
t5.next = &t6;
struct Test new={100,NULL};
printlink(&t1);
int ret;
ret=getNodesum(&t1);
printf("链表的节点个数=%d\n",ret);
int search;
search = searchNode(&t1,1);
if(search == 0){
printf("no 1 in Node\n");
}else{
printf("have 1 in Node\n");
}
printf("插入新节点到指定节点的后面:\n");
int info;
info=insertNodebehind(&t1,6,&new);
if(info == 0){
printf("插入失败\n");
}else{
printf("插入成功\n");
}
printlink(&t1);
return 0;
}