主题:关于串回文判读的问题
lsx6244413 [专家分:0] 发布于 2008-10-02 10:06:00
我用链栈编了一个回文串判读问题,编译通过了,就是运行时要报错,求高人赐教
#include<iostream>
using namespace std;
typedef struct Stack
{
char data;
struct Stack *next;
}Stactk,*Link;
void Creat(Link &S)
{
S=(Link)malloc(sizeof(Link));
S->next=NULL;
}
void Push(Link &S,char e)
{
Link p;
p=(Link)malloc(sizeof(Link));
p->data=e;
p->next=S;
S=p;
}
void Pop(Link &S,char e)
{
Link p;
if(p==NULL)
cout<<"The stack is empty"<<endl;
else
{
e=S->data;
p=S;
S=S->next;
free(p);
}
}
void main()
{
char s1[20],s2[20];
Link S;
int i=0,j=0;
Creat(S);
cout<<"Please input a string "<<endl;
cin>>s1;
for(;i<strlen(s1);i++)
{
if(s1[i]!=' ')
Push(S,s1[i]);
}
for(;j<strlen(s1);j++)
Pop(S,s2[j]);
if(strcmp(s1,s2)==0)
cout<<"This Word is Huiwen"<<endl;
else
cout<<"This Word is not Huiwen"<<endl;
}
回复列表 (共9个回复)
沙发
JackieRasy [专家分:3050] 发布于 2008-10-02 10:24:00
不知道楼主用的是什么编译器,开始我根本看不不出什么毛病,一检测,vc++6.0编译和连接都通过。而我就在主函数回括号前加了个 cin.get();
板凳
lsx6244413 [专家分:0] 发布于 2008-10-02 10:36:00
我就用的是VC++6.0,编译能通过,就是在运行是总要中止
3 楼
scaredkinght [专家分:5310] 发布于 2008-10-02 11:05:00
void Push(Link &S,char e)
{
Link p;
p=(Link)malloc(sizeof(Link));//因该是sizeof(Stack), C++就别用malloc 和 free
p->data=e;
p->next=S;
S=p;
}
4 楼
lsx6244413 [专家分:0] 发布于 2008-10-02 11:09:00
恩,可以运行了,不过不能正确计算,比如输入abccba结果还是说不是回文
5 楼
烈焰燃烧 [专家分:2400] 发布于 2008-10-02 11:39:00
void Pop(Link &S,char e) // e为传值参数
....
Pop(S,s2[j]); // s2[j]的值并没被改变
// 最后,记得给s2[j]加个'\0'
6 楼
mht@ [专家分:1260] 发布于 2008-10-03 22:21:00
呵呵,你是 "戏绝游拒~"~~~~???
??
7 楼
foolharry [专家分:0] 发布于 2008-10-31 23:14:00
同意5楼意见
8 楼
hhgirl [专家分:70] 发布于 2008-11-06 23:12:00
我算是服了你了,回文串只要几行代码搞定,楼主搞了这么多.
9 楼
moke9 [专家分:30] 发布于 2010-09-02 07:58:00
你好.我是全职网赚工作者.
如果你有时间有电脑.
想在网络上创业.请联系我..
项目绝对真实.详情QQ空间资料
加盟请联系 QQ908889846
我来回复