Sunday, September 27, 2015

What is STACK ? Write an Algorithm for the PUSH & POP operation.

Solution:-
STACK Introduction:-
*  Stack is non - primitive linear data structure.
*   Accessed from single end called Top Of Stack (TOS).
*   Stack operations are:
        i.  PUSH --: To insert into stack.
        ii.  POP -> To remove item from stack
 *  Using Last in First output (LIFO) principle

Algorithm:-
Algorithm for PUSH operation
PUSH (stack [maxsize] , item , tos)

   Step 1:- if ( tos == maxsize – 1)
                        print " STACK OVERFLOW"
                 Other wise
                        Read item
                        tos = tos + 1
                        set stack [ tos ] = item
                        print " PUSHED SUCCESSFULLY "
                 end if
Step 2: stop

Algorithm for POP operation
POP ( stack [maxsize ] , item , tos )

          Step 1:- if  ( tos  = =  -1 )
                               print " STACK UNDERFLOW "
                        Other wise
                               item = stack [tos ]
                               tos = tos -1
                               print " POPED SUCCESSFULLY "
                        End if
          Step 2:- stop