双链表以及链表循环


//初始化双链表
typedef struct DNode{
	int data;
	struct DNode *Prior,*next;
}DNode,*DLinklist;
bool InitDLinklist(DLinklist &L){
	L=(DNode *)malloc(sizeof(DNode));  //分配一个结点,用DNode时强调这是一个结点
	if(L==NULL){
		return false;   //分配空间失败
	}
	L->Prior=NULL;  //头结点的前驱结点为NULL
	L->next=NULL;
	return true;
}
//判断双链表是否为空
bool Empty(DLinklist L){
	if(L->next==NULL){
		return true;
	}
	return false;
}
//插入,在p节点后插入s结点
bool InsertNextDNode(DNode *p,DNode *s){
	if(p==NULL || s==NULL){
		return false;
	}
	s->next=p->next;   //将p的后继结点作为s的后继结点
	if(p->next!=NULL){   //如果p的后继结点为空
		p->next->Prior=s;
	}
	//p->next->Prior=s;   //让p后继节点的前驱结点指向s
	s->Prior=p;     //让s的前驱结点指向p
	p->next=s;     //让p的后继结点指向s
}

//删除p结点的后继结点
bool DeleteNextDNode(DNode *p){
	if(p==NULL){
		return false;
	}
	DNode *q = p->next;
	if(q==NULL){
		return false;
	}
	p->next=q->next;
	if(q->next!=NULL){
		q->next->Prior=p;
	}
	free(q);
	return true;
}
//调用删除p结点的后继结点函数,释放各个数据节点
void DestoryList(DLinklist &L){
	while(L->next!=NULL){
		DeleteNextDNode(L);
	}
	free(L);    //释放头指针
	L=NULL;     //头指针指向空
}
int main(){
	DLinklist L;          //用DLinklist是强调这是一个链表        DLinklist与DNode*是等价的
	InitDLinklist(L);
}

//循环单链表
typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList;
//初始化一个循环单链表
bool InitList(LinkList &L){
	L=(LNode *)malloc(sizeof(LNode));
	if(L==NULL){
		return false;        //内存分配失败
	}
	L->next=L;      //头结点next指向头结点
	return true;
}
//判断循环单链表是否为空
bool Empty(LinkList L){
	if(L->next==L){
		return true;
	}else{
		return false;
	}
}
//判断结点p是否为循环单链表的表尾结点
bool isTail(LinkList L,LNode *p){
	if(p->next==L){
		return true;
	}
	else{
		return false;
	}
}
typedef struct DNode{
	int data;
	struct DNode *prior,*next;
}DNode,*DLinkList;
//初始化一个空的循环双链表
bool InitDLinkList(DLinkList &L){
	L=(DNode *)malloc(sizeof(DNode));
	if(L==NULL){
		return false;
	}
	L->next=L;
	L->prior=L;
	return true;
}
//判断循环双链表是否为空
bool Empty(DLinkList L){
	if(L->next==L){
		return true;
	}else{
		return false;
	}
}
//判断双链表结点p是否为尾部节点
bool isTail(DLinkList L,DNode *p){
	if(p->next==L){
		return true;
	}
	else{
		return false;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值