实现链表,按值查找返回位置的功能,按位置查找返回值,释放单链表,链表逆置
//15.单链表按元素查找
int search_value(node_p H,datatyped value){
if(NULL==H || 0==H->len){
return -1;
}
int i=0;
node_p p=H;
while(p!=NULL){
if(p->data==value){
return i;
}
i++;
p=p->next;
}
return -1;
}
//12.单链表按位置查找
datatyped search_pos(node_p H,int pos){
if(NULL==H || 0==H->len){
return -1;
}
if(pos<0 || pos>H->len){
return -1;
}
node_p p=H;
for(int i=0;i<pos;i++){
p=p->next;
}
return p->data;
}
//17.单链表的空间释放
node_p free_space(node_p H){
if(NULL==H){
return NULL;
}
node_p p =H->next;
node_p t;
while(p!=NULL){
t=p;
p=p->next;
free(t);
}
t=NULL;
free(H);
H=NULL;
return H;
}
//16.单链表逆置
void rev_linklist(node_p H){
if(NULL==H || 0==H->len){
return;
}
node_p p=H->next;
H->next=NULL;
node_p t;
while(p!=NULL){
t=p;
p=p->next;
t->next=H->next;
H->next=t;
}
return;
}