Define stack. Implement push and pop functions for stack using arrays

A stack is an ordered list in which insertions (pushes) and deletions (pops) are made at one end called the top.

PUSH Function for stack using Array

void push(int element)
{
    if (top == MAX - 1)
    {
        printf("Stack Overflow\n");
    }
    else
    {
        top++;
        stack[top] = element;
        printf("Element pushed: %d\n", element);
    }
}

POP Function for stack using Array

int pop()
{
    if (top == -1)
    {
        printf("Stack Underflow\n");
        return -1;
    }
    else
    {
        return stack[top--];
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *