
上QQ阅读APP看书,第一时间看更新
1.3.5 获得节点元素值
函数Get()用于获得链表中第i个位置的节点元素值,参考代码如下。
1 int Get(Link Head,int i) //获得第i个位置的节点元素值
2 {
3 int j=1; //定义j为计数器
4 Link p=Head;
5 while(j<i && p!=NULL) //直到找到第i个位置的节点元素值
6 {
7 p=p->next;
8 j++;
9 }
10 if(p!=NULL)
11 return(p->data);
12 else
13 printf("输入数据错误!");
14 return -1;
15 }