w3resource

C++ Stack Exercises: Find the middle element(s) of a stack (using a vector)

C++ Stack: Exercise-27 with Solution

Write a C++ program to find the middle element(s) of a stack (using a vector).

Test Data:
Create a stack object:
Input and store (using vector) some elements onto the stack:
Stack elements are: 1 3 2 6 5 -1
Middle element(s) of the said stack elements:
2 6

Sample Solution:

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Stack {
private:
    vector<int> elements; // Vector to store elements

public:
    void push(int element) {
        elements.push_back(element); // Add element to the end of the vector
    }

    void pop() {
        if (elements.empty()) {
            cout << "Stack is full" << endl; // Display full message if the stack is empty
        } else {
            elements.pop_back(); // Remove the last element from the vector
        }
    }

    int top() {
        if (elements.empty()) {
            cout << "Stack is empty" << endl; // Display empty message if the stack is empty
            return 0;
        } else {
            return elements.back(); // Return the last element in the vector
        }
    }

    bool empty() {
        return elements.empty(); // Check if the vector is empty
    }

    void display() {
        vector<int> v = elements;
        if (v.empty()) {
            cout << "Stack is empty" << endl; // Display empty message if the stack is empty
            return;
        }
        cout << "Stack elements are: ";
        for (int i = 0; i < v.size(); i++) {
            cout << v[i] << " "; // Display the elements of the stack
        }
        cout << endl;
    }

    // Function to get the middle element(s) of the stack
    vector<int> getMiddleElements() {
        vector<int> result;
        int size = elements.size();
        if (size % 2 == 0) {
            result.push_back(elements[size / 2 - 1]); // Add middle element if stack size is even
        }
        result.push_back(elements[size / 2]); // Add middle element if stack size is odd
        return result;
    }
};

int main() {
    Stack stack;
    cout << "Create a stack object:\n"; // Initialize a stack
    cout << "\nInput and store (using vector) some elements onto the stack:\n";
    stack.push(1);
    stack.push(3);
    stack.push(2);
    stack.push(6);
    stack.push(5);
    stack.push(-1);
    stack.display();
    cout << "\nMiddle element(s) of the said stack elements:\n";
    vector<int> middleElements = stack.getMiddleElements();
    for (int i = 0; i < middleElements.size(); i++) {
        cout << middleElements[i] << " "; // Display middle element(s)
    }
    cout << "\n\nInput one more element:\n";
    stack.push(0);
    stack.display();
    cout << "\nMiddle element(s) of the said stack elements:\n";
    middleElements = stack.getMiddleElements();
    for (int i = 0; i < middleElements.size(); i++) {
        cout << middleElements[i] << " "; // Display middle element(s)
    }
    cout << endl;
    return 0;
}

Sample Output:

Create a stack object:

Input and store (using vector) some elements onto the stack:
Stack elements are: 1 3 2 6 5 -1 

Middle element(s) of the said stack elements:
2 6 

Input one more element:
Stack elements are: 1 3 2 6 5 -1 0 

Middle element(s) of the said stack elements:
6 

Flowchart:

Flowchart: Find the middle element(s) of a stack (using a vector).
Flowchart: Find the middle element(s) of a stack (using a vector).
Flowchart: Find the middle element(s) of a stack (using a vector).

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Reverse a stack (using a vector) elements.
Next C++ Exercise: Implement a stack using a Deque with push, pop operations.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/cpp-exercises/stack/cpp-stack-exercise-27.php