数据结构的题目

写一个算法判断输入的字串符是否为回文,使用栈和队列,已经有头文件了,我写的源程序#include<iostream.h>#include"Stack.h"//栈的头文件#... 写一个算法判断输入的字串符是否为回文,使用栈和队列,已经有头文件了,我写的源程序
#include <iostream.h>
#include "Stack.h" //栈的头文件
#include "QUEUE.H" //队列的头文件
bool Check(char c[])
{
SqStack S;
LinkQueue L;
InitQueue (L);
InitStack (S);
int e,f,i=0;
while (c[i]!='@')
{
Push(S,c[i]); //向栈中插入元素
EnQueue(L,c[i]); //向队列中插入元素
i++;
}
while (!StackEmpty(S)&&!QueueEmpty(L))
{
Pop(S,e); //弹出元素
DeQueue (L,f); //弹出元素
if (e==f)

cout<<"";
else return 0;
}
return 1;
}

int main()
{
char c[30];
cin.getline(c,30);
if(Check(c))
{
cout<<"是回文";
}
else cout<<"不是回文";
return 0;
}
但是现在用这个无法成功判断,求高手指导下
楼下的高手,我用你的程序还是无法正确的判断回文啊, 头文件的下载地址发不出来啊 你能直接跟我对话吗?
展开
 我来答
匿名用户
2009-10-13
展开全部
请写出你的数据输入格式以及所用的调试环境(包括所用的IDE以及操作系统等信息)

我的输入格式是:

字符串@

然后回车,之所以要在后面加上@是因为你的程序中要求这样做的。
////////////////////////////////////////////////
你自己写的check()函数以及主函数没有算法问题 ,请检查你用的其它代码的是否有问题或者检查一下你的函数调用是否有问题。

以下是我补全程序中用到的栈和队列的相关代码后的程序(没有修改check()和main()函数,只是添加了缺少的代码) ,在C++BUILDER 2007中测试通过:

//---------------------------------------------------------------------------

#include <iostream.h>
/*************************************
#include "Stack.h" //栈的头文件
#include "QUEUE.H" //队列的头文件

这两个文件没有提供,因此将其代码补充到下面
**************************************/

/******以下代码段为我根据所需要添加的栈和队列的定义以及程序中用到的其它操作函数*****/

typedef struct Head {char c; struct Head *next;} Head;
typedef struct sqstack{
int size;
Head *head;
} SqStack;

typedef struct{
char d[100];
int begin;
int end;
int size;
} LinkQueue;

void InitQueue(LinkQueue &a)
{
a.begin=a.end=a.size=0;
}

void InitStack(SqStack &a)
{
a.size=0 ;
a.head=NULL;
}

void Push(SqStack &a,char ch)
{
Head *st=new Head;
st->c=ch;
st->next =a.head;
a.head=st;
++(a.size);

}
void EnQueue(LinkQueue &a,char ch)
{
a.d[a.end++]=ch;
++(a.size);
}
bool StackEmpty(SqStack &a)
{
return a.size==0;
}
bool QueueEmpty(LinkQueue &a)
{
return a.size==0;
}

void Pop(SqStack &a,int &e)
{
Head *fr=a.head;
e=fr->c;
a.head=a.head->next ;
delete[] fr;
--(a.size);
}
void DeQueue(LinkQueue &a,int &c)
{
c=a.d[a.begin];
++(a.begin);
--(a.size);
}

/******添加代码段结束,以下为原程序代码段******/
bool Check(char c[])
{
SqStack S;
LinkQueue L;
InitQueue (L);
InitStack (S);
int e,f,i=0;
while (c[i]!='@')
{
Push(S,c[i]); //向栈中插入元素
EnQueue(L,c[i]); //向队列中插入元素
i++;
}
while (!StackEmpty(S)&&!QueueEmpty(L))
{
Pop(S,e); //弹出元素
DeQueue (L,f); //弹出元素
if (e==f) cout<<"";
else return 0;
}
return 1;
}

int main()
{
char c[30];
cin.getline(c,30);
if(Check(c))
{
cout<<"是回文";
}
else cout<<"不是回文";
return 0;
}

//---------------------------------------------------------------------------
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式