Implementation of stack using array

#include <stdio.h>
int val, top=-1, n, i, j, t, choice=0, stack[100];
void push();
void pop();
void show();
int main()
{
    printf("\nEnter the capacity of the stack\n");
    scanf("%d", &n);
    while(choice != 4)
    {
        printf("\nEnter your choice\n1.Push\n2.Pop\n3.Show\n4.Exit\nYou choose: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            {
                push();
                t += 1;
                break;
            }
            case 2:
            {
                pop();
                break;
            }
            case 3:
            {
                show();
                break;
            }
            case 4:
            {
                printf("\nExited\n");
                break;
            }
            default:
            {
                printf("\nInvalid choice.\nEnter a valid choice.\n");
            }
        }
    }
    return 0;
}
void push()
{
    if (top == n-1)
    {
        printf("\n**Overflow**\n");
    }
    else
    {
        printf("\nEnter the value\n");
        scanf("%d",&val);
        top = top + 1;
        stack[top] = val;
    }
}
void pop()
{
    if( top == -1)
    {
        printf("\n**Underflow**\n");
    }
    else
    {
        printf("\n%d is popped out form the stack.\n", stack[top]);
        top -= 1;
    }
}
void show()
{
    if(top == -1)
    {
        printf("\n**Stack is empty**\n");
    }
    else
    {
    for(i=top; i>=0; i--)
    {
        printf("\nValue at %d: %d\n", i, stack[i]);
    }
    }
}

Comments

Popular posts from this blog

My very 1st contest! | Problem - 1 | Beginner level | C language | CodeChef

Implementing linked list in C language

Score High | Problem - 4 | Beginner level | C language | CodeChef