设单链表中存放n个字符,试设计一个算法,使用栈推断该字符串是否中心对称

时间:2020-11-28 04:24:42

转载请注明出处:http://blog.csdn.net/u012860063

问题:设单链表中存放n个字符。试设计一个算法,使用栈推断该字符串是否中心对称,如xyzzyx即为中心对称字符串。


代码例如以下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define LEN sizeof(struct node)
#define MAX 147
struct node
{
char cc;
struct node *next;
};
int judge(struct node *head,int len)
{
struct node *top,*p1,*p2;
top = NULL;
p1 = head->next;
for(int i = 0 ; i < len/2 ; i++)
{
p2 = (struct node *)malloc(LEN);
p2->cc = p1->cc;
p2->next = top;
top = p2;
p1 = p1->next;
}
if(len%2 == 1)
p1 = p1->next;
p2 = top;
for(i = 0 ; i < len/2 ; i++)
{
if(p2->cc != p1->cc)
break;
top = p2->next;
p1 = p1->next;
p2 = top;
}
if(!top)
return 1;
else
return 0;
} int main()
{
int n=0;
char str[MAX];
struct node *head,*p;
head = p = (struct node *)malloc(LEN);
head->next = p->next = NULL;
printf("亲、请输入一个字符串:\n");
gets(str);
int len = strlen(str);
while(n < len && str[0] != '\n')
{
p = (struct node *)malloc(LEN);
p->cc = str[n];
p->next = head->next;
head->next = p;
n++;
}
int flag = judge(head,len);
if(flag)
printf("%s是一个回文!\n",str);
else
printf("%s不是一个回文!\n",str);
return 0;
}