Sunday, 25 August 2013

Complete stack program using switch case:


#include<stdio.h>
#include<conio.h>
#define max_size 40
typedef struct stack
{
        int element[max_size];
        int tos;
        }stack;
       
        void init_stack(stack *);
        void push(stack *,int item);
        int pop(stack *);
        void display(stack *);
       
        int main()
        {
            stack s;
            int i;
            int ch;
            init_stack(&s);
            push(&s,1);
            push(&s,2);
            push(&s,3);
            push(&s,4);
            push(&s,5);
           
            while(ch!=4)
            {
                   printf("\n press 1 to push \n");
                   printf("press 2 to pop \n");
                   printf("press 3 to display \n");
                   printf("press 4 to quit \n");
                   printf("enter your choice: \n");
                   scanf("%d",&ch);
                   switch(ch)
                   {
                       case 1:
                       printf("\n enter the element to be pushed \n");
                       scanf("%d",&i);
                       push(&s,i);
                       break;
                      
                       case 2:
                       i=pop(&s);
                       printf("\n you have popped value %d \n",i);
                       break;
                      
                       case 3:
                       display(&s);
                       break;
                      
                       case 4:
                       break;
                      
                       default:
                       printf("\n wrong choice try again \n");
                      }
                  }
                 getch();
                }
                                                         
                void init_stack(stack *s)
                {
                      s->tos=-1;
                }
                                                              
                void push(stack *s,int item)
                {
                     if(s->tos==max_size-1)
                     printf("overflow situation \n");
                     else
                     {
                           s->tos=s->tos+1;
                           s->element[s->tos]=item;
                      }
                 }
                                                                       
                int pop(stack *s)
                 {
                      int data;
                      if(s->tos==-1)
                      {
                          printf("underflow situation \n");
                          return NULL;
                       }
                       else
                       {
                           data=s->element[s->tos];
                           s->tos=s->tos-1;
                           return(data);
                        }
                  }
                                                                                             
                  void display(stack *s)
                  {
                      int i;
                      if(s->tos==-1)
                      printf("stack is empty, nothing to display \n");
                      else
                       {
                           for(i=s->tos;i>=0;i--)
                           printf("%d ",s->element[i]);
                      }
                   }
                                                                                             
                                                               
                                        
           

No comments:

Post a Comment

About the author

Scholar Ratan pal singh is a Google scholar, a man with innovative ideas and a nothing is impossible attitude, is a tech-lover.
He works as a independent researcher and freelance website and software developer.
Member of IEEE comsoc society, Green technology society and has published research paper and codes in many international journals and opensource.

"Failure is the pillar of success and Honesty with oneself is the only true way of achieving self-confidence."

Translate

Pageviews