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

React Js interview questions

 

What is Increament or decrement operator?

They are (++) increment operator in c. (--)decrement operator in c. These two operators are called unary operator as they require only one operand.The operand should be a variable not a constant. * (++) operator :- Add one to the value of operand. *(--) operator :- substract one to the value of operand. # These operators are used in two form 1 pastfix form 2 prefix form          

Digital computer v/s Analog computer

Analog computer don't have ability of digital computer to store data in large quantities,nor do they have the comprehensive logical facilities afforded by programming digital machine.although the arithmetic function perform by the computing unit are more complex in analogue machine than in digital system. Analog machine are design for specific applications but most electrical and electronic analog computer provide a number of different computing devices which can be connected together via plug board or provide different methods of operations for special problem. In digital machine number to be stored to be used in same calculations were represented by physical quantities such as electrical voltage.