Question

#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...


#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;
}



0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <stack>
#include <queue>
#include <string>

using namespace std;


void printFromStack(string expr) {
    stack<char> myStack;
    for (int i = 0; i < expr.length(); ++i) {
        myStack.push(expr[i]);
    }

    cout << "My stack is popped in this order" << endl;
    while (!myStack.empty()) {
        cout << myStack.top() << " ";
        myStack.pop();
    }
    cout << endl;
}

void printFromQueue(string expr) {
    queue<char> myQueue;

    for (int i = 0; i < expr.length(); ++i) {
        myQueue.push(expr[i]);
    }

    cout << "My queue is dequeued in this order" << endl;

    while (!myQueue.empty()) {
        cout << myQueue.front() << " ";
        myQueue.pop();
    }
    cout << endl;
}

bool isPalindrome(string expr) {
    queue<char> myQueue;
    stack<char> myStack;
    bool isPalindrome = true;

    for (int i = 0; i < expr.length(); ++i) {
        myQueue.push(expr[i]);
        myStack.push(expr[i]);
    }

    while (isPalindrome == true && (!myStack.empty() && !myQueue.empty())) {
        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;
}

Add a comment
Know the answer?
Add Answer to:
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • I have a queue and stack class program that deals with a palindrome, I need someone...

    I have a queue and stack class program that deals with a palindrome, I need someone to help to put in templates then rerun the code. I'd greatly appreciate it. It's in C++. Here is my program: #include<iostream> #include<list> #include<iterator> #include<string> using namespace std; class Queue { public: list <char> queue; Queue() { list <char> queue; } void Push(char item) { queue.push_back(item); } char pop() { char first = queue.front(); queue.pop_front(); return first; } bool is_empty() { if(queue.empty()) { return...

  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:       ...

    #include <iostream> #include <string> #include <cstring> using namespace std; class Node{       private:        int data;        Node* nextNodePtr;           public:        Node(){}               void setData(int d){            data = d;        }               int getData(){            return data;        }               void setNextNodePtr(Node* nodePtr){                nextNodePtr = nodePtr;        }                ...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top =...

    Convert following code to implement linked list C++ language #include<iostream> using namespace std; int top = -1; //globally defining the value of top, as the stack is empty void push(int stack[], int x, int n) { if (top == -1) //if top position is the last of posiition of stack,means stack is full { cout << "Stack is full Overflow condition"; } else { top = top + 1; //incrementing top position stack[top] = x; //inserting element on incremented position...

  • //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...

    //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what) : what(what) {} string getWhat() {return what;} private: string what; }; #endif //stack_test_app.cpp #include <iostream> #include <sstream> #include <string> #include "stack.h" using namespace std; /********************************************* * The 'contains' function template goes here * *********************************************/ int main() {    cout << boolalpha;    cout << "--- stack of int" << endl;    Stack<int> si;    cout << "si intially " << si << endl;   ...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string,...

    #include <iostream>   #include <string>   using namespace std;      void get_user_string(string *);   string convert_to_dash(String* );   int_search_and_replace(char, string, string &);       int main (){    string s;    cout << "Enter a string:" << endl;    get_user_string(&s);        string dash_version = convert_to_dash(&s)    if ( dash_version != 32){    &s.push_back('-');    } Here is an example operation of the completed program: Please enter a string: the weather is great! The dash-version of your string is: Please tell me the char that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT