设单链表中存放着n个字符,设计算法,判断该字符串中是否有中心对称关系

cl7106786 2011-10-11 07:27:19
#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;

}

...全文
1111 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cl7106786 2011-10-11
  • 打赏
  • 举报
回复
中心对称就是 xyzzyx、xyzyx都算是中心对称的字符串
我把InitStack(S);这行删啦,那不是没有给栈分配内存吗?
我的问题是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;
}

当比较字符串的时候m的值根本就没有变化
所以return m==n?1:0;
这个条件就有问题啦
星羽 2011-10-11
  • 打赏
  • 举报
回复
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++;
}


ps: 你的中心对称啥规则
jixingzhong 2011-10-11
  • 打赏
  • 举报
回复
你的问题是什么?

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧