Monday 21 October 2013

Program In C For Stack Implementation Using Array

#include<stdio.h>
#include<conio.h>
void main()
{ int stack[10],max=10,i,ch,c,item,top=-1,c2;
clrscr();
again:
c=1;
printf("STACK MENU: ");
printf("\n1.Insertion(Press 1)\n2.Deletion(Press 2)\n3.Display Stack(Press 3):\n");
scanf("%d",&ch);
if(ch==1)
{ while(c==1)
{ if(top==max)
{ printf("\nOverflow !!!! ");
break; }
printf("\nEnter The Element To Be Inserted In Stack: ");
scanf("%d",&item);
top++;
stack[top]=item;
printf("\nStack After Insertion Is: ");
for(i=0;i<=top;i++)
{ printf("%d\t",stack[i]); }
printf("\nWant To Insert More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c);
}}
else if(ch==2)
{ while(c==1)
{ if(top==-1)
{ printf("\nUnderFlow !!!!");
break; }
top--;
printf("Stack After Deletion Is: ");
for(i=0;i<=top;i++)
{ printf("%d\t",stack[i]); }
printf("\nWant To Delete More ?? Press 1 for Yes and 2 For No: \n");
scanf("%d",&c); }}
else if(ch==3)
{ if(top==-1)
printf("Stack Is Empty !!! ");
else
{ printf("Stack Is: ");
for(i=0;i<=top;i++)
{ printf("%d\t",stack[i]); }
}}
else
printf("Invalid Choice !!!!!! ");
printf("\n1.Back To Main Menu(Press 1)\n2.Exit(Press Any Key):\n");
scanf("%d",&c2);
if(c2==1)
goto again;
}

No comments:

Post a Comment