Stacks A Useful Abstract Data Type Creative Commons
Stacks A Useful Abstract Data Type Creative Commons License – Curt Hill.
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) Creative Commons License – Curt Hill.
Names • Push down stacks – Push down lists – Push down queues • Last In First Out Queue – LIFO • One of several sequential ADTs Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
Example and Exercise • Suppose the following operations: push 12 push 6 push 9 pop push 2 push 14 pop • What is the resulting stack? Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
Integer Stack Code • C++: class Stack { public: void push(int); int pop(); bool is. Empty(); Stack(); ~Stack(); private: . . . }; // Stack class Creative Commons License – Curt Hill.
Array stacks • Variables: int st[STACKMAX]; int top; • An additional method should be added: bool is. Full(); Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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? Creative Commons License – Curt Hill.
Pop • Also easy int pop(){ if(top>0) return st[--top]; } // Pop • Again error handling needs some thought Creative Commons License – Curt Hill.
Capacity • Several possibilities bool is. Empty(){ return top<=0; } bool is. Full(){ return top >= STACKMAX; } int size(){ return top; } Creative Commons License – Curt Hill.
Linked Stacks • When the stack has very unpredictable sizes a linked stack may be better • Typically two classes • A helper class has a value and a pointer • The main class has just a pointer – Initialized to NULL • Push and pop need some changes as well Creative Commons License – Curt Hill.
Template Classes template <class T> class Priv. Stack{ friend class Stack; T data; Priv. Stack * next; // no public anything }; template <class T> class Stack { Priv. Stack * top; public: void push(T); T pop(); … Creative Commons License – Curt Hill.
Picture Top 6 18 12 NULL Creative Commons License – Curt Hill.
Linked Push and Pop • Push – The linked push creates a new item – It links that item to the current top of the stack – It then redirects top to this new pointer • The pop must check that top is not NULL – It saves the item carried – Redirects top – Deletes the popped item Creative Commons License – Curt Hill.
Push(1) Temp_Ptr 9 NULL Create item to be pushed on pointer on Temp_Ptr Top 6 18 12 NULL Creative Commons License – Curt Hill.
Push(2) Add to chain Temp_Ptr 9 Top 6 18 12 NULL Creative Commons License – Curt Hill.
Push(3) Redirect top, by assigning Temp_Ptr to Top Temp_Ptr 9 Top 6 18 12 NULL Creative Commons License – Curt Hill.
Pop(1) Assign top to Temp_Ptr 9 Top 6 18 12 NULL Creative Commons License – Curt Hill.
Pop(2) Redirect Top Temp_Ptr 9 Top 6 18 12 NULL Creative Commons License – Curt Hill.
Pop(3) Save value Temp_Ptr 9 Top 6 Result 9 18 12 NULL Creative Commons License – Curt Hill.
Pop(4) Delete Temp_Ptr Top 6 Result 9 18 12 NULL Creative Commons License – Curt Hill.
Pop(5) Return Result. Temp_Ptr Top 6 Result 9 18 12 NULL Creative Commons License – Curt Hill.
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 also prefix and postfix Creative Commons License – Curt Hill.
The Others • Prefix – Operator followed by operands –+32 –+*345 • Postfix – Operands followed by operator – 32+ – 543*+ – aka Reverse Polish Notation (RPN) – aka Suffix Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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) Creative Commons License – Curt Hill.
Algorithm Overview • • Input for the infix expression Output for postfix expression Stack for operators Algorithm follows Creative Commons License – Curt Hill.
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 C)reative Commons License – Curt Hill.
Example – Examples • 2+3*(4 -5*6) • (3+4*5)*(7 -6) Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
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 Creative Commons License – Curt Hill.
- Slides: 38