Monday, July 19, 2010

Uses of STACKS

Stack is used by compiler as well as programmer.Its most important uses are listed below ---

      1)  Stack is used by compiler to store local variable declared inside a function.

      2)  Stack is also used by compiler to store return addresses whatever it encounters
               a function call

      3)  It is also used by programmer while writing down programs to convert a givin
               infix expression to its corresponding prefix or postfix expression.

     4)  Stack used by programmer whenever they implement sorting algorithms like
               quick sort, mergesort  etc in a non recursive way.



Ques :-    What is the need for a programmer to convert a infix expression to postfix or
                    prefix expression  ?

Ans :-       It is bit difficult to CPU to understand infix expression because of the operator priorities.
                   Moreover, different programming languages might have different operators with
                   varying properties. To make job of CPU easier DS two alternates for infix expression
                                   1) post fix    2) prefix

                           In these forms infix expression is arranged in such a way that the entire
                  expression is set according to the priority of the operator. So, the CPU start
                  reading  expression form one end  to the other end.

Priorities of operators  :-

                               1)    ( , )

                               2)     $ , ^ 

                               3)     / ,     * ,     %

                               4)     + , -

            NOTE :- 
                    Operators on same level have equal priorities means the operator who will come first in 
                    expression will be evaluate first. And the operator have higher priority will evaluate first.
                    Parentheses have highest priority.
                 


             

No comments:

Post a Comment