0% found this document useful (0 votes)
2 views

stack

This document contains a C program that implements a stack data structure with basic operations such as push, pop, display, and checking size. The user can interact with the stack through a menu-driven interface, allowing them to perform various operations until they choose to exit. The program includes error handling for stack overflow and underflow conditions.

Uploaded by

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

stack

This document contains a C program that implements a stack data structure with basic operations such as push, pop, display, and checking size. The user can interact with the stack through a menu-driven interface, allowing them to perform various operations until they choose to exit. The program includes error handling for stack overflow and underflow conditions.

Uploaded by

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

#include<stdio.

h>
#include<stdbool.h>

int stack[100];
int top = -1;
int size = 0;

bool isEmpty() {
if(top == -1)
return true;
else
return false;
}

bool isFull() {
if(top + 1 == size)
return true;
else
return false;
}

void push(int data) {


if(isFull())
printf("\nStack Overflow!");
else
stack[++top] = data;
}

int pop() {
int p = stack[top--];
return p;
}

void display() {
int i;
for(i = top; i >= 0; i--)
printf("\n%d", stack[i]);
}

void main() {

printf("\nEnter the size of stack: ");


scanf("%d", &size);

do {
printf("\nEnter 1 to push \nEnter 2 to pop \nEnter 3 to peek \nEnter 4 to
display the stack \nEnter 5 to check size \nEnter 6 to exit \n\nchoice: ");
int choice;
scanf("%d", &choice);

if(choice == 1) {
int data;
printf("\nEnter the data to pushed: ");
scanf("%d", &data);
push(data);
}
else if(choice == 2) {
if(isEmpty())
printf("\nStack Underflow!");
else {
int popped = pop();
printf("\nPopped element = %d", popped);
}
}
else if(choice == 3) {
printf("\nTop of stack = %d", stack[top]);
}
else if(choice == 4) {
display();
}
else if(choice == 5) {
printf("\nSize = %d", top + 1);
}
else if(choice == 6) {
return;
}
else {
printf("\nInvalid choice!!!");
}
}while(true);
}

You might also like