A palindrome is a string that reads the same forwards as backwards. Using
only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed
number of int and char variables, write a code to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The code should output true or false as appropriate.
#include <iostream>
#include <queue>
#include <stack>
int main()
{
std::queue<char> myQueue;
std::stack<char> myStack;
while (true)
{
char c;
std::cin.get(c);
if (std::cin.eof() || c == '\r' || c == '\n')
break;
myStack.push(c);
myQueue.push(c);
}
int size = myStack.size();
bool result = true;
for (int i = 0; i < size; i++)
{
char queue_char = myQueue.front();
myQueue.pop();
char stack_char = myStack.top();
myStack.pop();
if (stack_char != queue_char)
{
result = false;
break;
}
}
std::cout << (result ? "true" : "false");
return 0;
}
Comments
Leave a comment