请写出你的数据输入格式以及所用的调试环境(包括所用的IDE以及操作系统等信息)
我的输入格式是:
字符串@
然后回车,之所以要在后面加上@是因为你的程序中要求这样做的。
////////////////////////////////////////////////
你自己写的check()函数以及主函数没有算法问题 ,请检查你用的其它代码的是否有问题或者检查一下你的函数调用是否有问题。
以下是我补全程序中用到的栈和队列的相关代码后的程序(没有修改check()和main()函数,只是添加了缺少的代码) ,在C++BUILDER 2007中测试通过:
//---------------------------------------------------------------------------
#include
/*************************************
#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;
}
//---------------------------------------------------------------------------