Write a program to implement Stack in C

/* Write a program to implement Stack in C */

#include<stdio.h>
#define size 10
void push(int a[],int *top,int x)
{
if(*top>size-1)
printf(“\nThe stack is full……”);
else
{
a[*top]=x;
*top=*top+1;
}

}

int pop(int a[],int *top)
{
int x;
if(*top<0)
printf(“\nThe stack is empty……”);
else
{
x=a[*top];
*top=*top-1;
}
return x;
}

void display(int a[])
{
int x;
printf(“\n\nHere is the data::”);
for(x=0;x<=size-1;x++)
printf(“\n%d”,a[x]);
}

int main()
{
int s=0,i=0,x=0,d[size],disp[size],j;
int c=1;
int y=1;
while(y==1)
{
printf(“\nThis is a programm of stack……..”);
printf(“\nPress 1 for inputing data into stack. “);
printf(“\nPress 2 for outputing data from stack. “);
printf(“\nPress 3 for displaying data present in stack. “);
printf(“\nPlease select from any of the above>>”);
scanf(“%d”,&s);
switch(s)
{
case 1:
{
while(c==1)
{
printf(“\nEnter any Integer>>”);
scanf(“%d”,&x);
push(d,&i,x);
printf(“\nDo you want to insert more data then press 1>>”);
scanf(“%d”,&c);
}

}
break;

case 2:
{
printf(“\nPoping of data finished”);
while(i>-1)
{
disp[i]=pop(d,&i);
printf(“.”);
}

}
break;
case 3:
{
display(disp);

}
break;

default:
printf(“\nInvalid option……!!”);

}
c=1;
printf(“\nPress 1 to continue or press any other key to exit…”);
printf(“\nPlease…….>>”);
scanf(“%d”,&y);
}
return 0;
}

/*This programm is compiled in Dev c++ a GNU compiler used in Linux ………

I could not copy the output since the output is too long ……..

*/

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.