Wednesday, 8 August 2012

Stack using C programming

#include<stdio.h>
#include<conio.h>
void add();
void del();
void display();
void exit();
int n,a[10],i,j,k=-1;
void main()
{
clrscr();
do
{
printf("\n1.ADD");
printf("\n2.DELETE");
printf("\n3.DISPLAY");
printf("\n0.EXIT");
printf("\n\nEnter your choice:\t");
scanf("%d",&n);
switch(n)
{
case 1:add();
break;
case 2:del();
break;
case 3:display();
break;
case 0:
break;
}
}
while(n!=0);
}
void add()
{
if(k<9)
{
printf("\n enter the elements to be inserted...\n");
scanf("%d",&i);
k++;
a[k]=i;
}
else
{
printf("\n stack is fill!");
}
}
void del()
{
if(k==-1)
{
printf("\n stack is empty!");
}
else
{
i=a[k];
k--;
printf("\n deleted element is...%d",i);
}
}
void display()
{
if(k==-1)
{
printf("\n no value");
}
else
{
int j=k;
printf("\n element stored as:\n");
for(j=k;j>=0;j--)
{
printf("%d",a[j]);
printf("\n");
}
}
}

No comments:

Post a Comment

There was an error in this gadget