Hello friends!!!!!

Stack ADT

DATA STRUCTURES

3/12/20251 min read

#include <stdio.h>

#include <stdlib.h>

//Definition section

#define MAX 100

// Initialising the Stack structure

struct stack {

int arr[MAX];

int top;

};

typedef struct stack Stack;

// Function to initialize the stack

void initialisation(Stack* s) {

s->top = -1;

}

// Function that check whether the stack is full or not

int isFull(Stack* s) {

return s->top == MAX - 1;

}

// Function to check if the stack is empty

int isEmpty(Stack* s) {

return s->top == -1;

}

// push operation

void push(Stack* s, int value) {

if (isFull(s)) {

printf("Stack overflow!\n");

} else {

s->arr[++s->top] = value;

}

}

// pop function

int pop(Stack* s) {

if (isEmpty(s)) {

printf("Stack underflow!\n");

return -1;

} else {

int poppedValue = s->arr[s->top--];

return poppedValue;

}

}

// Function to get peek value

int peek(Stack* s) {

if (isEmpty(s)) {

printf("Stack is empty!\n");

return -1;

} else {

return s->arr[s->top];

}

}

//display - function

void display(Stack* s) {

if (isEmpty(s)) {

printf("Stack is empty!\n");

} else {

printf("Stack elements: ");

for (int i = s->top; i >= 0; i--) {

printf("%d ", s->arr[i]);

}

printf("\n");

}

}

int main() {

Stack stack;

initialisation(&stack);

push(&stack, 10);

push(&stack, 20);

push(&stack, 30);

push(&stack, 40);

push(&stack, 50);

display(&stack);

printf("Peek value is %d\n", peek(&stack));

printf("Popped element - %d\n", pop(&stack));

display(&stack);

return 0;

}