Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type."— Presentation transcript:

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

2 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

3 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

4 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

5 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 popped Copyright © 2001-2009 Curt Hill

6 Example and Exercise Suppose the following operations: push 12 push 6 push 9 pop push 2 push 14 pop pop What is the resulting stack? Copyright © 2001-2009 Curt Hill

7 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

8 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 methods Copyright © 2001-2009 Curt Hill

9 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

10 Integer Stack Code C++: class Stack { public: void push(int); int pop(); bool isEmpty(); Stack(); ~Stack(); private:... }; // Stack class Copyright © 2001-2009 Curt Hill

11 Array stacks Variables: int st[STACKMAX]; int top; An additional method should be added: bool isFull(); Copyright © 2001-2009 Curt Hill

12 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

13 Push Now easy void 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

14 Pop Also easy int pop(){ if(top>0) return st[top--]; } // Pop Again error handling needs some thought Copyright © 2001-2009 Curt Hill

15 Capacity Several possibilities bool isEmpty(){ return top>0; } bool isFull(){ return top >= STACKMAX; } int size(){ return top; } Copyright © 2001-2009 Curt Hill

16 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 postfix Copyright © 2001-2009 Curt Hill

17 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

18 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

19 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

20 Algorithm Overview Input for the infix expression Output for postfix expression Stack for operators Algorithm follows Copyright © 2001-2009 Curt Hill

21 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

22 Example –Examples 2+3*(4-5*6) (3+4*5)*(7-6) Copyright © 2001-2009 Curt Hill

23 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

24 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

25 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

26 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


Download ppt "Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type."

Similar presentations


Ads by Google