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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
void push(int element)
{
if (top == MAX - 1)
{
printf("Stack Overflow\n");
}
else
{
top++;
stack[top] = element;
printf("Element pushed: %d\n", element);
}
}
void push(int element) { if (top == MAX - 1) { printf("Stack Overflow\n"); } else { top++; stack[top] = element; printf("Element pushed: %d\n", element); } }
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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int pop()
{
if (top == -1)
{
printf("Stack Underflow\n");
return -1;
}
else
{
return stack[top--];
}
}
int pop() { if (top == -1) { printf("Stack Underflow\n"); return -1; } else { return stack[top--]; } }
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 *