C++ program to implement stack operations using array

19-06-2016

Aim:

To write a program to implement stack operations using array.

Algorithm:

PUSH (STACK, TOP, MAXSTK, ITEM)

This procedure pushes an ITEM onto a stack.

1.    [Stacl already filled ?]

           If TOP = MAXSTK, then: Print: OVERFLOW, and Return.

2.    Set TOP: = TOP + 1. [Increases TOP by 1.]

3.    Set STACK [TOP] : = ITEM. [Insert ITEM in new TOP position.]

4.    Return.

POP (STACK, TOP, ITEM)

This procedure deletes the top element of the STACK and assigns it to the variable ITEM.

1.    [Stack has an ITEM to be removed ?]

           If TOP = 0, then: Print: UNDERFLOW, and Return.

2.    Set ITEM : = STACK [TOP]. [Assingns TOP element to ITEM.]

3.    Set TOP : = TOP -1. [Decreases TOP by 1. ]

4.    Return.

 

Tagged in: ,