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 <stack>
#include <queue>
using namespace std;
int main()
{
stack<char> stack_;
queue<char> queue_;
while (true)
{
char c;
cin.get(c);
if (cin.eof() || c == '\r' || c == '\n')
break;
stack_.push(c);
queue_.push(c);
}
int size = stack_.size();
for (int i = 0; i < size; i++)
{
char stack_char = stack_.top(); stack_.pop();
char queue_char = queue_.front(); queue_.pop();
if (stack_char != queue_char)
{
cout << "false";
return 0;
}
}
cout << "true";
return 0;
}
Comments
Leave a comment