线性表链式存储的查找元素

线性表链式存储的查找元素

问题描述:

img


如图 在线性表的链式存储中 查找第n个元素
图中圈出来的地方 count > n 这个条件 是不是多余啊 通过上面的while循环后 count 最多 = n
什么时候会出现 count > n 呢

                                              口10:27
       HD2
CsDn 登录
                 C语言常用字
              1f(p->data e
                     return 1;
              p=p->next;
        return 0;
 /*获取元素,n表示第几个,并返回查到的值*
 intGetElem(LinkListL,intn)
        LinkListp; //生成节点p,存放Nodei
       p=L->next; //p指向第一个元素
        intcount=1; //计数器
        while(p&&count<n){//查找
              p=p->next;
              count+
        if(!p count>n)t/ 没找到
              return 0;
       int ret //找到
        return ret
 /*判断链表是否为空
 intListElmp APP内打开
                抢沙发
                                            山
  2写评论
                              评论 点赞 3 分享

这只是以防万一的写法,也不会有count > n的出现,只是防止一些没法预想到的情况或者有时候程序写得不那么好会导致不应该发生的结果