Notes From CS Undergrad Courses FSU
This project is maintained by awa03
#include <stack>
// Stack operations
stack<T> stackExample;
stackExample.push();
stackExample.pop();
stackExample.top();
stackExample.empty();
stackExample.size();
// with constructor & destructor
![[Illustrations-with-traversals-step-3-1.png]]
Keep Going Deeper
// Depth First Search
DFS() {
stack<location> S;
//Mark the start location as visited
S.push(start);
while (!S.empty()) {
t = S.top();
if (t == goal) Success(S);
if (// t has unvisited neighbors) {
//Choose an unvisited neighbor n
// mark n visited;
S.push(n);
} else {
BackTrack(S);
}
}
Failure(S);
}
/*
Another Implementation Of DFS
____________________________________________________
*/
BackTrack(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();
}
}
Evaluate(postfix expression) {
// use stack of tokens;
while(// expression is not empty) {
t = next token;
if (t is operand) {
// push onto the stack
} else {
// pop operands for t off stack
// evaluate t on these operands
// push the result onto the stack
}
}
// return top of stack
}
![[postfix-evaluation-tn.webp]]