数据结构Day4作业

实现链表,按值查找返回位置的功能,按位置查找返回值,释放单链表,链表逆置

//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;
}

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值