统计链表节点个数和链表查找
Posted 橙子果果
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了统计链表节点个数和链表查找相关的知识,希望对你有一定的参考价值。
统计链表节点个数和链表查找
先定义一个链表的结构体。
struct Test
{
int data;
struct Test *next;
};
静态创建一个链表
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};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
struct Test *head;
head=&t1;
链表节点统计
int getLinkTotalNodeNum(struct Test *head)
{
int cnt = 0;
struct Test *p = head;
while(p != NULL){
cnt++;
p = p->next;
}
return cnt;
}
int ret = getLinkTotalNodeNum(&t1);
printf("total num = %d\\n",ret);
链表查找
int searchLink(struct Test *head,int data)
{
while(head != NULL){
if(head->data == data){
return 1;
}
head = head->next;
}
return 0;
}
int set = searchLink(&t1,1);
if(set == 0){
printf("no 1\\n");
}else{
printf("have 1\\n");
}
set = searchLink(&t1,19);
if(set == 0){
printf("no 19\\n");
}else{
printf("have 19\\n");
}
return 0;
最后输出结果
以上是关于统计链表节点个数和链表查找的主要内容,如果未能解决你的问题,请参考以下文章