#include <bits/stdc++.h> // Includes standard libraries like iostream for input/output
using namespace std; // Allows direct use of standard namespace elements (e.g., cout, endl)
// Custom Stack class implementation
int *arr; // Pointer to the array that will store stack elements
int limit; // Acts as the 'top' pointer, indicating the index of the top element
int maxSize; // Stores the maximum capacity of the stack
// Constructor to initialize the stack
this->maxSize = maxSize; // Set the maximum size for the stack
arr = new int[maxSize]; // Dynamically allocate an array of given size
limit = -1; // Initialize limit to -1, indicating an empty stack
// Function to add an element to the stack
// Check for stack overflow
if(limit == maxSize-1) { // If limit has reached the last valid index
cout << "Stack overflow!" << endl; // Print error message
limit++; // Increment limit to point to the next available slot
arr[limit] = data; // Place the data at the new top position
// Function to remove an element from the stack
// Check for stack underflow
if(limit == -1) { // If limit is -1, the stack is empty
cout << "Stack underflow!" << endl; // Print error message
limit--; // Decrement limit, effectively removing the top element
// Function to check if the stack is empty
// Returns true if limit is less than 0 (i.e., -1), false otherwise
return (limit < 0) ? true : false;
// Function to get the current number of elements in the stack
// Since limit is 0-indexed, limit + 1 gives the count of elements
// Function to get the top element of the stack without removing it
// Returns -1 if stack is empty, otherwise returns the element at arr[limit]
return (limit < 0) ? -1 : arr[limit];
// Creation of stack instance with a maximum size of 10
s.push(10); // Add 10 to the stack
s.push(61); // Add 61 to the stack (61 is now at the top)
cout << "Current size of stack : " << s.size() << endl; // Output: 2
cout << "Top element : " << s.top() << endl; // Output: 61
s.pop(); // Remove the top element (61 is removed, 10 becomes the top)
cout << "Current size of stack : " << s.size() << endl; // Output: 1
cout << "Top element : " << s.top() << endl; // Output: 10
// Check if stack is empty
if(s.isEmpty()) { // Condition: s.isEmpty() is false
cout << "Stack is empty!" << endl;
cout << "Stack is not empty!" << endl; // Output: Stack is not empty!
s.pop(); // Remove the last element (10 is removed, stack becomes empty)
// Check if stack is empty again
if(s.isEmpty()) { // Condition: s.isEmpty() is true
cout << "Stack is empty!" << endl; // Output: Stack is empty!
cout << "Stack is not empty!" << endl;
cout << "Current size of stack : " << s.size() << endl; // Output: 0
return 0; // Indicate successful program execution