C++ Stack Exercises: Middle element of a stack (using a dynamic array)
C++ Stack: Exercise-23 with Solution
Write a C++ program to find the middle element of a stack (using a dynamic array).
Test Data:
Input the size of the stack: 8
Input some elements onto the stack:
Stack elements are: 4 7 5 3 1
Reverse the said stack elements:
Stack elements are: 1 3 5 7 4
Sample Solution:
C++ Code:
#include <iostream>
using namespace std;
// Class representing a Stack
class Stack {
private:
int* arra; // Dynamic array to store elements
int top; // Index of top element
int capacity; // Maximum size of the stack
public:
// Constructor to initialize the stack with a given size
Stack(int size) {
arra = new int[size]; // Allocate memory for the stack array
top = -1; // Initialize top index to -1 (empty stack)
capacity = size; // Set the maximum capacity of the stack
}
// Function to push an element onto the stack
bool push(int x) {
if (isFull()) {
cout << "Stack overflow" << endl; // Display overflow message if the stack is full
return false;
}
// Add element to array by incrementing top index
arra[++top] = x;
return true;
}
// Function to pop an element from the stack
int pop() {
if (isEmpty()) {
cout << "Stack underflow" << endl; // Display underflow message if the stack is empty
return 0;
}
// Return the top element and decrement the index of the top element
return arra[top--];
}
// Function to peek the top element of the stack without removing it
int peek() {
if (isEmpty()) {
cout << "Stack is empty" << endl; // Display message if the stack is empty
return 0;
}
// Return the top element without modifying the top index
return arra[top];
}
// Function to check if the stack is empty
bool isEmpty() {
// Stack is empty if top index is -1
return (top < 0);
}
// Function to check if the stack is full
bool isFull() {
// Stack is full if top index is equal to capacity - 1
return (top >= capacity - 1);
}
// Function to display the elements of the stack
void display() {
if (top < 0) {
cout << "Stack is empty" << endl; // Display message if the stack is empty
return;
}
cout << "\nStack elements are: ";
for (int i = top; i >= 0; i--)
cout << arra[i] << " "; // Display the elements of the stack
cout << endl;
}
};
// Function to find the middle element of the stack
int get_middle(Stack stk) {
int length = 0;
// Create a copy of the original stack
Stack temp(stk);
while (!temp.isEmpty()) {
length++;
temp.pop();
}
int mid = length / 2;
for (int i = 0; i < mid; i++) {
// Remove elements before the middle element
stk.pop();
}
// Get the middle element
int middle_element = stk.pop();
for (int i = mid; i < length - 1; i++) {
// Restore the original stack
stk.push(stk.pop());
}
return middle_element;
}
int main() {
int size;
cout << "Input the size of the stack: ";
cin >> size;
Stack stk(size);
cout << "\nInput some elements onto the stack:";
stk.push(1);
stk.push(3);
stk.push(5);
stk.push(7);
stk.push(9);
stk.push(10);
stk.push(11);
stk.display();
int z = get_middle(stk); // Get the middle element of the stack
cout << "Middle element = " << z;
cout << "\n\nRemove one element from the said stack:";
stk.pop();
stk.display();
z = get_middle(stk); // Get the middle element after removing one element
cout << "\nMiddle element = " << z;
return 0;
}
Sample Output:
Input the size of the stack: 8 Input some elements onto the stack: Stack elements are: 11 10 9 7 5 3 1 Middle element = 7 Remove one element from the said stack: Stack elements are: 10 9 7 5 3 1 Middle element = 5
Flowchart:
CPP Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: Reverse a stack (using a dynamic array) elements.
Next C++ Exercise: Implement a stack using a vector with push, pop operations.
What is the difficulty level of this exercise?
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://www.w3resource.com/cpp-exercises/stack/cpp-stack-exercise-23.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics