|
| 1 | +#include <iostream> // Include the input-output stream library |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +#define MAX 5 // Define the maximum size of the stack |
| 5 | + |
| 6 | +// Declare the stack and the top variable |
| 7 | +int stack[MAX], top = -1; |
| 8 | + |
| 9 | +// Function to check if the stack is empty |
| 10 | +bool isEmpty() { |
| 11 | + // If top is -1, stack is empty |
| 12 | + return top == -1; |
| 13 | +} |
| 14 | + |
| 15 | +// Function to check if the stack is full |
| 16 | +bool isFull() { |
| 17 | + // If top is equal to the last index, stack is full |
| 18 | + return top == MAX - 1; |
| 19 | +} |
| 20 | + |
| 21 | +// Function to push an element onto the stack |
| 22 | +void push(int x) { |
| 23 | + // Check if the stack is full |
| 24 | + if (isFull()) { |
| 25 | + cout << "The stack is full." << endl; // Display a message if the stack is full |
| 26 | + } else { |
| 27 | + top++; // Increment the top index |
| 28 | + stack[top] = x; // Add the new element to the top of the stack |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Function to pop an element from the stack |
| 33 | +void pop() { |
| 34 | + // Check if the stack is empty |
| 35 | + if (isEmpty()) { |
| 36 | + cout << "The stack is empty." << endl; // Display a message if the stack is empty |
| 37 | + } else { |
| 38 | + cout << "Popped element: " << stack[top] << endl; // Display the popped element |
| 39 | + top--; // Decrement the top index to remove the top element |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Function to peek the top element of the stack |
| 44 | +int peek() { |
| 45 | + // Check if the stack is empty |
| 46 | + if (isEmpty()) { |
| 47 | + cout << "The stack is empty." << endl; // Display a message if the stack is empty |
| 48 | + return -1; // Return -1 if the stack is empty |
| 49 | + } else { |
| 50 | + return stack[top]; // Return the top element of the stack |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Function to display the elements of the stack |
| 55 | +void display() { |
| 56 | + // Check if the stack is empty |
| 57 | + if (isEmpty()) { |
| 58 | + cout << "The stack is empty." << endl; // Display a message if the stack is empty |
| 59 | + } else { |
| 60 | + cout << "Stack elements: " << endl; // Display a message indicating stack elements |
| 61 | + // Loop through the stack from top to bottom |
| 62 | + for (int i = top; i >= 0; i--) { |
| 63 | + cout << stack[i] << endl; // Print each element of the stack |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Main function |
| 69 | +int main() { |
| 70 | + push(1); // Push 1 onto the stack |
| 71 | + push(2); // Push 2 onto the stack |
| 72 | + push(3); // Push 3 onto the stack |
| 73 | + push(4); |
| 74 | + display(); // Display the stack elements |
| 75 | + pop(); // Pop the top element from the stack |
| 76 | + display(); // Display the stack elements after popping |
| 77 | + cout << "Top element: " << peek() << endl; // Display the top element of the stack |
| 78 | + return 0; // Return 0 to indicate successful execution |
| 79 | +} |
| 80 | + |
0 commit comments