//设计一个算法用于判断带头结点的循环双链表是否对称
include <stdio.h>
include <stdlib.h>
typedef struct DNode
{
int data;
struct DNode *prior, *next;
} DNode, *DLinkList;
DLinkList DList_TailInsert(DLinkList L, int n)
{
int i; //设元素类型为整型
L = (DLinkList)malloc(sizeof(DNode));
L->prior = L->next = L;
DNode *p, *q = L;
printf("请输入表元素:");
for (i = 0; i < n; i++)
{
p = (DNode *)malloc(sizeof(DNode));
scanf("%d", &p->data);
p->next = q->next;
q->next = p;
p->prior = q;
L->prior = p;
q = p;
}
return L;
}
void print(DLinkList L)
{
DLinkList p;
p = L->next;
while (p != L)
{
printf("%d ", p->data);
p = p->next;
}
}
int Symmetry(DLinkList L)
{
DNode *p = L->next, *q = L->prior; //两头工作指针
while (p != q && q->next != p) //循环跳出条件
{
if (p->data == q->data) //所指结点值相同则继续比较
{
p = p->next;
q = q->prior;
}
else
{
return 0;
}
}
return 1;
}
int main()
{
DLinkList L, A;
int n;
A = (DLinkList)malloc(sizeof(DNode));
printf("请输入要建立的链表长度:");
scanf("%d", &n);
A = DList_TailInsert(L, n);
printf("尾插法建立的双向循环链表:");
print(A);
printf("\n");
if (Symmetry(A) == 1)
printf("该循环双链表对称");
else
printf("该循环双链表不对称");
printf("\n");
return 0;
}