判断带头结点的双向循环链表L是否对称相等的算法

编写出判断带头结点的双向循环链表L是否对称相等的算法

<span style="font-weight: normal;">#include <iostream>
using namespace std;

typedef int ElemType;

typedef struct DNode{
	ElemType data;
	struct DNode *prior;
	struct DNode *next;
}DLinkList;

void CreateListR(DLinkList * &L,ElemType a[],int n){    //尾插法建立双链表 
	DLinkList *s , *r;
	int i;
	L=new DLinkList;
	r = L;
	for(i=0;i<n;i++){
		s=new DLinkList;
		s->data = a[i];
		r->next = s;  
		s->prior=r;
		r=s;
	} 
	L->prior=s;
	r->next=L;
}

bool IsEqual(DLinkList * &L){
	 DLinkList *l , *r;
	 l=L->prior;
	 r=L->next;
	 while(l!=r){
	 	if(l->data!=r->data)
	 		return false;
	 	else{
	 		l=l->prior;
	 		r=r->next;
		 }
	 }
	 return true;
} 

void Destroy(DLinkList *&L){
	DLinkList *pre = L, *p = L->next;
	while(p!=L){
		delete(pre);
		pre=p;
		p=p->next;
	} 
	delete(pre);
}

int main(){
	DLinkList *L;
	int n,i;
	int a[10000];
	cin>>n;
	for(i=0;i<n;i++)
		cin>>a[i];
	CreateListR(L,a,n);
	if(IsEqual(L))
		cout<<"双链表对称"<<endl;
	else cout<<"双链表不对称"<<endl; 
	Destroy(L);
	return 0;
}</span>

    原文作者:C_Khalid
    原文地址: https://blog.csdn.net/C_Khalid/article/details/52782047
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞