ECE 250 Algorithms and Data Structures: Stacks, Slides of Data Structures and Algorithms

this slide about the data structure and alogrithum

Typology: Slides

2019/2020

Uploaded on 06/29/2020

muhammad-zeshan-2
muhammad-zeshan-2 🇵🇰

1 document

1 / 76

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECE 250 Algorithms and Data Structures
ECE 250 Algorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LEL
Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
© 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
© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Stacks
Stacks
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c

Partial preview of the text

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:
    • (^) Matching parenthesis
  • (^) 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]; }