CircularList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct DListNode
{
DataType data;
struct DListNode *next;
struct DListNode *previous;
}DListNode;
DListNode *BuyListNode(DataType data);
void DListInit(DListNode **pHead);
void DListPushBack(DListNode *pHead,DataType data);
void DListPopBack(DListNode *pHead);
void DListPushFront(DListNode *pHead, DataType data);
void DListPopFront(DListNode *pHead);
void DListInsert(DListNode *pos,DataType data);
void DListErase(DListNode *pos);
DListNode *DListFind(DListNode *pHead, DataType data);
void DListDestroy(DListNode *pHead);
#endif //_LINKLIST_H_
linklist.c
#include"CircularList.h"
//创建一个节点
DListNode *BuyListNode(DataType data)
{
DListNode *ret = (DListNode *)malloc(sizeof(DListNode));
if(NULL == ret)
{
assert(0);
}
else
{
ret->data = data;
ret->next = NULL;
ret->previous = NULL;
}
return ret;
}
//初始化
void DListInit(DListNode **pHead)
{
assert(pHead);
*pHead = BuyListNode(0);
(*pHead)->next = (*pHead);
(*pHead)->previous = *pHead;
}
//双向链表的尾插
void DListPushBack(DListNode *pHead,DataType data)
{
DListNode *cur = NULL;
//如果链表内只有头节点
if(pHead->next == pHead)
{
cur = BuyListNode(data);
pHead->next = cur;
cur->previous = pHead;
cur->next = pHead;
pHead->previous = cur;
}
//如果链表内有其他节点
else
{
cur = BuyListNode(data);
pHead->previous->next = cur;
cur->previous = pHead->previous;
pHead->previous = cur;
cur->next = pHead;
}
}
//双向链表的尾删
void DListPopBack(DListNode *pHead)
{
DListNode *cur = NULL;//用来表示要删除的节点
//如果链表内只有头节点,不用删除,直接返回
if(pHead == pHead->next)
return;
else
{
cur = pHead->previous;
pHead->previous = cur->previous;
cur->previous->next = pHead;
free(cur);
cur = NULL;
}
}
//头插
void DListPushFront(DListNode *pHead, DataType data)
{
DListNode *cur = BuyListNode(data);
//当链表只有头节点时
if(pHead->next == pHead)
{
pHead->next = cur;
cur->next = pHead;
cur->previous = pHead;
pHead->previous = cur;
}
else
{
cur->previous = pHead;
cur->next = pHead->next;
pHead->next ->previous = cur;
pHead->next = cur;
}
}
//双向链表的头删
void DListPopFront(DListNode *pHead)
{
DListNode *cur = pHead->next;
if(pHead->next == pHead)
return;
else
{
cur->next->previous = pHead;
pHead->next = cur->next;
free(cur);
cur = NULL;
}
}
//在任意位置插入
void DListInsert(DListNode *pos,DataType data)
{
DListNode *cur = BuyListNode(data);
if(NULL == pos)
return;
//在指定位置之后插入一个节点
else
{
cur->next = pos->next;
cur->previous = pos;
pos->next = cur;
cur->next->previous = cur;
}
}
//在指定位置删除一个节点
void DListErase(DListNode *pos)
{
if(pos == NULL)
return ;
else
{
pos->previous->next = pos->next;
pos->next->previous = pos->previous;
free(pos);
pos = NULL;
}
}
//查找Data数据的节点
DListNode *DListFind(DListNode *pHead, DataType data)
{
DListNode *cur = pHead->next;//用来记录头节点的位置
while(cur != pHead)
{
if(cur->data == data)
return cur;
cur = cur->next;
}
return NULL;
}
//销毁双向循环链表
void DListDestroy(DListNode *pHead)
{
DListNode *cur = pHead->next;
while(cur != pHead)
{
DListNode *ret = cur;
cur = cur->next;
free(ret);
ret = NULL;
}
free(pHead);
pHead = NULL;
}
link.c
#include"CircularList.h"
void Text()
{
DListNode *pHead = NULL;
DListInit(&pHead);
DListPushBack(pHead, 1);
DListPushBack(pHead, 2);
DListPushBack(pHead, 3);
//DListPopBack(pHead);
DListPushFront(pHead, 4);
DListPushFront(pHead, 5);
DListPopFront(pHead);
DListInsert(DListFind(pHead, 3),6);
DListErase(DListFind(pHead,4));
DListDestroy(pHead);
}
int main()
{
Text();
return 0;
}