






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Stack ADT
Uses of ADT Stack
Stack ModelāLIFO
ļ” S.empty() is true ļ” S.top() not defined ļ” S.size() == 0
ļ” S.empty() is false ļ” S.top() == āmosquitoā ļ” S.size() == 1
ļ” S.empty() is false ļ” S.top() == āfishā ļ” S.size() == 2 (^) food chain stack Stack ModelāLIFO
food chain stack Queue ADT - FIFO
Queue ModelāFIFO
animal_parade_queue front back Queue ModelāFIFO
front back animal_parade_queue back back back back Queue ModelāFIFO
front animal_parade_queue back back back back Uses of ADT Queue
Depth First SearchāBacktracking
ļ” Discover a path from start to goal
ļ” Go deep ļ¬ If there is an unvisited neighbor, go there ļ” Backtrack ļ¬ Retreat along the path to find an unvisited neighbor
ļ” If there is a path from start to goal , DFS finds one such path
start goal Depth First SearchāBacktracking (2)
start goal
Push
Depth First SearchāBacktracking (9)
start goal
Push Pop Depth First SearchāBacktracking (10)
start goal
Push Pop Depth First SearchāBacktracking (11)
start goal
Push Depth First SearchāBacktracking (12)
start goal
Push Push Push Depth First SearchāBacktracking (13)
start goal
Push Push Push Push DFS Implementation DFS() { stack
DFS Implementation (2) BackTrack while (!S.empty() && S.top() has no unvisited neighbors) {(S) { }^ 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
start goal Breadth First Search (2)
start goal
push Breadth First Search (3)
start goal pop Breadth First Search (4)
start goal
pop pop pop Breadth First Search (5)
start goal pop
Breadth First Search (12)
start goal
Push Breadth First Search (13)
start goal
Pop Breadth First Search (14)
start goal
Push BFS Implementation BFS { queue
Evaluating Postfix Expressions (2)
ļ”Use stack of tokens ļ”Repeat
Evaluating Postfix Expressions (3)
Evaluating Postfix Expressions (4)
Evaluating Postfix Expressions (5)
Evaluating Postfix Expressions (6)
Evaluating Postfix Expressions (7)
Evaluating Postfix Expressions (8)
Evaluating Postfix Expressions (15)
Evaluating Postfix Expressions (16)
Evaluating Postfix Expressions (17)
Evaluating Postfix Expressions (18)
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 } Reading Exercise
ļ”Balance brackets ļ”Convert infix to postfix
Runtime Stack
ļ”Static
ļ”Stack
ļ”Heap
static stack heap program memory Recursion
ļ”function calls itself
ļ” f() calls g() , and g() calls f()
Adaptor Class
Stack Adaptor Requirements
ļ” push() ļ” pop() ļ” top() ļ” empty() ļ” size()
Queue Adaptor Requirements
ļ” push() ļ” pop () ļ” front() ļ” back() ļ” empty() ļ” size()
Class Stack template <typename T, class Container> class Stack { protected: Container c; public: void push(const T & x) { c.push_back(x); } void pop() { c.pop_back(); } T top() const { return c.back(); } int empty() const { return c.empty(); } unsigned int size() const { return c.size(); } void clear() { c.clear(); } };