例题2-12:请设计算法,判断带头结点的循环双向链表是否对称
核心代码:
bool Symmetry(CDLinkList L){
CDLinkList p=L->next,q=L->pre;
while(p!=q&&q->next!=p){ //链表个数为: 奇数 和偶数 同时讨论
if(p->data==q->data){
p=p->next;
q=q->pre;
}else{
return false*puts("该链表不对称");
}
}return true*puts("该链表对称");
}
调试代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#define LElemType int
#define inf 0x3f3f3f3f
using namespace std;
typedef struct CDLNode{
LElemType data;
struct CDLNode * pre;
struct CDLNode * next;
CDLNode(LElemType Data=0,struct CDLNode* Pre=NULL,struct CDLNode* Next=NULL){
data=Data; pre=Pre; next=Next;
}
}CDLNode,*CD