数据结构--双链表的基本操作

#include<stdio.h>
#include<stdlib.h>
typedef struct dnode{
	int data;
	struct dnode *prior,*next;
}DNode;
int length(DNode *Dl){
	int length = 0;
	while(Dl->next != NULL){
		Dl = Dl->next;
		length++;
	}
	return length;
}
bool lengthOver(DNode *Dlist,int flag){
	int len = length(Dlist);
	if(flag > len){
		printf("请删除正确的节点!");
		return false;
	}else{
		return true;
	}
	
}
bool InitDlist(DNode* &Dl){
	Dl = (DNode*)malloc(sizeof(DNode));
	if(Dl == NULL){
		return false;
	}
	Dl->next = NULL;
	Dl->prior = NULL;
	return true;
}

void InsertDlist(DNode *Dl,int data){
	DNode *p;
	InitDlist(p);
	p->data = data;
	p->next = Dl->next;
	if( Dl->next != NULL ){
		Dl->next->prior = p;
	}
	p->prior = Dl;
	Dl->next = p;
}
bool DelDlist(DNode *Dl,int flag){
	DNode *p;
	InitDlist(p);
	if(!lengthOver(Dl,flag)){
		return false;
	}
	for(int i = 0;i<flag-1;i++){
			Dl = Dl->next;
	}
	p = Dl->next;
	Dl->next = p->next;
	p->next->prior = Dl;
	free(p);
	return true;
	

} 
bool UpdateDlist(DNode *Dlist,int data,int flag){
	if(!lengthOver(Dlist,flag)){
		return false;
	}
	for(int i = 0; i < flag;i++ ){
		Dlist = Dlist->next;
	}
	Dlist->data = data;
	return true;
}
int main(){
	DNode *Dlist;
	InitDlist(Dlist);
	InsertDlist(Dlist,1);
	InsertDlist(Dlist,2);
	InsertDlist(Dlist,3);
	InsertDlist(Dlist,4);
	DelDlist(Dlist,2);
	UpdateDlist(Dlist,99,2);
	while(Dlist->next != NULL){
		Dlist = Dlist->next;
		printf("%d",Dlist->data);
	}
	printf("\n");
	while(Dlist->prior != NULL){
		printf("%d",Dlist->data);
		Dlist = Dlist->prior;
	}
	
} 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值