+3 votes
in Class 12 by kratos

What is a stack? Write an algorithm for PUSH() and POP() operations.

1 Answer

+1 vote
by kratos
 
Best answer

Algorithm for PUSH operation:
PUSH( STACK, TOP, SIZE, ITEM)
Step 1: if TOP >= N-1 then
PRINT “stack is overflow”
Exit
End of if
Step 2: Top = TOP + 1
Step 3: STACK[TOP] = ITEM
Step 4: Return

Algorithm for POP operation:
PUSH( STACK, TOP, ITEM)
Step 1: if TOP = 0 then
PRINT “stack is empty”
Exit
End of if

Step 2: ITEM = STACK[POP]
Step 3: TOP = TOP -1
Step 4: Return

...