用c语言统计俩报表节点个数及链表的查找
#include<stdio.h>
struct Test
{
int data;
struct Test *next;
};
void printLine(struct Test *head)
{
struct Test *point;
point = head;
while(point != NULL)
{
printf("%d ",point->data);
point = point->next;
}
putchar('\n');
}
int printnum(struct Test *head)
{
int sum;
while(head != NULL)
{
head = head -> next;
sum ++;
}
return sum;
}
int searchnum(struct Test *head,int data)
{
while(head != NULL){
if(head->data == data)
{
return 1;
}
head = head -> next;
}
return 0;
}
int main()
{
int arry[]={1,2,3};
int i;
for(i=0;i<sizeof(arry)/sizeof(arry[0]);i++)
{
printf("%d ",arry[i]);
}
putchar('\n');
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;
printLine(&t1);
//printf("%d,%d,%d",t1.data,t1.next->data,t1.next->next->data);
int ret = printnum(&t1);
printf("total number:%d\n",ret);
ret = searchnum(&t1,1);
if(ret == 0)
{
printf("no 1\n");
}else
{
printf(" have 1\n");
}
ret = searchnum(&t1,8);
if(ret == 1)
{
printf("no 8\n");
}else
{
printf("have 8\n");
}
return 0;
}
运行结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/12b88668b69e1831f34554164dcb22f2.png#pic_center)