C program to implement stacks
// C Program to demonstrate how to Implement a Stack
#include <stdio.h>
#include <stdbool.h>
// Define the maximim capacity of the stack
#define MAX_SIZE 100
// Define a structure for the stack
typedef struct {
// Array to store stack elements
int arr[MAX_SIZE];
// Index of the top element in the stack
int top;
} Stack;
// Function to initialize the stack
void initialize(Stack *stack) {
// Set top index to -1 to indicate an empty stack
stack->top = -1;
}
// Function to check if the stack is empty
bool isEmpty(Stack *stack) {
// If top is -1, the stack is empty
return stack->top == -1;
}
// Function to check if the stack is full
bool isFull(Stack *stack) {
// If top is MAX_SIZE - 1, the stack is full
return stack->top == MAX_SIZE - 1;
}
// Function to push an element onto the stack
void push(Stack *stack, int value) {
// Check for stack overflow
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
// Increment top and add the value to the top of the stack
stack->arr[++stack->top] = value;
printf("Pushed %d onto the stack\n", value);
}
// Function to pop an element from the stack
int pop(Stack *stack) {
// Check for stack underflow
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
// Return the top element
int popped = stack->arr[stack->top];
// decrement top pointer
stack->top--;
printf("Popped %d from the stack\n", popped);
// return the popped element
return popped;
}
// Function to peek the top element of the stack
int peek(Stack *stack) {
// Check if the stack is empty
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
// Return the top element without removing it
return stack->arr[stack->top];
}
int main() {
Stack stack;
// Initialize the stack
initialize(&stack);
// Push elements onto the stack and print the stack after each
push
push(&stack, 3);
printf("Top element: %d\n", peek(&stack));
push(&stack, 5);
printf("Top element: %d\n", peek(&stack));
push(&stack, 2);
printf("Top element: %d\n", peek(&stack));
push(&stack, 8);
printf("Top element: %d\n", peek(&stack));
// Pop elements from the stack and print the stack after each pop
while (!isEmpty(&stack)) {
printf("Top element: %d\n", peek(&stack));
printf("Popped element: %d\n", pop(&stack));
}
return 0;
}