sp; i++;
if(i == pos){
break;
}
hl = hl- >next;
}
if(hl != NULL){
return hl- >data;
}else{
printf( "pos值非法,退出运行! ");
exit(1);
}
}
/* 6.遍历一个单链表 */
void traverseList(struct sNode *hl)
{
while(hl != NULL){
printf( "%5d", hl->data);
hl = hl- >next;
}
printf( " ");
return;
}
/* 7.从单链表中查找具有给定值x的第一个元素,若查找成功则返回该结点data域的存储地址,否则返回NULL */
elemType* findList(struct sNode *hl, elemType x)
{
while(hl != NULL){
if(hl- >data == x){
return &hl->data;
}else{
hl = hl- >next;
}
}
return NULL;
}
/* 8.把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0 */
int updatePosList(struct sNode *hl, int pos, elemType x)
{
int i = 0;
struct sNode *p = hl;
while(p != NULL){ /* 查找第pos个结点 */
i++;
if(pos == i){
break;
}else{
p = p- >next;
}
}
if(pos == i){
p- >data = x;
return 1;
}else{
return 0;
}