双向循环链表:
就是在单链表中,有两个指针域,一个指针指向前驱,一个指针指向后继。
#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;
}
初始化版本完成,后续继续完善,且行且珍惜…
继续完善…