In: Computer Science
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
void printFromStack(string expr){
stack<char> myStack;
for(int i=0; i<expr.length(); i++){
//Insert code here to push each character onto the stack
}
cout << "My stack is popped in this order" << endl;
while(!myStack.empty()){
//Insert code here to cout the top of the stack one by one
//Pop each one after it’s printed out
}
cout << endl;
}
void printFromQueue(string expr){
queue<char> myQueue;
//Insert code here to push each character onto the queue
cout << "My queue is dequeued in this order" << endl;
//Insert code here to cout the front of the queue one by one
//Pop (or dequeue) each one after it’s printed out
cout << endl;
}
bool isPalindrome(string expr){
queue<char> myQueue;
stack<char> myStack;
bool isPalindrome = true;
//Complete your code here!
while (isPalindrome==true && (!myStack.empty() && !myQueue.empty()))
{
//Complete your code here!
}
return isPalindrome;
}
int main(int argc, const char * argv[])
{
string expression;
while (expression != "end"){
cout << "Please enter an expression that you want to store: " << endl;
cin >> expression;
printFromStack(expression);
printFromQueue(expression);
cout << "Your expression: " << expression << " is " << (isPalindrome(expression)? "a palindrome!": "not a palindrome!") << endl;
}
return 0;
}
the question is inside of coding. needed to complete in c++ form
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
void printFromStack(string expr)
{
// create a queue
stack<char> myStack;
int i;
// raverse the string
for( i = 0 ; i < expr.length() ; i++ )
// push the character into stack
myStack.push(expr[i]);
cout<<"My stack is popped in this order"<<endl;
// loop untill the qaueue is empty
while( !myStack.empty() )
{
// display the element in the top
cout<<myStack.top()<<" ";
// pop the element
myStack.pop();
}
cout<<endl;
}
void printFromQueue(string expr)
{
// create a queue
queue<char> myQueue;
int i;
// raverse the string
for( i = 0 ; i < expr.size() ; i++ )
// push the character into queue
myQueue.push(expr[i]);
cout<<"My queue is dequeued in this order"<<endl;
// loop untill the queue is empty
while( !myQueue.empty() )
{
// display the element in the front
cout<<myQueue.front()<<" ";
// pop the element
myQueue.pop();
}
cout<<endl;
}
bool isPalindrome(string expr)
{
queue<char> myQueue;
stack<char> myStack;
bool isPalindrome = true;
int i;
// raverse the string
for( i = 0 ; i < expr.size() ; i++ )
// push the character into queue
myQueue.push(expr[i]);
// raverse the string
for( i = 0 ; i < expr.length() ; i++ )
// push the character into stack
myStack.push(expr[i]);
while( isPalindrome == true && ( !myStack.empty() && !myQueue.empty() ) )
{
// if the elements are not same
if( myStack.top() != myQueue.front() )
{
isPalindrome = false;
}
myStack.pop();
myQueue.pop();
}
return isPalindrome;
}
int main(int argc, const char * argv[])
{
string expression;
while (expression != "end")
{
cout << "Please enter an expression that you want to store: " << endl;
cin >> expression;
printFromStack(expression);
printFromQueue(expression);
cout << "Your expression: " << expression << " is " << (isPalindrome(expression)? "a palindrome!": "not a palindrome!") << endl;
}
return 0;
}
Sample output
