7

您所在的位置:网站首页 按值查找单链表返回的p 7

7

2024-07-11 07:46| 来源: 网络整理| 查看: 265

有一字符型单链表List,假设表中无重复值,现要按值查找,查找成功时返回指定值所在的位置,否则输出“not found”。

输入格式:

有多组数据,每组数据占两行,代表一次按值查找操作。 每组第一行有两个数,第一个为表长n(0next=NULL; r->next=p; r=p; } } void DestrpyList_L(LinkList &L){ LinkList p; while(L){ p=L; L=L->next; free(p); } } int LocateElem(LinkList L,char e){ int i; LinkList p; p=L; while(p && p->data!=e){ p=p->next; i++; } if(p){ return i; } else{ return 0; } } int main(){ int n=0,j=0; char m='0'; while(~scanf("%d %c",&n,&m)){ LinkList L; getchar(); Creat_L(L,n); j=LocateElem(L,m); if(j){ printf("%d\n",j); } else{ printf("not found\n"); } DestrpyList_L(L); } return 0; }

给个三连吧🤭

欢迎交流



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3