Friday, June 15, 2012

What are STACK in DataBase and its alogrithms

A stack is an ordered collection of items into which new items may be inserted and from which items can be deleted at one end,called top of stack.It is First In Last Out(FILO) type of data structure.
Stack can be thought as pile of plates one on other.One which is put first stay at last or bottom of pile and one that is pile last is always on top.Also to take out a plate form it one which is  on top is remove first and other later.That's why it is called FILO. 

Push is the process of adding data on top of stack.
Pop  is the process of removing data from top of stack.

Stack Algorithms:
There are basically two methods implement stack:-

 a)Top of stack varying  method:
    1)Declare and initialize necessary variables e.g. top=-1 , Max_Size.
    2)For  Push operation-
                     IF top=Max_Size-1
                               print" Stack is Full "
                  ELSE
                         top=top+1
                        READ items from user
                        Stack[top]=item
   3)For next Push  operation go to step 2
   4)For Pop operation-
                    IF top=-1
                               print" Stack is Empty "
                  ELSE
                        item=Stack[top]
                         top=top-1
                        Display item
  5)For next Pop operation  goto  step 4
  6)Stop]



b)Top of stack fixed method:
    1)Declare and initialize necessary variables
            e.g. Bos=0,Max_Size etc.
    2) For Push operation-
                 IF BOS=Max_Size
                        print"Stack Overflow!"
                ELSE
                    insert new element by making vaccant space at stack[0] location
    3)For next Push operation goto step2
    4)For Pop operation -
                IF BOS=0
                      print "Stack is Empty!"
                ELSE
                   remove item from stack [0]
                   BOS--
                   refill empty space by lowering each step by 1
     5)For next Pop operation goto step4
    6)Stop
                     


         
         

No comments:

Post a Comment