双向循环链表
双向链表每结点有两个指针,一个指向结点的前驱,另一个指向结点的后继,所以从链表的每一个结点出发,都可到达任意一个结点,有利于链表的查找,单链表的找前驱函数,除了有指向当前结点的指针外,还有一个紧跟其,一直指向其前驱的指针。在双向链表中。不需要这个指向前驱的指针。
双向循环链表的实现
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//线性表的双向链表存储结构
typedef struct Node
{
int data; //数据域
struct Node *prior,*next; //一个指向前驱,一个指向后继
}NODE,*PNODE;
//基本操作
PNODE Create1(); //创建双向循环链表,在表尾插入数据
PNODE Create2(); //创建双向循环链表,在表头插入数据
void Init(PNODE pHead); //构造空的双向循环链表
void Destroy(PNODE pHead); //销毁双向循环链表
void Clear(PNODE pHead); //将双向链表置空
bool Empty(PNODE pHead); //判断双向链表是否为空
int Length(PNODE pHead); //返回链表元素个数
bool Get(PNODE pHead,int i,int *e); //将第i个元素的值赋给e
bool Prior(PNODE pHead,int cur_e,int *pre_e);//用pre_e保存cur_e元素的前驱元素
bool Next(PNODE pHead,int cur_e,int *next_e); //用pre_e保存cur_e元素的后继元素
bool Insert(PNODE pHead,int i,int e); //在第i个位置前面插入元素e
bool Delete(PNODE pHead,int i,int *e);//删除第i个位置的元素,用e保存
void Traverse1(PNODE pHead); //正向遍历双向链表
void Traverse2(PNODE pHead); //逆向遍历双向链表
int main()
{
int val;
PNODE pHead=NULL;
pHead=Create1();
Traverse1(pHead);
printf("链表元素个数为:%d\n",Length(pHead));
/* Clear(pHead);
if(Empty(pHead))
printf("链表为空!\n");
else
printf("链表不为空!\n");
*/
if(Get(pHead,3,&val))
printf("第三个元素的值为:%d\n",val);
else
printf("第三个元素无值!\n");
if(Prior(pHead,3,&val))
printf("元素3的前驱元素是:%d\n",val);
else
printf("元素3无前驱!\n");
if(Next(pHead,3,&val))
printf("元素3的后继元素是:%d\n",val);
else
printf("元素3无后继!\n");
if(Insert(pHead,4,100))
printf("插入成功!\n");
else
printf("插入失败!\n");
Traverse1(pHead);
printf("链表元素个数为:%d\n",Length(pHead));
if(Delete(pHead,3,&val))
printf("删除的元素为:%d\n",val);
else
printf("删除失败!\n");
Destroy(pHead);
printf("销毁成功!\n");
return 0;
}
PNODE Create1()
{
int i,val;
PNODE pHead,pNew,pTail;
pHead=(PNODE)malloc(sizeof(NODE));
if(pHead==NULL)
exit(-1);
pHead->next=pHead;
pHead->prior=pHead;
pTail=pHead;
printf("请输入申请的元素的个数:");
scanf("%d",&val);
if(val<1)
exit(-1);
for(i=0;i<val;i++)
{
pNew=(PNODE)malloc(sizeof(NODE));
if(pNew==NULL)
exit(-1);
printf("输入第%d个元素的值:",i+1);
scanf("%d",&pNew->data);
pTail->next=pNew;
pNew->prior=pTail;
pTail=pNew;
}
pHead->prior=pTail;
pTail->next=pHead;
return pHead;
}
PNODE Create2()
{
int i,val;
PNODE pHead,pNew;
pHead=(PNODE)malloc(sizeof(NODE));
if(pHead==NULL)
exit(-1);
pHead->next=pHead;
pHead->prior=pHead;
printf("请输入申请的元素的个数:");
scanf("%d",&val);
if(val<1)
exit(-1);
pNew=(PNODE)malloc(sizeof(NODE));
if(pNew==NULL)
exit(-1);
printf("输入第1个元素的值:");
scanf("%d",&pNew->data);
pHead->next=pNew;
pNew->next=pHead;
pHead->prior=pNew;
pNew->prior=pHead;
for(i=0;i<val-1;i++)
{
pNew=(PNODE)malloc(sizeof(NODE));
if(pNew==NULL)
exit(-1);
printf("输入第%d个元素的值:",i+2);
scanf("%d",&pNew->data);
pNew->next=pHead->next;
pHead->next=pNew;
pNew->prior=pHead;
pNew->next->prior=pNew;
}
return pHead;
}
void Init(PNODE pHead)
{
pHead=(PNODE)malloc(sizeof(NODE));
if(pHead==NULL)
exit(-1);
pHead->prior=pHead;
pHead->next=pHead;
}
void Destroy(PNODE pHead)
{
PNODE p=(pHead)->prior,q;
while(pHead!=p)
{
q=(pHead)->next;
free(pHead);
pHead=q;
}
free(pHead);
pHead=NULL;
}
void Clear(PNODE pHead)
{
PNODE p=pHead->next,q;
while(p!=pHead)
{
q=p->next;
free(p);
p=q;
}
pHead->prior=pHead;
pHead->next=pHead;
}
bool Empty(PNODE pHead)
{
if(pHead->next==pHead&&pHead->prior==pHead)
return true;
else
return false;
}
int Length(PNODE pHead)
{
int count=0;
PNODE p=pHead->next;
while(p!=pHead)
{
count++;
p=p->next;
}
return count;
}
bool Get(PNODE pHead,int i,int *e)
{
int j=1;
PNODE p=pHead->next;
while(j<i&&p!=pHead)
{
j++;
p=p->next;
}
if(j>i||p==pHead)
return false;
*e=p->data;
return true;
}
bool Prior(PNODE pHead,int cur_e,int *pre_e)
{
PNODE p=pHead->next->next;
while(p!=pHead)
{
if(p->data==cur_e)
{
*pre_e=p->prior->data;
return true;
}
p=p->next;
}
return false;
}
bool Next(PNODE pHead,int cur_e,int *next_e)
{
PNODE p=pHead->next;
while(p!=pHead->prior)
{
if(p->data==cur_e)
{
*next_e=p->next->data;
return true;
}
p=p->next;
}
return false;
}
bool Insert(PNODE pHead,int i,int e)
{
int j=1;
PNODE p=pHead,pNew;
if(i<1||i>Length(pHead)+1)
return false;
while(j<i)
{
j++;
p=p->next;
}
pNew=(PNODE)malloc(sizeof(NODE));
if(pNew==NULL)
exit(-1);
pNew->data=e;
pNew->next=p->next;
p->next=pNew;
pNew->prior=p;
pNew->next->prior=pNew;
return true;
}
bool Delete(PNODE pHead,int i,int *e)
{
int j=1;
PNODE p=pHead->next;
if(i<1||i>Length(pHead))
return false;
while(j<i)
{
p=p->next;
j++;
}
*e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
return true;
}
void Traverse1(PNODE pHead)
{
PNODE p=pHead->next;
while(p!=pHead)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Traverse2(PNODE pHead)
{
PNODE p=pHead->prior;
while(p!=pHead)
{
printf("%d ",p->data);
p=p->prior;
}
printf("\n");
}