使用堆栈和队列判断字符序列是否是回文。帮忙找错?????????

来源:百度知道 编辑:UC知道 时间:2024/06/04 05:17:48
void HuiWen(char str[])
{
LQueue myQueue;
LSNode myStack;
LSNode *head;
char x,y;
int i,length;
length=strlen(str);
QueueInitiate(&myQueue);
StackInitiate(&head);//问题出在这里!
for(i=0;i<length;i++)
{
QueueAppend(&myQueue,str[i]);
StackPush(&myStack,str[i]);
}

while(QueueNotEmpty(&myQueue)==1&&StackNotEmpty(&myStack)==1)
{
if(QueueDelete(&myQueue,&x)==1&&StackPop(&myStack,&y)==1&&x==y)
{
printf("%s is a HuiWen!\n",str);
return;
}
}
if(QueueNotEmpty(&myQueue)||StackNotEmpty(&myStack))
printf("%s is not HuiWen\n",str);
else
printf("%s is HuiWen?n",str);
}

不知道你的队列与栈是如何实现的,或许是照抄的stl?

也许
if(QueueDelete(&myQueue,&x)==1&&StackPop(&myStack,&y)==1&&x==y)
{
printf("%s is a HuiWen!\n",str);
return;
}
出了问题,可能有一个字符相等就判断是回文。

我写了一个代码,工作得很好。
#include <cstdio>
using namespace std;
class Lnode{
public:
char data;
Lnode *next;
Lnode(){next=NULL;}
};
class Stack{
private:
Lnode *head;
public:
Stack(){head=NULL;}
void Push(char ch){
Lnode *p;
p=new Lnode;
p->next=head;
p->data=ch;
head=p;
}
void Pop(char &ch){
Lnode *p;
if (!head){
ch=EOF;
return;
}else{
ch=head->data;
p=head->next;
delete head;
head=p;
}
}
int NotEmpty(){return head!=NULL;}
~Stack(){
Lnode *p;
while (head){
p=head->next;
delete head;
head=p;
}
}