Answer to Question #286489 in C++ for hajar

Question #286489

Write your OWN CLASS for realization one of data structure - Queue, Stack, Singly linked list, Doubly linked list, Circular list, Random access array.


Develop a program that reads from the keyboard sequence of N unique integers N (1 <N <256), saves them to a data structure (Queue, Stack, Singly linked list, Doubly linked list, Circular list, Random access array.) and displays the following characteristics: 

- number of elements; 

- average of saved items; 

- minimum and maximum element; 

- fourth element of the sequence; 

- element that is before the minimum element.

We emphasize that all the characteristics necessary must be define for a filled data structure. Only those operations that are inherent to given structure are allowed, for example, one must not access an element on an arbitrary position in a queue which is based on an array. Usage of ready data structures (e.g., STL) is prohibited. 


1
Expert's answer
2022-01-11T09:44:31-0500
#include <bits/stdc++.h>
using namespace std;


struct MyStack {
    stack<int> s;
    int bar;


    void mxNum()
    {
        if (s.empty())
            cout << "Stack is empty\n";


        else
            cout << "Maximum Element in the stack is: "
                << bar << "\n";
    }


    void foo()
    {
        if (s.empty()) {
            cout << "Stack is empty ";
            return;
        }


        int t = s.top();


        cout << "Top Most Element is: ";


    
        (t > bar) ? cout << bar : cout << t;
    }


    void pop()
    {
        if (s.empty()) {
            cout << "Stack is empty\n";
            return;
        }


        cout << "Top Most Element Removed: ";
        int t = s.top();
        s.pop();


        
        if (t > bar) {
            cout << bar << "\n";
            bar = 2 * bar - t;
        }


        else
            cout << t << "\n";
    }


    void push(int x)
    {
        if (s.empty()) {
            bar = x;
            s.push(x);
            cout << "Number Inserted: " << x << "\n";
            return;
        }


        if (x > bar) {
            s.push(2 * x - bar);
            bar = x;
        }


        else
            s.push(x);


        cout << "Number Inserted: " << x << "\n";
    }
};


int main()
{
    MyStack s;
    s.push(99);
    s.push(1);
    s.mxNum();
    s.push(12);
    s.push(9);
    s.mxNum();
    s.pop();
    s.mxNum();
    s.pop();
    s.foo();


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment