CSCI 3333 Data Structures Stacks Acknowledgement Dr Bun
CSCI 3333 Data Structures Stacks
Acknowledgement Dr. Bun Yue ¡ Mr. Charles Moen ¡ Dr. Wei Ding ¡ Ms. Krishani Abeysekera ¡ Dr. Michael Goodrich ¡
Stacks ¡ A Last In First Out (LIFO) data structure.
Simple Stack ADT Example int size() Return the number of elements in the stack bool is. Empty() Indicate whether the stack is empty void push( Object element ) Insert element at the top of the stack Object top() Return the top element on the stack without removing it; an error occurs if the stack is empty. Object pop() Remove and return the top element on the stack; an error occurs if the stack is empty
Exceptions ¡ ¡ Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be “thrown” by an operation that cannot be executed ¡ ¡ In the Stack ADT, operations pop and top cannot be performed if the stack is empty Attempting the execution of pop or top on an empty stack throws an Empty. Stack. Exception
C++ STL Stack ¡ ¡ See C++ STL reference. Methods: l l l Stack constructors : construct a new stack Empty: true if the stack has no elements pop : removes the top element of a stack Push: adds an element to the top of the stack Size: returns the number of items in the stack Top: returns the top element of the stack
Applications of Stacks ¡ Direct applications l l l ¡ Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in view stack in visual studio Indirect applications l l Auxiliary data structure for algorithms Component of other data structures
Runtime Stacks ¡ ¡ A running C++ program has a private stack, called the run-time stack, which is a chain of active methods with a stack When a method is called, it is pushed on the stack, which is used to keep track of local variables and other information on functions. These descriptors are called frames. l Local variables and return value l Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } bar PC = 1 m=6 foo PC = 3 j=5 k=6 main PC = 2 i=5
Array-based Stack ¡ ¡ ¡ A simple way of Algorithm size() implementing the return t + 1 Stack ADT uses an Algorithm pop() array if is. Empty() then We add elements from left to right throw Stack. Empty. Exception A variable keeps else track of the index of o = s[t] the top element t t 1 return o //pg. 161 … S 0 1 2 t N-element array S where t gives the index of the top element
Array-based Stack (cont. ) ¡ ¡ The array storing the stack elements may become full A push operation will then throw a Full. Stack. Exception l l Limitation of the array -based implementation Not intrinsic to the Stack ADT Algorithm push(o) if t = S. length 1 then throw Stack. Full. Exception else t t+1 S[t] o … S 0 1 2 t
Performance and Limitations ¡ Performance l l l ¡ Let n be the number of elements in the stack The space used is O(n) Each operation runs in time O(1) Limitations l l The maximum size of the stack must be defined a priori and cannot be changed Trying to push a new element into a full stack causes an implementation-specific exception
Questions and Comments?
- Slides: 12