双向循环链表

双向循环链表:
就是在单链表中,有两个指针域,一个指针指向前驱,一个指针指向后继。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
typedef struct s_doublelink{
	int data;
	struct s_doublelink *pfront;
	struct s_doublelink *pNext;
}SDOUBLELINK,*PSDOUBLELINK;
PSDOUBLELINK DoubleLinkInit(void){
	SDOUBLELINK *LinkHeader;
	LinkHeader = (SDOUBLELINK *)malloc(sizeof(struct s_doublelink));
	if(NULL == LinkHeader){
		printf("init double link header fail\n");
		return NULL;
	}
	LinkHeader->pfront = LinkHeader;
	LinkHeader->pNext = LinkHeader;
	LinkHeader->data = 0;  //init
	return LinkHeader;
}
int DoubleLinkInsert(PSDOUBLELINK pHeader,int nval){
	PSDOUBLELINK pNode = NULL;
	PSDOUBLELINK pTemp = NULL;
	if(NULL == pHeader){
		printf("header node is NULL\n");
		return -1;
	}
	pTemp = pHeader;
	pNode = (struct s_doublelink *)malloc(sizeof(struct s_doublelink));
	if(NULL == pNode){
		printf("create temp node fail\n");
		return -1;
	}
	pNode->data = nval;
	pNode->pfront = pNode;
	pNode->pNext = pNode;
	while(pHeader->pNext != pTemp){
		pHeader = pHeader->pNext;
	}
	pNode->pNext = pTemp;
	pNode->pfront = pHeader;
	pTemp->pfront = pNode;
	pHeader->pNext = pNode;
	return 0;
}
int DoubleLinkPrint(PSDOUBLELINK pHeader){
	PSDOUBLELINK pTemp = NULL;
	if(NULL == pHeader){
		printf("the init pointer is NULL\n");
		return -1;
	}
	pTemp = pHeader;
	while(pHeader->pNext!= pTemp){
		pHeader = pHeader->pNext;
		printf("%d ",pHeader->data);
	}
	putchar(10);
	printf("%d\n",pHeader->data);
	while(pHeader->pfront != pTemp){
		printf("%d ",pHeader->data);
		pHeader = pHeader->pfront;
	}
	printf("%d ",pHeader->data);
	putchar(10);
	return 0;
}
int DoubleLinkDel(PSDOUBLELINK pHeader,int nVal)
{
	PSDOUBLELINK pTemp = NULL;
	PSDOUBLELINK pNode = NULL;
	int flag = 0;
	if(NULL == pHeader){
		printf("init pointer is NULL\n");
		return -1;
	}
	pTemp = pHeader;
	while(pHeader->pNext != pTemp){
		if(pHeader->data == nVal){
			pNode = pHeader;
			flag = 1;
			break;
		}
		pHeader = pHeader->pNext;
	}
	if(flag){
		pNode->pfront->pNext = pNode->pNext;
		pNode->pNext->pfront = pNode->pfront;
		free(pNode);
		pNode = NULL;
		printf("done\n");
	}else{
		printf("nval not exsit\n");
	}
	return 0;
}	
PSDOUBLELINK CutHeaderDoubleLoopLink(PSDOUBLELINK pHeader){
	PSDOUBLELINK pTemp = NULL;
	if(NULL == pHeader){
		printf("init pointer is NULL\n");
		return NULL;
	}	
	return pTemp;
}
int main(int argc,char argv[]){
	PSDOUBLELINK pInit;
	pInit = DoubleLinkInit();
	if(NULL == pInit){
		printf("init double link fail\n");
		return -1;
	}
	DoubleLinkInsert(pInit,10);
	DoubleLinkInsert(pInit,20);
	DoubleLinkInsert(pInit,30);
	DoubleLinkInsert(pInit,40);
	DoubleLinkPrint(pInit);	
	DoubleLinkDel(pInit, 30);
	DoubleLinkPrint(pInit);
	return 0;
}

初始化版本完成,后续继续完善,且行且珍惜…
继续完善…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值