Wednesday, 20 July 2016

Stack



#include<stdio.h>
#define SIZE 5
stack[SIZE];
int top=-1;
void push();
void pop();
void peep();
void change();
void display();

int main()
{
    int i=-1,n;
    do
        {
            printf("\n Menu ");
            printf("\n=======\n");
            printf("\n 1.Push");
            printf("\n 2.Pop");
            printf("\n 3.Peep");
            printf("\n 4.Change");
            printf("\n 5.Display");
            printf("\n 6.Exit \n");

            printf("\n Enter Your Choise :");
            scanf("%d",&n);

            switch(n)
                {
                case 1 :
                    {
                        push();
                        display();
                        break;
                    }
                case 2 :
                    {
                        pop();
                        display();
                        break;
                    }
                case 3 :
                    {
                        peep();
                        break;
                    }
                case 4 :
                    {
                        change();
                        display();
                    }
                case 5 :
                    {
                        display();
                        break;
                    }
                case 6 :
                    {
                        i=6;
                        break;
                    }
                default :
                    {
                        printf("Please enter correct choise");
                        break;
                    }
                }

        }while(i!=6);
    return 0;
}

void push()
{
    int n;
    if(top==(SIZE-1))
    {
        printf("\nStack is Overflow ");
    }
    else
    {
        printf("\nEnter Value :");
        scanf("%d",&n);
        top++;
        stack[top]=n;
    }
}

void pop()
{
    if(top==-1)
    {
        printf("\nStack is Underflow ");
    }
    else
    {
        printf("\nPush value is : %d",stack[top]);
        top--;

    }
}

void display()
{
    int i;
    printf("\nStack is \n");
    for(i=top;i>=0;i--)
    {
        printf("%d\n",stack[i]);
    }

}

void peep()
{
    int n;
    printf("Enter element for peep :");
    scanf("%d",&n);
    if(n<=0)
    {
        printf("Your choise is wrong");
    }
    else if(n>(top+1))
    {
        printf("your choise is out of stack");
    }
    else
    {
        printf("Value %d is : %d\n",n,stack[n-1]);
    }
}

void change()
{
    int n;
    printf("Enter element for change :");
    scanf("%d",&n);
    if(n<=0)
    {
        printf("Your choise is wrong");
    }
    else if(n>(top+1))
    {
        printf("your choise is out of stack");
    }
    else
    {
        printf("Enter value : ");
        scanf("%d",&stack[n-1]);
    }
}

0 comments:

Post a Comment