copyright © 2001-2009 curt hill stacks an useful abstract data type
DESCRIPTION
Names Push down stacks –Push down lists –Push down queues Last In First Out Queue –LIFO One of several sequential ADTs Copyright © Curt HillTRANSCRIPT
![Page 1: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/1.jpg)
Copyright © 2001-2009 Curt Hill
StacksAn Useful Abstract Data Type
![Page 2: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/2.jpg)
Examples• Come look at my desk• Sock drawers• Cafeteria plates• Function data spaces
– If a calls b and b calls c and c calls d– Their local variables are a stack (aka
the invocation stack)
Copyright © 2001-2009 Curt Hill
![Page 3: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/3.jpg)
Names• Push down stacks
– Push down lists– Push down queues
• Last In First Out Queue– LIFO
• One of several sequential ADTs
Copyright © 2001-2009 Curt Hill
![Page 4: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/4.jpg)
Why use?• Why does a cafeteria use them?• Used for storage of things
temporarily• Things are all the same size• Do not care about how long each
thing is stored– Response or arrival times do not matter
• Easiest list to maintain• All the work is at one end
Copyright © 2001-2009 Curt Hill
![Page 5: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/5.jpg)
What Operations?• Two main operations Push and Pop• Push places a new item on the stack• Pop removes it• Others:
– Determination of empty or number of items
– Initialization• Only the top of the stack is
accessible– Top is most recently pushed item that
has not been poppedCopyright © 2001-2009 Curt Hill
![Page 6: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/6.jpg)
Example and Exercise• Suppose the following operations:
push 12push 6push 9pop push 2push 14poppop
• What is the resulting stack?Copyright © 2001-2009 Curt Hill
![Page 7: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/7.jpg)
Result• Top on left• After the first three pushes: 9 6 12• After first pop: 6 12• After two more pushes: 14 2 6 12• After second pop: 2 6 12• After last pop: 6 12
Copyright © 2001-2009 Curt Hill
![Page 8: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/8.jpg)
Implementation• Like many ADTs a stack may have
several different possible implementations
• The underlying data structure could be– An array– A vector– Linked list
• Others are possible but less reasonable
• Each might need additional methodsCopyright © 2001-2009 Curt Hill
![Page 9: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/9.jpg)
Interface and Implementation
• The same interface may exist with different implementations
• We will now look at an interface for an integer stack– Just the public part
• How this would be implemented would be in the private part and method implementations
Copyright © 2001-2009 Curt Hill
![Page 10: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/10.jpg)
Integer Stack Code• C++:class Stack { public: void push(int); int pop(); bool isEmpty(); Stack(); ~Stack(); private: ... }; // Stack class
Copyright © 2001-2009 Curt Hill
![Page 11: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/11.jpg)
Array stacks• Variables:int st[STACKMAX];int top;
• An additional method should be added:bool isFull();
Copyright © 2001-2009 Curt Hill
![Page 12: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/12.jpg)
Initialization• Initialization is only setting top• Set to zero
– Indexes first unused item• Set to -1
– Indexes first used item• Which is better?• The way of the C family is set to
zero• Other languages may choose either
Copyright © 2001-2009 Curt Hill
![Page 13: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/13.jpg)
Push• Now easyvoid push(int item){ if (top < STACKMAX) st[top++] = item; } // Push
• Error handling is also a design issue– What do we do with a stack that is full
and a request for a push?
Copyright © 2001-2009 Curt Hill
![Page 14: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/14.jpg)
Pop• Also easyint pop(){ if(top>0) return st[top--]; } // Pop
• Again error handling needs some thought
Copyright © 2001-2009 Curt Hill
![Page 15: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/15.jpg)
Capacity• Several possibilitiesbool isEmpty(){ return top>0; }bool isFull(){ return top >= STACKMAX; }int size(){ return top; }
Copyright © 2001-2009 Curt Hill
![Page 16: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/16.jpg)
Algebraic expressions• Order of algebraic expressions• There are several ways that an
algebraic expression may be shown• Everyone is familiar with infix, which
is standard algebraic notation• Operands surround operators
– 3 + 2– 5 + 3 * 4– Requires precedence and parentheses
• There are also prefix and postfixCopyright © 2001-2009 Curt Hill
![Page 17: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/17.jpg)
The Others• Prefix
– Operator followed by operands– + 3 2– + * 3 4 5
• Postfix– Operands followed by operator– 3 2 +– 5 4 3 * +– aka Reverse Polish Notation (RPN)– aka Suffix
Copyright © 2001-2009 Curt Hill
![Page 18: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/18.jpg)
Why do I care?• The older HP calculators used to
use RPN• Computers do not have a
parenthesis, so every infix expression must be converted to postfix– The natural notation for computers
• Load the operands into a register and then execute the operation
Copyright © 2001-2009 Curt Hill
![Page 19: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/19.jpg)
Conversion of infix to postfix
• Operators must have a priority• Starting at the lowest priority this
is:– end of expression– (– =– + - (addition and subtraction)– * / (multiplication and division)– - (unary negation)
Copyright © 2001-2009 Curt Hill
![Page 20: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/20.jpg)
Algorithm Overview• Input for the infix expression• Output for postfix expression• Stack for operators• Algorithm follows
Copyright © 2001-2009 Curt Hill
![Page 21: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/21.jpg)
Algorithm• Repeat until no more inputs• What to do if you find a:
– Operand• Copy to output
– Operator (• Push on regardless
– Any operator but )while priority(topstack) >= priority(current)
write(pop())push(current)
– Operator ) • Pop and write until ( found
Discard )Copyright © 2001-2009 Curt Hill
![Page 22: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/22.jpg)
Example– Examples
• 2+3*(4-5*6)• (3+4*5)*(7-6)
Copyright © 2001-2009 Curt Hill
![Page 23: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/23.jpg)
RPN calculator• Evaluate an expression• While not eof do• For an operand push it on stack• For an operator
– Pop one or two things off of stack– Do the operation– Push result on stack
Copyright © 2001-2009 Curt Hill
![Page 24: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/24.jpg)
Evaluate Infix• Combining the two algorithms
– Converting to RPN– Executing RPN
• Notes– When you write an operand then push
on second stack– When you write an operator - execute it
in stack fashion - pop off two, execute, push on result
– When you are done the stack should contain only one item, which you pop and print
Copyright © 2001-2009 Curt Hill
![Page 25: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/25.jpg)
Code generation for a compiler
• Stack machine is same as a RPN calculator
• Register machine (register-memory instructions)
• Convert to RPN• Each output will be a load to a
register
Copyright © 2001-2009 Curt Hill
![Page 26: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type](https://reader036.vdocuments.mx/reader036/viewer/2022062503/5a4d1ad37f8b9ab05997205b/html5/thumbnails/26.jpg)
Conclusion• Stacks are the easiest to maintain
linear structure• All the action is at just one end
– No iteration, no indexing• Used as temporary storage when
holding time does not matter• Underlying data structure could be
– Array– List– Vector
Copyright © 2001-2009 Curt Hill