Skip to main content

Write a data structure program to make an array as stack.

Ans:-
#include<stdio.h>
#define MAX 10
int st[MAX],top=-1;
void push(int st[],int val);
int pop(int st[]);
void display(int st[]);
int main()
{
int val,option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
printf("\n***************");
printf("\n enter your option:");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on the stack:");
scanf("%d",&val);
push(st,val);
break;
case 2:
val = pop(st);
if(val!=-1)
printf("\n The value deleted from the stack is:%d",val);
break;
case 3:
display(st);
break;
  }
}
while(option!=5);
getch();
return 0;
}
void push(int st[],int val)
{
if(top==MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top]=val;
}
}
int pop(int st[])
{
int val;
if (top==-1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val=st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top==-1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
}
}

Comments

Popular posts from this blog

Important links

  Software Drive https://drive.google.com/drive/folders/1siws7oga-10wdlPhOdcjamlDH6wm0iE_?usp=sharing PPT Drive https://drive.google.com/drive/folders/1lNHhZ4_qlsyHrWA862DyBSvkF_J4YVp8 https://docs.google.com/document/d/1fo_4gFB4sgWfTBfZvK7qf0LP0y9ge1dH/edit

Python Practice questions

Reverse  Check substring in string  Armstrong no or not Palindrome  Table print  Power of the no Factorial