编写一个程序,实现双链表的各种基本运算和整体建表算法(假设双 链表的元素类型 ElemType 为 char),并在此基础上设计一个程序 cdlink_main.c 完成以下功能方法。
(1)初始化循环双链表 h。
(2)依次采用尾插法插入 a、b、c、d、e 元素 。
(3)输出循环双链表 h。
(4)输出循环双链表 h 的长度。
(5)判断循环双链表 h 是否为空。
(6)输出循环双链表 h 的第 3 个元素。
(7)在第 4 个元素的位置上插入 f 元素。
(8)输出循环双链表 h。
(9)删除循环双链表 h 的第 3 个元素。
(10)输出循环双链表 h。
(11)释放循环双链表 h。
#include <stdio.h>
#include <cstdlib>
typedef char ElemType;
typedef struct Dnode
{ ElemType data;
struct Dnode *prior;
struct Dnode *next;
}DLinkNode;
DLinkNode * InitList()
{
DLinkNode *L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结
点
L->next=NULL;
return L;
}
void CreateListR(DLinkNode *&h,int n) //尾插法建立循环双
链表
{
DLinkNode *s,*r;int i;
h=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点
h->next=NULL;
r=h;
printf("请输入字符数组:\n"); //r 始终指向尾结点,开始时指向头结点
for (i=0;i<n;i++)
{
s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点
scanf("%s",&s->data);
r->next=s;s->prior=r; //将结点 s 插入结点
r 之后
r=s;
}
r->next=h; //尾结点 next 域指向头
结点
h->prior=r; //头结点的 prior 域
指向尾结点
}
void DestroyList(DLinkNode **h)
{
DLinkNode *p=*h,*q=p->next;
while (q!=*h)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
bool ListEmpty(DLinkNode *h)
{
return(h->next==h);
}
int ListLength(DLinkNode *h)
{
DLinkNode *p=h;
int i=0;
while (p->next!=h)
{
i++;
p=p->next; }
return(i);
}
void DispList(DLinkNode *h)
{
DLinkNode *p=h->next;
while (p!=h)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
bool GetElem(DLinkNode *L,int i,ElemType &e) //求线性表中第 i 个元
素值
{
int j=1;
DLinkNode *p=L->next;
if (i<=0 || L->next==L)
return false; //i 错误或者 L 为空表返回假
if (i==1) //i=1 作为特殊情况处理
{
e=L->next->data;
return true;
}
else //i 不为 1 时
{
while (j<=i-1 && p!=L) //查找第 i 个结点 p
{
j++;
p=p->next;
}
if (p==L) //没有找到第 i 个节,返回假
return false;
else //找到了第 i 个节,返回真
{
e=p->data;
return true; }
}
}
bool ListInsert(DLinkNode *h,int i,ElemType e)
{
int j=0;
DLinkNode *p=h,*s;
if (p->next==h) //原双链表为空表时
{
s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点 s
s->data=e;
p->next=s;s->next=p;
p->prior=s;s->prior=p;
return true;
}
else if (i==1) //原双链表不为空表
但 i=1 时
{
s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点 s
s->data=e;
s->next=p->next;p->next=s; //将结点 s 插入到结
点 p 之后
s->next->prior=s;s->prior=p;
return true;
}
else
{
p=h->next;
while (j<i-2 && p!=h)
{ j++;
p=p->next;
}
if (p==h) //未找到第 i-1 个结点
return false;
else //找到第 i-1 个结点*p
{
s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点 s
s->data=e;
s->next=p->next; //将结点 s 插入到结点p 之后
if (p->next!=NULL) p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
}
}
bool ListDelete(DLinkNode *h,int i)
{
int j=0;
DLinkNode *p=h,*q;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if (p==NULL) //未找到第 i-1 个结点
return false;
else //找到第 i-1 个结点 p
{ q=p->next; //q 指向第 i 个结点
if (q==NULL) //当不存在第 i 个结点时返
回 false
return false;
p->next=q->next; //从双链表中删除 q 结点
if (q->next!=NULL) //若 q 结点存在后继结点
q->next->prior=p; //修改 q 结点后继结点
的前驱指针
free(q); //释放 q 结点
return true;
}
}
int main()
{
DLinkNode *h;
ElemType e ;
h=InitList();
CreateListR(h,5); DispList(h);
printf("循环双链表 h 的长度为:%d\n",ListLength(h));
while(!ListEmpty(h))
{
printf("循环双链表不为空。\n");
break;
}
GetElem(h,3,e);
printf("链表第 3 个元素为:%c\n",e);
ListInsert(h,4,'f');
DispList(h);
ListDelete(h,3);
DispList(h);
DestroyList(&h);
}