Tweet
/* Program to implement stack using array */
#include<stdio.h>
void push(int a[],int n,int *top,int val)
{
if(*top>=n-1)
{
printf("Overflow !!! \n\n");
return;
}
a[++(*top)]=val;
}
int pop(int a[],int *top)
{
if(*top==-1)
{
printf("Underflow !!! \n\n");
return (-999);
}
return(a[(*top)--]);
}
void display(int a[],int *top)
{
int i;
if(*top==-1) printf("\nStack Empty !! \n");
else
{
printf("\nStack : ");
for(i=*top;i>=0;i--)
printf("%4d",a[i]);
}
}
main()
{
int n,r,val,top=-1;
printf("\nProgram to implement stack : \n");
printf("\nEnter array capacity : ");
scanf("%d",&n);
int a[n];
int ch;
do
{
printf("\n[1] Push :");
printf("\n[2] Pop : ");
printf("\n[3] Display : ");
printf("\n[4] Quit : ");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter value to push : ");
scanf("%d",&val);
push(a,n,&top,val);
display(a,&top);
break;
case 2:
r=pop(a,&top);
if(r==-999)
printf("\nStack empty : ");
else
{
printf("\nDeleted element : %d",r);
display(a,&top);
}
break;
case 3:
display(a,&top);
break;
case 4: break;
default: printf("\nEnter correct choice ... ");
break;
}
}while(ch!=4);
printf("\n\n");
}
Stack using array and pointer in C
Posted by
LAHAUL SETH
~
Stack using array and pointer in C
2011-11-29T11:00:00+05:30
LAHAUL SETH
Programming in C
|
Stack Implementation
|
Comments
Stack using array and pointer in C
2011-11-29T11:00:00+05:30
LAHAUL SETH
Programming in C
|
Stack Implementation
|