0% found this document useful (0 votes)
6 views3 pages

C Stack Implementation in C Language

This document contains a C program that implements a stack data structure with basic operations: push, pop, and display. The program uses an array to store stack elements and provides a menu-driven interface for user interaction. It includes error handling for stack overflow and underflow conditions.

Uploaded by

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

C Stack Implementation in C Language

This document contains a C program that implements a stack data structure with basic operations: push, pop, and display. The program uses an array to store stack elements and provides a menu-driven interface for user interaction. It includes error handling for stack overflow and underflow conditions.

Uploaded by

nandinipatil4748
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<process.h>
#include<stdlib.h>

#define MAX 5 //Maximum number of elements that can be stored

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;
while(1) //infinite loop, will end when choice will be 4
{
printf("\n*** Stack Menu ***");
printf("\n\[Link]\[Link]\[Link]\[Link]");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}
void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
}

void pop()
{
if(top= = -1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
}

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

You might also like