Monday, 26 March 2018

Stack representation Using Array

Stack is a basic concept in Data Structure , It totally work on-
L: lAST
I: IN
F: FIRST
O: OUT

That means the last inserted element would be deleted first . So, here the program for the same

/* pre processor used throughout the program*/
#include<stdio.h>
#include<conio.h>
#define MAX 50

/*Some variable declared that we gonna used in the program*/
int stack[MAX],n,top,vk,i;

void push();     /*function for push the value in stack*/
void pop();       /*function for pop value from stack*/
void dsp();       /*function to display element in the stack*/

void main()
{
int po,pu;
clrscr();
top=-1;
printf("\nSpecify size for your stack:- ");
scanf("%d",&n);
printf("\n\nEnter no. of element you want to push:- ");
scanf("%d",&pu);
for(i=0;i<pu;i++)
{
push();
}
printf("\n\nDo you want to pop element (1--> Yes and 0-->No):- ");
scanf("%d",&po);
if(po==1)
{
pop();
}
dsp();
getch();
}

void push()
{
if(top>=n-1)
{
printf("\nStack is over flow");
}
else
{
printf("\n\tEnter a value to push:- ");
scanf("%d",&vk);
top++;
stack[top]=vk;
}
}

void pop()
{
if(top<=-1)
{
printf("\nStack is empty");
}
else
{
printf("\nPopped value:- %d",stack[top]);
top--;
}
}

void dsp()
{
if(top>=0)
{
for(i=top;i>=0;i--)
printf("\nOutput:- %d",stack[i]);
}
else
{
printf("\nSTACK IS EMPTY");
}
}


No comments:

Post a Comment