You are on page 1of 21

Satck

C#ODE Studio || codstudio.wordpress.com

Stack- A stack is a linear data structure in which items may be added or removed only at one end . Accordingly, stacks are also called lastin-first-out or LIFO lists. The end at which element is added or removed is called the top of the stack. Two basic operations associated with stacks are : Push- Term used to denote insertion of an element onto a stack. Pop- Term used to describe deletion of an element from a stack. The order in which elements are pushed onto a stack is reverse of the

order in which elements are popped from a stack

C#ODE Studio || codstudio.wordpress.com

Representation of stacks Stacks may be represented in memory in various ways usually by means of one-way list or a linear array In array representation of stack, stack is maintained by an array named STACK , a variable TOP which contains the location/index of top element of stack and a variable MAXSTK giving the maximum number of elements that can be held by the stack. The condition TOP=0 or TOP=NULL indicates that stack is empty. The operation of addition of an item on stack and operation of removing an item from a stack may be implemented respectively by sub algorithms, called PUSH and POP. Before executing operation PUSH on to a stack, one must first test whether there is room in the stack for the new item. If not, then we have the condition known as overflow. Analogously, in executing the POP operation, one must first test where there is an element in stack to be deleted. If not, then we have condition known as underflow.
C#ODE Studio || codstudio.wordpress.com

ARRAY IMPLEMENTATION OF STACK


C#ODE Studio || codstudio.wordpress.com

Algorithm: PUSH (STACK,TOP,MAXSTK,ITEM) This algorithm pushes an item onto the stack array. TOP stores the index of top element of the stack and MAXSTK stores the maximum size of the stack.

Step 1: [Stack already filled] If TOP=MAXSTK, then: Write: OVERFLOW Return Step 2: Set TOP:=TOP+1 Step 3: Set STACK[TOP]:=ITEM Step 4: Return

C#ODE Studio || codstudio.wordpress.com

Algorithm: POP(STACK,TOP,ITEM) This procedure deletes the top element of STACK array and assign it to variable ITEM

Step 1: If TOP=0,then: Write: UNDERFLOW Return Step 2: Set ITEM:=STACK[TOP] Step 3: Set TOP:=TOP-1 Step 4: Return

C#ODE Studio || codstudio.wordpress.com

A stack represented using a linked list is also known as linked stack. The array based representation of stack suffers from following limitations: Size of stack must be known in advance Representing stack as an array prohibits the growth of stack beyond finite number of elements. In a linked list implementation of stack, each memory cell will contain the data part of the current element of stack and pointer that stores the address of its bottom element and the memory cell containing the bottom most element will have a NULL pointer

C#ODE Studio || codstudio.wordpress.com

Push operation on linked list representation of stack Algorithm: PUSH(INFO, LINK, TOP, ITEM, AVAIL) This algorithm pushes an element to the top of the stack Step 1: If AVAIL=NULL, then Write: OVERFLOW Return Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Step 3: Set INFO[NEW]:=ITEM Step 4: If TOP=NULL, then Set LINK[NEW]:=NULL Set TOP:=NEW Return Else: Set LINK[NEW]:=TOP Set TOP:=NEW Step 5: Return
C#ODE Studio || codstudio.wordpress.com

POP operation on linked list representation of stack Algorithm: POP(INFO, LINK, TOP, AVAIL) This algorithm deletes an element from the top of the stack Step 1: If TOP=NULL , then: Write: UNDERFLOLW Return Step 2: Set PTR:=TOP Set TOP:=LINK[TOP] Write: INFO[PTR] Step 3: Set LINK[PTR]:=AVAIL and AVAIL:=PTR Step 4: Return

C#ODE Studio || codstudio.wordpress.com

Application of stack Evaluation of arithmetic expression. For most common arithmetic operations, operator symbol is placed between its operands. This is called infix notation of an expression. To use stack to evaluate an arithmetic expression, we have to convert the expression into its prefix or postfix notation. Polish notation , refers to the notation in which operator symbol is placed before its two operands. This is also called prefix notation of an arithmetic expression. The fundamental property of polish notation is that the order in which operations are to be performed is completely determined by positions of operators and operands in expression. Accordingly, one never needs parentheses when writing expression in polish notation. Reverse Polish notation refers to notation in which operator is placed after its two operands. This notation is frequently called postfix notation. Examples of three notations are:

C#ODE Studio || codstudio.wordpress.com

INFIX NOTATION: A+B PREFIX OR POLISH NOTATION: +AB POSTFIX OR REVERSE POLISH NOATATION: AB+ Convert the following infix expressions to prefix and postfix forms A+(B*C) (A+B)/(C+D) Prefix: +A*BC Postfix: A BC*+ Prefix: / +AB+CD Postfix: AB+CD+/

C#ODE Studio || codstudio.wordpress.com

The computer usually evaluates an arithmetic expression written in infix notation in two steps. Converts expression to postfix notation Evaluates the postfix notation.

Evaluation of postfix expression Suppose P is an arithmetic expression written in postfix notation. The following algorithm which uses a STACK to hold operands and evaluates P

C#ODE Studio || codstudio.wordpress.com

Algorithm: This algorithm finds the VALUE of an arithmetic expression P written in Postfix notation. Step 1: Add a right parentheses ) at the end of P Step 2: Scan P from left to right and repeat step 3 and 4 for each element of P until ) is encountered Step 3: If an operand is encountered, put the operand on the stack Step 4: If an operator is encountered , then: (a) Remove the two top elements of stack, where A is top element and B is next-top-element. (b) Evaluate B A (c ) Place the result of (b) back on stack [End of if structure] [End of step 2 loop] Step 5: Set VALUE equal to the top element of stack Step 6: Exit

C#ODE Studio || codstudio.wordpress.com

Transforming Infix Expression into Postfix Expression The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using operands from Q and operators which are removed from STACK. The algorithm begins by pushing a left parentheses onto stack and adding a right parentheses at the end of Q Algorithm: POSTFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P Step 1: Push ( on to the STACK and add ) to the end of Q Step 2: Scan Q from left to right and repeat step 3 to 6 for each element of Q until the STACK is empty Step 3: If an operand is encountered, add it to P Step 4: If left parentheses is encountered, add it to STACK

C#ODE Studio || codstudio.wordpress.com

Step 5: If an operator is encountered, then: (a) Repeatedly pop from STACK and add to P each operator which has same precedence or higher precedence than (b) Add to STACK Step 6: If a right parentheses is encountered , then: (a) Repeatedly pop from STACK and add to P each operator until a left parentheses is encountered (b) Remove the left parentheses [End of Step 2 Loop] Step 7: Exit

C#ODE Studio || codstudio.wordpress.com

Example: Convert Q=A+(B*C) / D) to its corresponding postfix form Solution: put ) at the end of Q and put ( on stack Starting from left: Operand A , put it on P Operator + move to stack as no operator there ( move on stack Operand B, put it on P Operator * , move to stack as no operator Operand C , move to P ) , pop from stack and put on P until ( is encountered. Pop ( also operator /, as precedence of / is higher than + on stack, no pop possible. Push / on stack Operand D , put it on P Right parentheses ), Pop all the elements and add the P until ( is encountered. Also remove ( from stack

* ( /

+
(

+
(

+
(

P= A B C* D / +
C#ODE Studio || codstudio.wordpress.com

Transforming Infix Expression into Prefix Expression Algorithm: [Polish Notation] PREFIX (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent prefix expression P Step 1: Reverse the input string Step 2: Examine the next element in the input Step 3: If it is operand, add it to output string Step 4: If it is closing parentheses, push it on stack Step 5: If it is operator, then: (i) if stack is empty, push operator on stack (ii) if top of stack is closing parentheses, push operator on the stack (iii) If it has same or higher priority than top of stack, push operator on stack Else pop the operator from the stack and add it to output string, repeat step 5
C#ODE Studio || codstudio.wordpress.com

Step 6: If it is an opening parentheses, pop operators from stack and add them to output string until a closing parentheses is encountered, pop and discard the closing parentheses. Step 7: If there is more input go to step 2 Step 8: If there is no more input, unstack the remaining operators and add them to output string Step 9: Reverse the output string

C#ODE Studio || codstudio.wordpress.com

Consider the following arithmetic expression P written in postfix notation P: 12, 7, 3 -, /, 2, 1, 5, +, *, + (a) Translate P, by inspection and hand, into its equivalent infix expression (b) Evaluate the infix expression Sol: (a) Scanning from left to right, translate each operator from postfix to infix notation P = 12, [7-3], /, 2, 1, 5, +, *, + = [12/[7-3]],2, [1+5],*,+ = 12/(7-3)+2*(1+5) (b) 12/(7-3)+2*(1+5) = [3],[2*6],+ = 3+12 = 15
C#ODE Studio || codstudio.wordpress.com

Practical applications of stack


Stacks are used for implementing function calls in a program Used for implementing recursion. Used for conversion of infix expression to its postfix or prefix form Used for evaluation of postfix expression. Used in sorting of arrays (quicksort and mergesort technique)

C#ODE Studio || codstudio.wordpress.com

References
Dinesh Mehta and Sartaj Sahni Handbook of Data Structures and Applications, Chapman and Hall/CRC Press, 2007. Niklaus Wirth, Algorithms and Data Structures, Prentice Hall, 1985. Diane Zak, Introduction to programming with c++, copyright 2011 Cengage Learning Asia Pte Ltd Schaumm Series ,McGraw Hill.

C#ODE Studio || codstudio.wordpress.com