/*设单链表的表头指针为L,结点结构由data、next两个域构成,其中data域为字符型。试设计算法判断该链表
的全部n个字符是否中心对称。例如xyx、xyyx都是中心对称。*/
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 50
typedef char ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList tailInsert(LinkList &L)
{
ElemType x;
L = (LinkList)malloc(sizeof(LNode));
LNode *r = L;
scanf("%c",&x);
while(x!='\n')
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
scanf("%c",&x);
}
r->next = NULL;
return L;
}
bool printList(LinkList L)
{
LNode *s = L->next;
while(s)
{
printf("%c",s->data);
s = s->next;
}
printf("\n");
return true;
}
bool judgeSym(LinkList L,int n)
{
int i;
ElemType a[MaxSize];
LNode *p = L->next;
for(i=0;i<n/2;++i)
{
a[i] = p->data;
p = p->next;
}
--i;
if(n%2 == 1)
p = p->next;
while(p && a[i]==p->data)
{
--i;
p = p->next;
}
if(i == -1)
{
printf("对称!\n");
return true;
}
else{
printf("不对称!\n");
return false;
}
}
void main()
{
LinkList L;
tailInsert(L);
printList(L);
judgeSym(L,5);
}
判断字符串是否对称
最新推荐文章于 2022-12-17 15:40:02 发布