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

Lab Program 3

This document describes designing and implementing a menu-driven C program for stack operations on integers using an array to represent the stack. The operations include: (1) pushing an element onto the stack, (2) popping an element from the stack, (3) handling overflow and underflow conditions, and (4) displaying the status of the stack. The program uses appropriate functions for each operation and a while loop menu to continuously prompt the user for operation choices until exiting.

Uploaded by

shreya gowda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab Program 3

This document describes designing and implementing a menu-driven C program for stack operations on integers using an array to represent the stack. The operations include: (1) pushing an element onto the stack, (2) popping an element from the stack, (3) handling overflow and underflow conditions, and (4) displaying the status of the stack. The program uses appropriate functions for each operation and a while loop menu to continuously prompt the user for operation choices until exiting.

Uploaded by

shreya gowda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Design, Develop and Implement a menu driven Program in C for the following

operations on
STACK of Integers (Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate Overflow and Underflow situations on Stack
d. Display the status of Stack
e. Exit
Support the program with appropriate functions for each of the above operations

Algorithm:
Step 1: Start.
Step 2: Initialize stack size MAX and top of stack -1.
Step 3: Push integer element on to stack and display the contents of the
stack. If stack is full give a message as ‘Stack Overflow’.
Step 4: Pop elements from stack along with display the stack contents.
If stack is empty give a message as ‘Stack Underflow’.
Step 5: Stop.

Program 3:
#include<stdio.h>
#include<stdlib.h>
#define max 4
int st[max],top=-1,i,ch,elm,flag;
void push()
{
if(top==(max-1))
printf("Stack Overflow\n");
else
{
printf("Enter the element\n");
scanf("%d",&elm);
top++;
st[top]=elm;
}}
void pop()
{
if(top==-1)
printf("Stack Underflow\n");
else
{
elm=st[top];
top--;
printf("Popped element=%d\n",elm);
}}
void display()
{
if(top==-1)
printf("Stack is empty\n");
else
{
printf("Stack elements:\n");
for(i=top;i>=0;i--)
printf("%d\n",st[i]);
}}
void main()
{
while(1)
{
printf("\n\tMENU\n1.Push\t2.Pop\t3.Display\t4.EXIT\nEnter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Invalid choice\n");
}}}

You might also like