Thursday, 6 October 2016

Implement the stack operations

Implement the stack operations for the stack of books, for the followings:

a) Push 5 book information.

b) Pop 2 book information.

c) Peep the 1st book.

d) Display all books from stack.

#include<stdio.h>
#define max 5
struct stack
{
            int stc[max];
        int top;
}s;

/* ~~~~~~~~~~~ PUSH~~~~~~~~~*/
void push()
{
            int x;
            if(s.top==(max-1))
            {
                        printf("The stack is overflow :");
                return;
            }
            else
            {
                        printf("Enter the element you want to stack=");
                        scanf("%d",&x);
                        s.top=s.top+1;
                        s.stc[s.top]=x;

            }
}
/*~~~~~~~~~~~~POP~~~~~~~~~~~~*/
void pop()
{
        int a;
            a=s.stc[s.top];
            if(s.top==-1)
            {
                        printf("The stack is underflow on pop :");
      
            }

            else
            {
                        s.top=s.top-1;
                        printf("The popped element is= %d",a);
            }
}
/*~~~~~~~~~~~PEEP~~~~~~~~~*/
void peep()
{
            int i,a;
            printf("Enter the position of element you want to peep from top :");
            scanf("%d",&i);
            /*if((s.top-i)+1<=0)
            {
                        printf("The stack is underflow");
                return;
            }
            else
            {
                        if(i==s.top)
                        {
                                    printf("The top most element is=",s.stc[s.top]);
                        }
                        else
                        {
                                    printf("The elemnet you peeped is=",s.stc[s.top-i+1]);
                        }
            }*/
        if((s.top-i+1)<=0)
            {
                        printf("The stack is underflow on pop :");
            }
            else
            {
                        a = s.stc[s.top-i+1];
                        printf("The searched elemement is:%d",a);
            }
        
}
/*~~~~~~~~~~DISPLAY~~~~~~~~~~~*/
void display()
{
            int i;
            if(s.top==-1)
            {
                        printf("Stack is empty :");
             
            }
            else
            {
                        for(i=s.top;i>=0;i--)
                {   
                        printf("value is :");
                                    printf("%d",s.stc[i]);
                        printf("\n");
                        }
            }         
}
/*~~~~~~~~~~ MAIN~~~~~~~~*/
void main()
{
            int b,i;
            s.top=-1;
            do
            {
                        printf("\n..............................................\n");
                        printf("Enter your choice\n");
                        printf("Enter 1 for push\n");
                        printf("Enter 2 for pop\n");
                        printf("Enter 3 for peep\n");
                        printf("Enter 4 for display\n");
                        printf("\n..............................................\n");
                        scanf("%d",&i);
                        switch(i)
                        {
                                    case 1 : push();
                                                 break;
                                    case 2 : pop();
                                                 break;
                                    case 3 : peep();
                                                 break;
                                    case 4 : display();
                                                 break;
                                    default : printf("You entered wrong choice :");
                        }
                        printf("Enter 0 to continue and enter 9 for exit :");
                        scanf("%d",&b);
            }while(b==0);
}

 Output:-