Implement Stack using array. ( 10 Marks, Mumbai University)
Implement stack using array.
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#define MAX 5
struct stack
{
int a[MAX];
int top;
}s;
void push( int, x);
void pop();
void display();
void main()
{
int n, ch;
clrscr();
s.top=-1; // Initially Stack is Empty
printf(“\n\t 1.push \n\t 2.pop \n\t 3.display \n\t 4.exit”);
while(1)
{
printf(“\n\t Enter your choice:”);
scanf(“%d” ,&ch);
switch(ch)
{
case 1 : printf(“\n\t Enter number to be pushed:”);
scanf(“%d” ,&n);
push(n);
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : exit(0);
default : printf(“\n\t Invalid choice.”);
}
}
getch();
}
void push(int x)
{
if (s.top==MAX-1)
printf(““\n\t The stack is full.”);
else
s.a[++s.top]=x;
}
void pop()
{
if(s.top==-1)
printf(“\n\t The stack is empty.”);
else
{
printf(“\n\t The element deleted is %d” , s.a[s.top]);
s.top--;
}
}
void display()
{
if(s.top==-1)
printf(“\n\t The stack is empty.”);
else
{
for(int i=s.top; i>=0; i--)
{
printf(“%d \t” , s.a[i]);
}
}
}
No comments: