Stacks and Queues Andy Wang Data Structures Algorithms

  • Slides: 85
Download presentation
Stacks and Queues Andy Wang Data Structures, Algorithms, and Generic Programming

Stacks and Queues Andy Wang Data Structures, Algorithms, and Generic Programming

Abstract Data Type l. A collection of data l A set of operations on

Abstract Data Type l. A collection of data l A set of operations on the data or subsets of the data l A set of axioms, or rules of behavior governing the interaction of operators l Examples: stack, queue, list, vector, deque, priority queue, table (map), associative array, set, graph, digraph

Stack ADT l Collections: ¡ Elements of some proper type T l Operations: ¡

Stack ADT l Collections: ¡ Elements of some proper type T l Operations: ¡ void push(T t) ¡ void pop() ¡ T top() ¡ bool empty() ¡ unsigned int size() ¡ constructor and destructor

Stack ADT (2) l Axioms ¡ S. size, (for any stack S) S. empty(),

Stack ADT (2) l Axioms ¡ S. size, (for any stack S) S. empty(), and S. push(t) are always defined ¡ S. pop() and S. top() are defined iff S. empty() is false ¡ S. empty(), S. size(), S. top() do not change S ¡ S. empty() is true iff S. size() == 0 ¡ S. push(t) followed by S. pop() leaves S unchanged

Stack ADT (3) l Axioms ¡ After (for any stack S) S. push(t), S.

Stack ADT (3) l Axioms ¡ After (for any stack S) S. push(t), S. top() returns t ¡ S. push(t) increases S. size() by 1 ¡ S. pop() decreases S. size() by 1

Stack Model—LIFO l Empty stack S ¡ ¡ ¡ S. empty() is true S.

Stack Model—LIFO l Empty stack S ¡ ¡ ¡ S. empty() is true S. top() not defined S. size() == 0 food chain stack

Stack Model—LIFO l S. push(“mosquito”) ¡ ¡ ¡ S. empty() is false S. top()

Stack Model—LIFO l S. push(“mosquito”) ¡ ¡ ¡ S. empty() is false S. top() == “mosquito” S. size() == 1 mosquito food chain stack

Stack Model—LIFO l S. push(“fish”) ¡ ¡ ¡ S. empty() is false S. top()

Stack Model—LIFO l S. push(“fish”) ¡ ¡ ¡ S. empty() is false S. top() == “fish” S. size() == 2 fish mosquito food chain stack

Stack Model—LIFO l S. push(“raccoon”) ¡ ¡ ¡ S. empty() is false S. top()

Stack Model—LIFO l S. push(“raccoon”) ¡ ¡ ¡ S. empty() is false S. top() == “raccoon” S. size() == 3 raccoon fish mosquito food chain stack

Stack Model—LIFO l S. pop() ¡ ¡ ¡ S. empty() is false S. top()

Stack Model—LIFO l S. pop() ¡ ¡ ¡ S. empty() is false S. top() == “fish” S. size() == 2 fish mosquito food chain stack

Derivable Behaviors (Theorems) l If (S. size() == n) is followed by k push

Derivable Behaviors (Theorems) l If (S. size() == n) is followed by k push operations, then S. size() == n + k l If (S. size() == n) is followed by k pop operations, then S. size == n – k (k <= n) l The last element of S pushed onto S is the top of S l S. pop() removes the last element of S pushed onto S

Uses of ADT Stack l Depth first search / backtracking l Evaluating postfix expressions

Uses of ADT Stack l Depth first search / backtracking l Evaluating postfix expressions l Converting infix to postfix l Function calls (runtime stack) l Recursion

Queue ADT l Collection ¡ Elements of some proper type T l Operations ¡

Queue ADT l Collection ¡ Elements of some proper type T l Operations ¡ void push(T t) ¡ void pop() ¡ T front() ¡ bool empty() ¡ unsigned int size() ¡ Constructors and destructors

Queue ADT l Axioms (for any Queue Q) ¡ Q. size(), Q. empty(), Q.

Queue ADT l Axioms (for any Queue Q) ¡ Q. size(), Q. empty(), Q. push(t) are always defined ¡ Q. pop() and Q. front() are defined iff Q. empty() is false ¡ Q. empty(), Q. size(), Q. front() do not change Q ¡ Q. empty() is true iff Q. size() == 0 ¡ Suppose Q. size() == n, and the next element pushed onto Q is t; then, after n elements have been popped from Q, t = Q. front()

Queue ADT l Axioms (for any Queue Q) ¡ Q. push(t) increases Q. size()

Queue ADT l Axioms (for any Queue Q) ¡ Q. push(t) increases Q. size() by 1 ¡ Q. pop() decreases Q. size() by 1 ¡ If t = Q. front() then Q. pop() removes t from Q

Queue Model—FIFO l Empty Q animal parade queue

Queue Model—FIFO l Empty Q animal parade queue

Queue Model—FIFO l Q. Push(“ant”) animal parade queue a nt front back

Queue Model—FIFO l Q. Push(“ant”) animal parade queue a nt front back

Queue Model—FIFO l Q. Push(“bee”) animal parade queue a b nt e fronte back

Queue Model—FIFO l Q. Push(“bee”) animal parade queue a b nt e fronte back

Queue Model—FIFO l Q. Push(“cat”) animal parade queue a b c nt e at

Queue Model—FIFO l Q. Push(“cat”) animal parade queue a b c nt e at fronte back

Queue Model—FIFO l Q. Push(“dog”) animal parade queue a b c d nt e

Queue Model—FIFO l Q. Push(“dog”) animal parade queue a b c d nt e at o fronte g back

Queue Model—FIFO l Q. Pop() animal parade queue bee cat dog front back

Queue Model—FIFO l Q. Pop() animal parade queue bee cat dog front back

Queue Model—FIFO l Q. Pop() animal parade queue cat dog front back

Queue Model—FIFO l Q. Pop() animal parade queue cat dog front back

Queue Model—FIFO Q. Push(“eel”) l Q. Pop() l animal parade queue eel front back

Queue Model—FIFO Q. Push(“eel”) l Q. Pop() l animal parade queue eel front back

Derivable Behaviors (Theorems) l If (Q. size() == n) is followed by k push

Derivable Behaviors (Theorems) l If (Q. size() == n) is followed by k push operations, then Q. size() == n + k l If (Q. size() == n) is followed by k pop operations, then Q. size() == n – k (k <= n) l The first element pushed onto Q is the front of Q l Q. pop() removes the front element of Q

Uses of ADT Queue l Buffers l Breadth first search l Simulations

Uses of ADT Queue l Buffers l Breadth first search l Simulations

Depth First Search—Backtracking l Problem ¡ l Discover a path from start to goal

Depth First Search—Backtracking l Problem ¡ l Discover a path from start to goal Solution ¡ Go deep l ¡ If there is an unvisited neighbor, go there Backtrack l l Retreat along the path to find an unvisited neighbor Outcome ¡ start 1 If there is a path from start to goal, DFS finds one such path 5 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (2) l Stack start 1 5 Push 1 2 3 4

Depth First Search—Backtracking (2) l Stack start 1 5 Push 1 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (3) l Stack start 1 5 Push 2 1 2 3

Depth First Search—Backtracking (3) l Stack start 1 5 Push 2 1 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (4) l Stack start 1 Push 5 2 1 5 2

Depth First Search—Backtracking (4) l Stack start 1 Push 5 2 1 5 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (5) l Stack start 1 Push 6 5 2 1 5

Depth First Search—Backtracking (5) l Stack start 1 Push 6 5 2 1 5 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (6) l Stack start 1 Push Push 9 6 5 2

Depth First Search—Backtracking (6) l Stack start 1 Push Push 9 6 5 2 1 5 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (7) l Stack start 1 Pop Push 6 5 2 1

Depth First Search—Backtracking (7) l Stack start 1 Pop Push 6 5 2 1 5 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (8) l Stack start 1 Pop Push 5 2 1 5

Depth First Search—Backtracking (8) l Stack start 1 Pop Push 5 2 1 5 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (9) l Stack start 1 5 2 3 4 6 7

Depth First Search—Backtracking (9) l Stack start 1 5 2 3 4 6 7 8 9 10 Pop Push 2 1 goal 11 12

Depth First Search—Backtracking (10) l Stack start 1 5 Pop Push 1 2 3

Depth First Search—Backtracking (10) l Stack start 1 5 Pop Push 1 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (11) l Stack start 1 5 Push 3 1 2 3

Depth First Search—Backtracking (11) l Stack start 1 5 Push 3 1 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (12) l Stack start 1 Push 7 3 1 5 2

Depth First Search—Backtracking (12) l Stack start 1 Push 7 3 1 5 2 3 4 6 7 8 9 10 goal 11 12

Depth First Search—Backtracking (13) l Stack start 1 Push 10 7 3 1 5

Depth First Search—Backtracking (13) l Stack start 1 Push 10 7 3 1 5 2 3 4 6 7 8 9 10 goal 11 12

DFS Implementation DFS { stack<location> S; // mark the start location as visited S.

DFS Implementation DFS { stack<location> S; // mark the start location as visited S. push(start); while (S is not empty) { t = S. top(); if (t == goal) Success(S); if (// t has unvisited neighbors) { // choose an unvisited neighbor // mark n visited; S. push(n); } else { Back. Track(S); } } Failure(S); }

DFS Implementation (2) Back. Track(S) { while (!S. empty() && S. top() has no

DFS Implementation (2) Back. Track(S) { while (!S. empty() && S. top() has no unvisited neighbors) { S. pop(); } } Success(S) { // print success while (!S. empty()) { output(S. top()); S. pop(); } } Failure(S) { // print failure while (!S. empty()) { S. pop(); } }

Breadth First Search l Problem ¡ Find a shortest path from start to goal

Breadth First Search l Problem ¡ Find a shortest path from start to goal start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (2) l Queue 1 Push start 1 5 2 3 4

Breadth First Search (2) l Queue 1 Push start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (3) l Queue Pop start 1 5 2 3 4 6

Breadth First Search (3) l Queue Pop start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (4) l Queue 2 3 4 Push start 1 5 2

Breadth First Search (4) l Queue 2 3 4 Push start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (5) l Queue 3 4 Pop start 1 5 2 3

Breadth First Search (5) l Queue 3 4 Pop start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (6) l Queue 3 4 5 6 Push start 1 5

Breadth First Search (6) l Queue 3 4 5 6 Push start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (7) l Queue 4 5 6 Pop start 1 5 2

Breadth First Search (7) l Queue 4 5 6 Pop start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (8) l Queue 4 5 6 7 8 Push start 1

Breadth First Search (8) l Queue 4 5 6 7 8 Push start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (9) l Queue 5 6 7 8 Pop start 1 5

Breadth First Search (9) l Queue 5 6 7 8 Pop start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (10) l Queue 6 7 8 Pop start 1 5 2

Breadth First Search (10) l Queue 6 7 8 Pop start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (11) l Queue 7 8 Pop start 1 5 2 3

Breadth First Search (11) l Queue 7 8 Pop start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (12) l Queue 7 8 9 Push start 1 5 2

Breadth First Search (12) l Queue 7 8 9 Push start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (13) l Queue 8 9 Pop start 1 5 2 3

Breadth First Search (13) l Queue 8 9 Pop start 1 5 2 3 4 6 7 8 9 10 goal 11 12

Breadth First Search (14) l Queue 8 9 1 0 Push start 1 5

Breadth First Search (14) l Queue 8 9 1 0 Push start 1 5 2 3 4 6 7 8 9 10 goal 11 12

BFS Implementation BFS { queue<location> Q; // mark the start location as visited Q.

BFS Implementation BFS { queue<location> Q; // mark the start location as visited Q. push(start); while (Q is not empty) { t = Q. front(); for (// each unvisited neighbor n) { Q. push(n); if (n == goal) Success(S); } Q. pop(); } Failure(Q); }

Evaluating Postfix Expressions l Postfix expressions: operands precede operator l Tokens: atomics of expressions,

Evaluating Postfix Expressions l Postfix expressions: operands precede operator l Tokens: atomics of expressions, either operator or operand l Example: ¡z = 25 + x*(y – 5) ¡ Tokens: z, =, 25, +, x, *, (, y, -, 5, )

Evaluating Postfix Expressions (2) l Evaluation algorithm: ¡ Use stack of tokens ¡ Repeat

Evaluating Postfix Expressions (2) l Evaluation algorithm: ¡ Use stack of tokens ¡ Repeat If operand, push onto stack l If operator l • pop operands off stack • evaluate operator on operands • push result onto stack Until expression is read l Return top of stack l

Evaluating Postfix Expressions (3) l Most CPUs have hardware support for this algorithm l

Evaluating Postfix Expressions (3) l Most CPUs have hardware support for this algorithm l Translation from infix to postfix also uses a stack (software)

Evaluating Postfix Expressions (4) l Original expression: 1 + (2 + 3) * 4

Evaluating Postfix Expressions (4) l Original expression: 1 + (2 + 3) * 4 + 5 l Evaluate: 1 2 3 + 4 * + 5 +

Evaluating Postfix Expressions (5) l Input: 123+4*+5+ l Push(1) 1

Evaluating Postfix Expressions (5) l Input: 123+4*+5+ l Push(1) 1

Evaluating Postfix Expressions (6) l Input: 23+4*+5+ l Push(2) 2 1

Evaluating Postfix Expressions (6) l Input: 23+4*+5+ l Push(2) 2 1

Evaluating Postfix Expressions (7) l Input: 3+4*+5+ l Push(3) 3 2 1

Evaluating Postfix Expressions (7) l Input: 3+4*+5+ l Push(3) 3 2 1

Evaluating Postfix Expressions (8) l Input: +4*+5+ l Pop() == 3 l Pop() ==

Evaluating Postfix Expressions (8) l Input: +4*+5+ l Pop() == 3 l Pop() == 2 1

Evaluating Postfix Expressions (9) l Input: +4*+5+ l Push(2 + 3) 5 1

Evaluating Postfix Expressions (9) l Input: +4*+5+ l Push(2 + 3) 5 1

Evaluating Postfix Expressions (10) l Input: 4*+5+ l Push(4) 4 5 1

Evaluating Postfix Expressions (10) l Input: 4*+5+ l Push(4) 4 5 1

Evaluating Postfix Expressions (11) l Input: *+5+ l Pop() == 4 l Pop() ==

Evaluating Postfix Expressions (11) l Input: *+5+ l Pop() == 4 l Pop() == 5 1

Evaluating Postfix Expressions (12) l Input: *+5+ l Push(5 * 4) 20 1

Evaluating Postfix Expressions (12) l Input: *+5+ l Push(5 * 4) 20 1

Evaluating Postfix Expressions (13) l Input: +5+ l Pop() == 20 l Pop() ==

Evaluating Postfix Expressions (13) l Input: +5+ l Pop() == 20 l Pop() == 1

Evaluating Postfix Expressions (14) l Input: +5+ l Push(1 + 20) 21

Evaluating Postfix Expressions (14) l Input: +5+ l Push(1 + 20) 21

Evaluating Postfix Expressions (15) l Input: 5+ l Push(5) 5 21

Evaluating Postfix Expressions (15) l Input: 5+ l Push(5) 5 21

Evaluating Postfix Expressions (16) l Input: + l Pop() == 21 l Pop() ==

Evaluating Postfix Expressions (16) l Input: + l Pop() == 21 l Pop() == 5

Evaluating Postfix Expressions (17) l Input: + l Push(21 + 5) 26

Evaluating Postfix Expressions (17) l Input: + l Push(21 + 5) 26

Evaluating Postfix Expressions (18) l Input: l Pop() == 26

Evaluating Postfix Expressions (18) l Input: l Pop() == 26

Postfix Evaluation Implementation Evaluate(postfix expression) { // use stack of tokens; while(// expression is

Postfix Evaluation Implementation Evaluate(postfix expression) { // use stack of tokens; while(// expression is not empty) { t = next token; if (t is operand) { // push onto stack } else { // pop operands for t off stack // evaluate t on these operands // push result onto stack } } // return top of stack }

Runtime Stack l Runtime environment ¡ Static heap Executable code l Global variables l

Runtime Stack l Runtime environment ¡ Static heap Executable code l Global variables l ¡ Stack Push for each function call l Pop for each function return l ¡ Heap l Dynamic new and delete stack static program memory

Recursion l Order 1: function calls itself l Order 2: f() calls g(), and

Recursion l Order 1: function calls itself l Order 2: f() calls g(), and g() calls f() l Facilitated by stack

Adaptor Class l Adapts the public interface of another class l Adaptee: the class

Adaptor Class l Adapts the public interface of another class l Adaptee: the class being used l Adaptor: the new class being defined l Uses protected object of the adaptee type l Uses the adaptee’s methods to define adaptor methods l Stack and Queue implemented via adaptor classes

Stack Adaptor Requirements l Stack ¡ Push. Back() ¡ Pop. Back() ¡ Empty() ¡

Stack Adaptor Requirements l Stack ¡ Push. Back() ¡ Pop. Back() ¡ Empty() ¡ Size() l Can use TDeque, TList, TVector

Queue Adaptor Requirements l Queue ¡ Push. Back() ¡ Pop. Front() ¡ Empty() ¡

Queue Adaptor Requirements l Queue ¡ Push. Back() ¡ Pop. Front() ¡ Empty() ¡ Size() l Can use TDeque, TList

Class CStack template <typename T, class Container> class CStack { protected: Container c; public:

Class CStack template <typename T, class Container> class CStack { protected: Container c; public: typedef T value_type; void Push(const value_type& x) { c. Push. Back(x); } void Pop() { c. Pop. Back(); } value_type Top() const { return c. Back(); } int Empty() const { return c. Empty(); } unsigned int Size() const { return c. Size(); } void Clear() { c. Clear(); } void Display(ostream& os, char ofc) const; };

Class CStack (2) template <typename T, class Container> void CStack<T, Container>: : Display(std: :

Class CStack (2) template <typename T, class Container> void CStack<T, Container>: : Display(std: : ostream& os, char ofc) const { typename Container: : Iterator I; if (ofc == ‘’) { for (I = c. Begin(); I != c. End(); ++I) { os << *I; } } else { for (I = c. Begin(); I != c. End(); ++I) { os << *I << ofc; } } } template <typename T, class Container> std: : ostream& operator<<(std: : ostream& os, const CStack<T, Container>& s) { s. Display(os); return os; }

Declarations l CStack<float, TList<float> > float. Stack; l CStack<int, TDeque<int> > int. Stack;

Declarations l CStack<float, TList<float> > float. Stack; l CStack<int, TDeque<int> > int. Stack;

Class CQueue template <typename T, class Container> class CQueue { protected: Container c; public:

Class CQueue template <typename T, class Container> class CQueue { protected: Container c; public: typedef T value_type; void Push(const value_type& x) { c. Push. Back(x); } void Pop() { c. Pop. Front(); } value_type Front() const { return c. Front(); } int Empty() const { return c. Empty(); } unsigned int Size() const { return c. Size(); } void Clear() { c. Clear(); } void Display(ostream& os, char ofc) const; };

Class CQueue (2) template <typename T, class Container> void CQueue<T, Container>: : Display(std: :

Class CQueue (2) template <typename T, class Container> void CQueue<T, Container>: : Display(std: : ostream& os, char ofc) const { typename Container: : Iterator I; if (ofc == ‘’) { for (I = c. Begin(); I != c. End(); ++I) { os << *I; } } else { for (I = c. Begin(); I != c. End(); ++I) { os << *I << ofc; } } } template <typename T, class Container> std: : ostream& operator<<(std: : ostream& os, const CQueue<T, Container>& s) { s. Display(os); return os; }

Example Usage l Functionality Testing ¡ ¡ l fcstack. cpp fcqueue. cpp Performance Testing

Example Usage l Functionality Testing ¡ ¡ l fcstack. cpp fcqueue. cpp Performance Testing ¡ ¡ ¡ qrace. cpp srace. cpp dragstrp. h timer. h datetime. h xran. h