Pages

  • Home
  • About & Contact
  • Archive
  • C Programming Examples

25 June 2012

C program to implement Stack implementation [using Structure]

Here is the program to implement Stack in C using Structure . Stack is maintained in (LIFO) Last IFirst Out manner.So from diagram it is clear that all operations required in stack i.e Push and pop are done at the front end so we require a variable top which will give Status of current position.Initially value of top is -1 which indicates Stack is empty.Variables top and a[] are elements of Structure
C program to implement Stack using Structure
We have 2 functions push() andpop() which operates on variabletop increments and decrements respectively.In push() we have to take care that size of Stack is not full i.e top==max-1.While in pop() Stack should not underflow i.e Stack is already empty and you are trying to remove an element i.e top==-1

C program to implement Stack using Structures


#include<stdio.h>
#define max 5                                     // size of the Stack
struct stack
{
        int top,a[max];
};
void push(struct stack *p1)
{
        int x;
        if(p1->top==max-1)                  // Condition for checking If Stack is Full
       {
               printf("stack overflow\n");
               return;
        }
        p1->top++;                               //increment the top
        printf("enter a no\n");
        scanf("%d",&x);
        p1->a[p1->top]=x;                     //insert element
C program to implement Stack using Structure        printf("%d succ. pushed\n",x);
        return;
}
void pop(struct stack *p2)
{
        int y;
        if(p2->top==-1)                         // Condition for checking If Stack is Empty           
        {
               printf("stack underflow\n");
               return;
        }
        y=p2->a[p2->top];
        p2->a[p2->top]=0;
        printf("%d succ. poped\n",y);
        p2->top--;
        return;
}
void display(struct stack *p3)
{
        int i;
        if(p3->top==-1)
        {
                 printf("stack is empty\n");
                 return;
        }
        for(i=0;i<=p3->top;i++)
        {
                printf("%d\n",p3->a[i]);
        }
         return;
}
void main()
{
        struct stack s;
         int c; s.top=-1;
         do
        {
                 printf("1:push\n2:pop\n3:display\n4:exit\nchoice:");
                 scanf("%d",&c);
                 switch(c)
                 {
                        case 1:push(&s);
                        break;
                        case 2:pop(&s);
                        break;
                        case 3:display(&s);
                        break;
                        case 4:printf("pgm ends\n");
                        break;
                        default :printf("wrong choice\n");
                        break;
                 }
         }while(c!=4); 
}

Similar programs:
Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.
Thank You.
-Romil shah

No comments:

Post a Comment