Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Add function showstack() in the upper implementation of the stack code

+3 votes
asked Jun 4, 2019 by mark (440 points)
Add function showstack() in the upper implementation of the stack code, which should get stack object as an argument and print out the content of the stack

1 Answer

0 votes
answered 10 hours ago by Shaurya (410 points)

Purpose

Add a showstack() function that receives a stack object and prints its contents (top → bottom) without destroying or altering the original stack.

Two safe approaches (pick one that matches your existing implementation)

1. If your stack is a simple struct or class with accessible array and top

Make showstack take the stack by const& and iterate the internal array from top down to 0.

#include <iostream>
using namespace std;

const int MAX = 100;

struct Stack {
    int arr[MAX];
    int top;
    Stack() : top(-1) {}
    bool isEmpty() const { return top == -1; }
    bool isFull() const { return top == MAX - 1; }
    void push(int x) { if (!isFull()) arr[++top] = x; }
    int pop() { return isEmpty() ? -1 : arr[top--]; }
    int peek() const { return isEmpty() ? -1 : arr[top]; }
};

// showstack reads internal members; stack passed as const reference
void showstack(const Stack &s) {
    if (s.isEmpty()) {
        cout << "Stack is empty\n";
        return;
    }
    cout << "Stack contents (top -> bottom): ";
    for (int i = s.top; i >= 0; --i) {
        cout << s.arr[i];
        if (i > 0) cout << " ";
    }
    cout << '\n';
}

int main() {
    Stack st;
    st.push(10);
    st.push(20);
    st.push(30);

    showstack(st); // prints: Stack contents (top -> bottom): 30 20 10

    // original stack unchanged
    cout << "Top after showstack: " << st.peek() << '\n'; // 30
    return 0;
}

Notes: This requires top and arr to be accessible (public). If they are private, use one of the options below.

2. If your stack is a class with private members

Either:

  • add a public accessor that returns a copy of the internal container (or a const reference to a read-only view), or

  • declare showstack as a friend so it can read private members, or

  • implement showstack using only the public API by copying the stack (pass by value) and popping from the copy.

Example: pass-by-value (works without exposing internals):

#include <iostream>
#include <vector>
using namespace std;

class Stack {
private:
    vector<int> data;
public:
    void push(int x) { data.push_back(x); }
    int pop() { if (data.empty()) return -1; int v = data.back(); data.pop_back(); return v; }
    bool isEmpty() const { return data.empty(); }
    int size() const { return (int)data.size(); }
    int top() const { return data.empty() ? -1 : data.back(); }
};

// showstack takes a copy of the stack and pops from the copy
void showstack(Stack s) { // copy constructor invoked
    if (s.isEmpty()) {
        cout << "Stack is empty\n";
        return;
    }
    cout << "Stack contents (top -> bottom): ";
    bool first = true;
    while (!s.isEmpty()) {
        if (!first) cout << " ";
        cout << s.top();
        s.pop(); // modifies the copy only
        first = false;
    }
    cout << '\n';
}

int main() {
    Stack st;
    st.push(5);
    st.push(15);
    st.push(25);

    showstack(st); // prints: Stack contents (top -> bottom): 25 15 5

    // original stack still intact
    cout << "Top after showstack: " << st.top() << '\n'; // 25
    return 0;
}

Notes: This is the most encapsulation-friendly approach. It relies on a correct copy constructor (the default works for std::vector).

Which approach to choose

  • If internals are public or you control the class: use the const& iterator approach — it’s efficient and simple.

  • If internals are private and you want to preserve encapsulation: pass by value and pop from the copy, or add a read-only accessor that returns a const reference or a copy of the internal container.

  • If performance matters and the stack is large: prefer const& with a friend or a public read-only accessor to avoid copying.

Final tip

Always document showstack’s behavior (does it print top→bottom? does it modify the stack?) so callers know whether the original stack remains unchanged.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...