Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Program Of Stack in C-Language




In this C program we will perform operations over Stack(i.e: Push Operation, Pop Operation and Display Operation). The Choice(i.e: Push,Pop or Display) will be made by the user and The Number will be taken from the user(i.e: For Push Operation).color>

input:

The Choice(i.e Push, Pop or Display) & The Number (integers) (i.e. for Push Operations) (15,10,128 etc.)

output:

The Operations will be excecuted as choosen by the user.

CODE---->



#include
#include
#define MAXSIZE 10
int s[MAXSIZE],top=-1;
void push(int x)
{
if(top==MAXSIZE-1)
printf("\n Stack Overflow.");
else
{
top++;
s[top]=x;
}
}
void pop()
{
int temp;
if(top==-1)
printf("\n Stack Underflow.");
else
{
temp=s[top];
printf("\n %d is Popped from Stack.",temp);
top--;
}
}
void display()
{
int i;
printf("\n The Stack Elements are...\n");
if(top==-1)
{
printf("\n No Elements to display.");
}
else
{
for(i=top;i>=0;i--)
printf(" %d ",s[i]);
}
}
int main()
{
int choice,x;
while(1)
{
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\n Please, Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\n Enter Element : ");
scanf("%d",&x);
push(x);
break;
case 2 : pop();
break;
case 3 : display();
break;
case 4 : exit(0);
default : printf("\n Wrong Choice.");
}

}
}





Don't just read, write it, run it.....color>

RESULT:color>





This post first appeared on ProgramJoy.blogspot.com, please read the originial post: here

Share the post

Program Of Stack in C-Language

×

Subscribe to Programjoy.blogspot.com

Get updates delivered right to your inbox!

Thank you for your subscription

×