data structures & programming golnar sheikhshab stacks · 2017. 9. 29. · c++ implementation...

26
Data Structures & Programming Stacks Golnar Sheikhshab

Upload: others

Post on 30-Sep-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Data Structures & Programming

Stacks

Golnar Sheikhshab

Page 2: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Stack Abstract Data Type (ADT)Last in First out (LIFO) container of objects

Applications:

● back button in browser● undo functionality● keeping the variables in recursive calls

2

Page 3: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Stack ADTFunctions:

3

Page 4: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

4

Page 5: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

STL stack

here the base type is int

STL containers including stack, don't throw exceptions

STL stack may cause the program to crash (abort)

for example if one calls pop() on an empty stack

5

Page 6: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Stack informal interfacetemplate <typename E>class Stack { public: int size() const; bool empty() const; const E& top() const throw(StackEmpty); void push(const E& e); void pop() throw(StackEmpty); };

How can we make it a formal interface? By making it an abstract class. How?

By making one of the functions pure virtual.

6

Page 7: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Stack formal interfacetemplate <typename E>class Stack { public: virtual int size() const = 0; bool empty() const; const E& top() const throw(StackEmpty); void push(const E& e); void pop() throw(StackEmpty); };

7

Page 8: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Stack Exceptions// Exception thrown on performing top or pop of an empty stack. class StackEmpty : public RuntimeException { public: StackEmpty(const string& err) : RuntimeException(err) {} };

// Exception thrown on performing top or pop of an empty stack. class StackFull : public RuntimeException { public: StackFull(const string& err) : RuntimeException(err) {} };

8

Page 9: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Class RuntimeExceptionclass RuntimeException { // generic run-time exceptionprivate: string errorMsg;public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; }};

9

Page 10: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

A simple array based stack implementation

10

Page 11: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Array based implementation of stack

11

Page 12: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Time complexity of array based implementation

12

Page 13: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

C++ implementation of ArrayStacktemplate <typename E>class ArrayStack :Stack<E> {public: ArrayStack(int capacity=1000):a_(new E[capacity]), capacity_(capacity), top_(-1){} int size() const {return top_+1;} bool empty() const {return top_<0;} const E& top() const throw(StackEmpty); // implementation in next slide void push(const E& e); // implementation in next slide void pop() throw(StackEmpty); // implementation in next slideprivate: E* a_; int top_; int capacity_;}; 13

Page 14: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

C++ implementation of ArrayStack (2)const E& top() const throw(StackEmpty){ if (empty()) throw StackEmpty("calling top() on an empty stack!"); return a_[top_];}

void push(const E& e){ if (top_>=capacity_) throw StackEmpty("can't push into a full stack!"); a_[++top_] = e;}

void pop() throw(StackEmpty){ if (empty()) throw StackEmpty("calling pop() on an empty stack!"); top_--;} 14

Page 15: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Using ArrayStack#include <iostream>#include "ArrayStack.h"using namespace std;int main(){ ArrayStack<int> A; // A = [ ], size = 0 A.push(7); // A = [7*], size = 1 A.push(13); // A = [7, 13*], size = 2 cout << A.top() << endl; // A = [7, 13*], outputs: 13 A.pop(); // A = [7*], size = 1 A.push(9); // A = [7, 9*], size = 2 cout << A.top() << endl; // A = [7, 9*], outputs: 9 A.pop(); // A = [7]}

15

Page 16: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

C++ Implementation of LinkedStack (non-generic)typedef string Elem; // stack element typeclass LinkedStack { // stack as a linked listpublic: LinkedStack(); // constructor int size() const; // number of items in the stack bool empty() const; // is the stack empty? const Elem& top() const throw(StackEmpty); // the top element void push(const Elem& e); // push element onto stack void pop() throw(StackEmpty); // pop the stackprivate: // member data SLinkedList<Elem> S; // linked list of elements int n; // number of elements};

16

Page 17: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

C++ Implementation of LinkedStack (non-generic)LinkedStack::LinkedStack(): S(), n(0) {} // constructor

int LinkedStack::size() const {return n;} // number of items in the stack

bool LinkedStack::empty() const {return n == 0;} // is the stack empty?

const Elem& LinkedStack::top() const throw(StackEmpty) { if (empty()) throw StackEmpty("Top of empty stack"); return S.front();}

17

Page 18: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

C++ Implementation of LinkedStack (non-generic)void LinkedStack::push(const Elem& e) { // push element onto stack ++n; S.addFront(e);}

// pop the stackvoid LinkedStack::pop() throw(StackEmpty) { if (empty()) throw StackEmpty("Pop from empty stack"); --n; S.removeFront();}

18

Page 19: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

.h and .cpp filesWhat goes into the .h file?

What goes into the .cpp file?

How is a generic class different from a non-generic class in terms of .h and .cpp files?

19

Page 20: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Reversing a vector using stack data structurevoid reverse(vector<string>& V) { // reverse a vector ArrayStack<string> S(V.size()); for (int i = 0; i < V.size(); i++) // push elements onto stack S.push(V[i]); for (int i = 0; i < V.size(); i++) { // pop them in reverse order V[i] = S.top(); S.pop(); }}

20

Page 21: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Using stacks for parentheses checkingThere are many pairs of parenthesis like characters:

• Parentheses: “(” and “)” • Braces: “{” and “}” • Brackets: “[” and “]” • Floor function symbols: “⌊” and “⌋” • Ceiling function symbols: “⌈” and “⌉,”The problem is to check if the parenthesizing is correct

• Correct: ( )(( )){([( )])} • Correct: ((( )(( )){([( )])})) • Incorrect: )(( )){([( )])} • Incorrect: ({[])} • Incorrect: ( 21

Page 22: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

If we have only regular parentheses "(" and ")", do we need stacks to check if the parenthesizing is correct?

22

Page 23: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Matching tags in an HTML document

23

Page 24: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

24

Page 25: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

25

Page 26: Data Structures & Programming Golnar Sheikhshab Stacks · 2017. 9. 29. · C++ Implementation of LinkedStack (non-generic) typedef string Elem; // stack element type class LinkedStack

Reading materialSection 5.1 of your textbook

26