我使用堆栈和队列来检查给定的单词是否是回文。我可以将一个新字符推入堆栈,但我不能将多个字符推入队列。我看不到代码中的错误在哪里。任何帮助将不胜感激。下面是使用 Dev-C++ 的 C++ 代码。谢谢你的时间!
#include <iostream>
#include <stack>
#include <queue>
#include <string>
using namespace std;
void push_char()
{
string givenword; int sizeword, countchar;
string letter;
stack<string> stackword; string stawo1;
queue<string> queueword; string quewo1;
cout<<"enter the word to test "<<endl;
getline(cin,givenword);
string str (givenword);
sizeword=str.size();
cout<<" the word given "<<givenword<<" size of word= "<<sizeword <<endl;
countchar=0;
bool pali=true;
while ((countchar<sizeword))
{
stackword.push(str.substr(countchar,1));
queueword.push(str.substr(countchar,1));
cout<<" stack letter= "<<stackword.top()<<" queue letter= "<<queueword.front()<<endl;
countchar++;
if(stackword.top()==queueword.front())
cout<<"same letter found !"<<endl;
else
pali=false;
if (pali==false)
cout<<"not a palindrome"<<endl;
else
cout<<"palindrome!"<<endl;
}
}
int main()
{
push_char();
}