Question

In: Computer Science

Consider the following statements: linkedStackTypemtack; linkedQueueType queue; int num; Suppose the input is 28 30 15...

Consider the following statements:

linkedStackTypemtack; linkedQueueType queue; int num;

Suppose the input is

28 30 15 11 10 -9 21 8 -3 33 17 14

Write a C++ code that processes these numbers as follows: If the number is an even number, it is pushed onto the stack. If the number is odd and divisible by 3, it is added into the queue; otherwise the top element, if any, of the stack is removed and the square of the number is added onto the stack. After processing these numbers, what is stored in stack and queue?

please with details, do not copy from the book, thank you

Solutions

Expert Solution

//Following is the required C++ program

#include <bits/stdc++.h>
using namespace std;

void showstack(stack <int> s) //function to show stack
{
    while (!s.empty())
    {
        cout << '\t' << s.top();
        s.pop();
    }
    cout << '\n';
}

void showqueue(queue <int> q) //function to show queue
{
  
    while (!q.empty())
    {
        cout << '\t' << q.front();
        q.pop();
    }
    cout << '\n';
}

int main(){
    int n;
    cout<<"Enter number of elements in the input: ";
    cin>>n; //Taking number of elements in the input
    stack<int>s; //declaring the stack
    queue<int>q; //declaring the queue
    int i,x;
    cout<<"Enter "<<n<<" elements: ";
  
    for(i=0;i<n;i++){ //loop to input every element
        cin>>x;
        //checking the conditions and pushing into stack or queue
        if(x%2==0){
            s.push(x);
        }else if(x%2==1 && x%3==0){
            q.push(x);
        } else if(!s.empty()){
            int sq = s.top();
            s.pop();
            s.push(sq*sq);
        }
    }
  
    //showing the stack and queue
    cout << "The stack is : ";
    showstack(s);
  
    cout << "The queue is : ";
    showqueue(q);
  
}


Sample Run:

Hope this helped. Please do upvote and if there are any queries please ask in comments section.


Related Solutions

Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
What will the following code segments print on the screen? int num = 3; switch (num){...
What will the following code segments print on the screen? int num = 3; switch (num){             case 1:                         System.out.println(“Spring”); case 2:                         System.out.println(“Summer”); case 3:                         System.out.println(“Autumn”); case 4:                         System.out.println(“Winter”); }
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25....
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25. Compute the 19th, 24th, 60th, and 70th percentiles. If needed, round your answers to two decimal digits. Percentile 19% 24% 60% 70% Value for percentile?
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25....
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25. Compute the 21st, 26th, 60th, and 70th percentiles. If needed, round your answers to two decimal digits.
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25....
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25. Compute the 20th, 25th, 58th, and 68th percentiles. If needed, round your answers to two decimal digits. Percentile   Value 20%   * 25%   * 58%   * 68% *
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25....
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25. Compute the 18th, 23rd, 59th, and 69th percentiles. If needed, round your answers to two decimal digits. Percentile Value 18% 23% 59% 69%
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25....
Consider a sample with data values of 27, 25, 20, 15, 30, 34, 28, and 25. Compute the 22nd, 27th, 59th, and 69th percentiles. If needed, round your answers to two decimal digits. Percentile Value 22% 27% 59% 69%
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT