设单链表中存放着n个字符,设计算法,判断该字符串中是否有中心对称关系
#include<iostream.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define OK 1
typedef struct node
{
char data;
struct node *next;
} *linklist;
typedef struct{
char *base;
char *top;
int stacksize;
}SqStack;
void CreateList(linklist &L,int n)
{
L=(linklist)malloc(sizeof(node));
L->next=NULL;
for(int i=n;i>0;--i){
struct node *p;
p=(linklist)malloc(sizeof(node));
cin>>p->data;
p->next=L->next;
L->next=p;
}
}
int InitStack(SqStack &S)
{
S.base=(char*)malloc(STACK_INIT_SIZE*sizeof(char));
// S.base=new char[];
if(!S.base) exit(0);//内存分配失败
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
void ListPush(SqStack S,linklist L)
{
struct node *p;
InitStack(S);
p=L->next;
while(p)
{
*S.top=p->data;
p=p->next;
S.top++;
}
}
int judge(SqStack S,linklist L,int n)
{
int m=0;//m用来计算比较的次数
struct node *p;
p=L->next;
while(p)
{
if(*S.top==p->data)
{
p=p->next;
S.top--;
m++;
//return 1;
}
else
{
break;
}
//return 0;
//return m==n?1:0;
}
return m;//我返回m主要是看在比较过程中m有没有变化,单m总是不变
// cout<<m<<endl;
}
void main()
{
int n,H;
SqStack S;
linklist L;
cout<<"输入要判断的字符串个数n"<<endl;
cin>>n;
cout<<"输入n个字符"<<endl;
CreateList(L, n);
InitStack(S);
ListPush(S, L);
// judge(S,L,n);
H=judge(S,L,n);
cout<<H<<endl;
if(H)
cout<<"字符串中心对称"<<endl;
else
cout<<"字符串中心不对称"<<endl;
}