0% found this document useful (0 votes)
2 views2 pages

Stack Operations

This document contains a C program that implements a stack data structure with operations to push, pop, and display elements. It defines a maximum stack size and handles stack overflow and underflow conditions. The main function provides a user interface for performing stack operations in a loop until the user chooses to exit.

Uploaded by

dchebelyon2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Stack Operations

This document contains a C program that implements a stack data structure with operations to push, pop, and display elements. It defines a maximum stack size and handles stack overflow and underflow conditions. The main function provides a user interface for performing stack operations in a loop until the user chooses to exit.

Uploaded by

dchebelyon2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#define MAX 100 // Maximum size of the stack

int stack[MAX], top = -1;

// Function to push an element onto the stack


void push(int value) {
if (top == MAX - 1) {
printf("Stack Overflow!\n");
return;
}
stack[++top] = value;
printf("%d pushed onto the stack.\n", value);
}

// Function to pop an element from the stack


void pop() {
if (top == -1) {
printf("Stack Underflow!\n");
return;
}
printf("%d popped from the stack.\n", stack[top--]);
}

// Function to display the stack elements


void display() {
if (top == -1) {
printf("Stack is empty.\n");
return;
}
printf("Stack elements: ");
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}

int main() {
int choice, value;
while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}

You might also like