Algorithms for push and pop

 Algorithm for inserting an item into the stack (PUSH)

Let stack[MaxSize] is an array for implementing the stack

1. [Check for stack overflow?]

If Top=Maxsize-1, then print: overflow and exit.

2. Set Top=Top+1[Increase Top by 1]

3. Set Stack[top]=item[Insert item in new top position]

4. Exit.

Algorithm for Deleting an element from the stack

1. [Check for the stack underflow]

If Top<0 then print "stack underflow" and exit.

Else [Remove the top element]

Set item=stack[Top].

2. Decrement the stack top

Set Top=Top-1

3. Return the deleted item from the stack 

4. Exit.

Post a Comment

0 Comments