Download ECE 250 Algorithms and Data Structures: Stacks and more Slides Data Structures and Algorithms in PDF only on Docsity!
ECE 250ECE 250 Algorithms and Data StructuresAlgorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca [email protected] © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca [email protected] © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Stacks Stacks
2 Outline
This topic discusses the concept of a stack:
- (^) Description of an Abstract Stack
- (^) List applications
- (^) Implementation
- (^) Example applications
- (^) Parsing: C++
- (^) Function calls
- (^) Reverse-Polish calculators
- (^) Robert’s Rules
- (^) Standard Template Library
4 Abstract Stack
Also called a last-in–first-out (LIFO) behaviour
- (^) Graphically, we may view these operations as follows:
There are two exceptions associated with abstract stacks:
- (^) It is an undefined operation to call either pop or top on an empty stack
5 Applications
Numerous applications:
- (^) Parsing code:
- (^) Tracking function calls
- (^) Dealing with undo/redo operations
- (^) Reverse-Polish calculators
- (^) Assembly language
The stack is a very simple data structure
- (^) Given any problem, if it is possible to use a stack, this significantly simplifies the solution
7 Linked-List Implementation
Operations at the front of a singly linked list are all Q(1)
The desired behaviour of an Abstract Stack may be reproduced by
performing all operations at the front
Front/ 1 st Back/ n th Find (^) Q(1) Q(1) Insert (^) Q(1) Q(1) Erase (^) Q(1) Q( n )
8 Stack-as-List Class
The stack class using a singly linked list has a single private
member variable:
template class Stack { private: Single_list list; public: bool empty() const; Type top() const; void push( Type const & ); Type pop(); };
10 Stack-as-List Class
The empty and push functions just call the appropriate functions of
the Single_list class
template bool Stack::empty() const { return list.empty(); } template void Stack::push( Type const &obj ) { list.push_front( obj ); }
11 Stack-as-List Class
The top and pop functions, however, must check the boundary case:
template Type Stack::top() const { if ( empty() ) { throw underflow(); } return list.front(); } template Type Stack::pop() { if ( empty() ) { throw underflow(); } return list.pop_front(); }
13 Destructor
We need to store an array:
- (^) In C++, this is done by storing the address of the first entry *Type array;
We need additional information, including:
- (^) The number of objects currently in the stack int stack_size;
- (^) The capacity of the array int array_capacity;
14 Stack-as-Array Class
We need to store an array:
- (^) In C++, this is done by storing the address of the first entry template class Stack { private: int stack_size; int array_capacity; Type *array; public: Stack( int = 10 ); ~Stack(); bool empty() const; Type top() const; void push( Type const & ); Type pop(); };
16 Constructor
Warning: in C++, the variables are initialized in the order in which
they are defined:
template class Stack { private: int stack_size; int array_capacity; Type *array; public: Stack( int = 10 ); ~Stack(); bool empty() const; Type top() const; void push( Type const & ); Type pop(); }; template Stack::Stack( int n ): stack_size( 0 ), array_capacity( std::max( 1, n ) ), array( new Type[array_capacity] ) { // Empty constructor }
17 Destructor
The call to new in the constructor requested memory from the
operating system
- (^) The destructor must return that memory to the operating system: template Stack::~Stack() { delete [] array; }
19 Top
If there are n objects in the stack, the last is located at index n – 1
template Type Stack::top() const { if ( empty() ) { throw underflow(); } return array[stack_size - 1]; }
20 Pop
Removing an object simply involves reducing the size
- (^) It is invalid to assign the last entry to “0”
- (^) By decreasing the size, the previous top of the stack is now at the location stack_size template Type Stack::pop() { if ( empty() ) { throw underflow(); } --stack_size; return array[stack_size]; }