20 likes | 140 Views
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as a first-in first-out data structure or a pushdown list .
E N D
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as a first-in first-out data structure or a pushdown list. Stacks have many applications but in this case we are interested in stack register as is used in the Pentium processor. The Pentium uses an 8 word (extended 80 bit floating-point) stack register to hold numeric values. In order to evaluate an expression using a stack, we must represent it in postfix or reverse Polish notation. To convert an infix (standard mathematical notation) expression into postfix we move the operators to the right of each operand pair in order of precedence. A*(B+C/(D-E)) A*(B+C/[DE-]) A*(B+[CDE-/]) A*[BCDE-/+] ABCDE-/+* (A+B)*(C-D)/E [AB+]*[CD-]/E [AB+]*[CD-E/] AB+CD-E/* [AB+CD-*]/E AB+CD-*E/
Algorithm for Converting an Infix Expression to Postfix Examine infix string from left to right 1. If next element is an operand, output it. 2. If next element is '(' push it onto a stack. 3. If next element is an operator a. If top of stack is '(' then push operator b. If top of stack is operator with higher precedence then push the operator c. Else pop operator from stack and output then repeat Step 3. 4. If next element is ')' pop and output until '(' is found, then pop and discard. 5. If more input, return to Step 1 else pop and output remaining operators.