#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char data;
struct node *priorPtr;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *headPtr, char ch);
static void Symmetry(LinkList headPtr); /* 判断双向链表是否对称函数 */
int main(void)
{
char ch;
LinkList headPtr = NULL;
printf("Enter a character('@' quit): ");
scanf("\n%c", &ch);
while (ch != '#')
{
CreateList(&headPtr, ch);
printf("continue enter character: ");
scanf("\n%c", &ch);
}
if (headPtr != NULL) /* 链表不空的情况下判断链表是否对称 */
{
Symmetry(headPtr);
}
else
{
printf("list is empty.\n");
}
return 0;
}
static void CreateList(LinkList *headPtr, char ch)
{
LinkList newPtr, previousPtr, currentPtr;
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
exit(1);
}
newPtr -> data = ch;
newPtr -> priorPtr = NULL;
newPtr -> nextPtr = NULL;
previousPtr = NULL;
currentPtr = *headPtr;
if (*headPtr == NULL)
{
newPtr -> priorPtr = *headPtr;
newPtr -> nextPtr = *headPtr;
*headPtr = newPtr;
}
else
{
while (currentPtr != NULL)
{
previousPtr = currentPtr;
currentPtr = currentPtr -> nextPtr;
}
previousPtr -> nextPtr = newPtr;
newPtr -> priorPtr = previousPtr;
newPtr -> nextPtr = currentPtr;
}
}
static void Symmetry(LinkList headPtr)
{
LinkList frontPtr, rearPtr;
int count = 1;
frontPtr = headPtr;
rearPtr = headPtr; /* 初始化为头结点*/
while (rearPtr -> nextPtr != NULL) /* 是rearPtr指向最后一个结点 */
{
rearPtr = rearPtr -> nextPtr;
count++; /* 计算结点的总数 */
}
count /= 2;
while (count != 0)
{
if (frontPtr -> data == rearPtr -> data)
{
frontPtr = frontPtr -> nextPtr;
rearPtr = rearPtr -> priorPtr;
count--;
}
else
{
break;
}
}
if (count == 0)
{
printf("symmetry success.\n");
}
else
{
printf("symmetry failure.\n");
}
}
#include <stdio.h>
#include <stdlib.h>
typedef struct DuLNode
{char data;
struct DuLNode *prior,*next;
}DuLNode,*DuLinkList;
//建立双向循环链表
DuLinkList Create(void){
char ch;
DuLinkList head=(DuLinkList)malloc(sizeof(DuLNode));
DuLinkList s,r;
head->next=NULL;
head->prior=NULL;
r=head;
while((ch=getchar())!='\n'){
s=(DuLNode*)malloc(sizeof(DuLNode));
s->data=ch;
r->next=s;
s->prior=r;
r=s;
}
r->next=head;
head->prior=r;
return head;
}
//打印
void Print(DuLinkList head){
DuLinkList p;
for(p=head->next;p!=head;p=p->next)
printf("%c",p->data);
printf("\n");
}
//判断双向循环链表是否对称
int DuiChen(DuLinkList head){
DuLinkList p,q;
int i=1;
p=head->next;
q=p;
while(q->next!=head)
{
q=q->next;
i++;}
if(i<4&&p->data==q->data)
return 1;
if(i>=4){
while(q->next!=p){
if(q->data==p->data){p=p->next;
q=q->prior;}
else return 0;}
return 1;}
}
void main()
{int x;
DuLinkList head;
head=Create();
Print(head);
x=DuiChen(head);
printf("%d",x);
}