设单链表的表头指针为head,结点结构由data和next两个域构成,其中data是字符型,写出中心算法,判断该链表的前n个字符是否为中心对称,例如 xyx 和 xyyx 都是中心对称,请将函数int DuiChen(LinkList *head)补充完整,其中是中心对称返回1,不是返回0。
#include <stdio.h>
#define OK 1
#include <stdlib.h>
#include <malloc.h>
typedef struct node {
char data;
struct node *next;
} LinkList;
LinkList *InitList() {
LinkList *head;
head = (LinkList *)malloc(sizeof(LinkList));
head->next = NULL;
return head;
}
LinkList *CreateList(int n) {
int i;
LinkList *head, *p, *r;
head = (LinkList *)malloc(sizeof(LinkList));
r = head;
for (i = 1; i <= n; i++) {
p = (LinkList *)malloc(sizeof(LinkList));
scanf("%c", &(p->data ));
r->next = p;
r = p;
}
r->next = NULL;
return head;
}
void PrintList(LinkList *head) {
int cnt = 0;
LinkList *p = head->next ;
while (p != NULL) {
cnt++;
printf("%c,", p->data);
p = p->next ;
}
printf("\n");
printf("该单链表的长度为: %d\n", cnt);
}
int DuiChen(LinkList *head) {
LinkList *p,*q,*r,*t,*f;
int i=1,j=1;
q=(LinkList *)malloc(sizeof(LinkList));
q->next=NULL;
p=head;
while(p!=NULL){
p=p->next;
j++;
}
p=head->next;
for(;i<j-1;i++){
r=(LinkList *)malloc(sizeof(LinkList));
r->data=p->data;
r->next=q->next;
q->next=r;
p=p->next;
}
i=1;
int s=1;
p=head->next;
t=q->next;
for(;i<j-1;i++){
if(p->data==t->data){
s++;
}
p=p->next;
t=t->next;
}
if(s==j-1){
return 1;
}else{
return 0;
}
}
int main() {
LinkList *head;
int n;
scanf("%d", &n);
getchar();
head = CreateList(n);
if (DuiChen(head)) {
printf("True!\n");
} else {
printf("False!\n");
}
}