26
Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type

Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type

Embed Size (px)

DESCRIPTION

Names Push down stacks –Push down lists –Push down queues Last In First Out Queue –LIFO One of several sequential ADTs Copyright © Curt Hill

Citation preview

Page 1: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type

Copyright © 2001-2009 Curt Hill

StacksAn Useful Abstract Data Type

Page 2: Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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