#include <iostream>
#define MAX 100 // Define the maximum size of the stack
using namespace std;
class Stack {
private:
int top;
int arr[MAX]; // Array to store stack elements
public:
Stack() {
top = -1; // Initialize top of the stack
}
// Function to add an element to the stack
void push(int value) {
if (top == MAX - 1) {
cout << "Stack overflow! Cannot push " << value << endl;
} else {
arr[++top] = value;
cout << value << " pushed to stack" << endl;
}
}
// Function to remove an element from the stack
void pop() {
if (top == -1) {
cout << "Stack underflow! Cannot pop" << endl;
} else {
cout << arr[top--] << " popped from stack" << endl;
}
}
// Function to get the top element of the stack
void peek() {
if (top == -1) {
cout << "Stack is empty!" << endl;
} else {
cout << "Top element is " << arr[top] << endl;
}
}
// Function to check if the stack is empty
int isEmpty() {
return top == -1;
}
// Function to check if the stack is full
int isFull() {
return top == MAX - 1;
}
};
int main() {
Stack stack; // Create a stack object
int n;
char choice;
// Push elements into the stack
do {
cout << "\nEnter item to push into the stack: ";
cin >> n;
stack.push(n); // Push the entered item onto the stack
cout << "Do you want to push another item into the stack (y/n)? ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
// Peek at the top element
stack.peek();
// Pop the top element
stack.pop();
stack.pop();
// Peek at the new top element
stack.peek();
// Check if the stack is empty or full
if (stack.isEmpty()) {
cout << "Stack is empty" << endl;
} else {
cout << "Stack is not empty" << endl;
}
if (stack.isFull()) {
cout << "Stack is full" << endl;
} else {
cout << "Stack is not full" << endl;
}
return 0;
}
Method 2
#include <iostream.h>
#include<conio.h>
#define MAX 100
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
Stack() { top = -1; }
void push(int x);
int pop();
int peek();
int isEmpty();
int isFull();
};
void Stack::push(int x) {
if (isFull()) { // Check for stack overflow using isFull()
cout << "Stack Overflow\n";
} else {
a[++top] = x; // Increment top and add the new element
cout << x << " pushed into stack\n";
}
}
int Stack::pop() {
if (isEmpty()) { // Check for stack underflow
cout << "Stack Underflow\n";
return -1; // Return -1 to indicate an error
} else {
return a[top--]; // Return the popped value
}
}
int Stack::peek() {
if (isEmpty()) { // Check if the stack is empty
cout << "Stack is Empty\n";
return -1; // Return -1 to indicate an error
} else {
return a[top]; // Return the top value
}
}
int Stack::isEmpty() {
return (top < 0);
}
int Stack::isFull() {
return (top >= (MAX - 1));
}
int main() {
clrscr();
Stack s;
int n;
char choice;
do {
cout << "\nEnter item to push into the stack: ";
cin >> n;
s.push(n); // Push the entered item onto the stack
cout << "Do you want to push another item into the stack (y/n)? ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
// Ensure there's at least one element before popping
if (!s.isEmpty()) {
cout << s.pop() << " Popped from stack\n";
}
// Print top element of stack after popping
cout << "Top element is: " << s.peek() << endl;
// Print all elements in stack:
cout << "Elements present in stack: ";
while (!s.isEmpty()) {
cout << s.peek() << " ";
s.pop();
}
cout << endl;
// Check if the stack is full
if (s.isFull()) {
cout << "Stack is full\n";
} else {
cout << "Stack is not full\n";
}
getch();
return 0;
}