630 likes | 668 Vues
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors. Stacks and Queues. Sections 3.6 and 3.7. Stack ADT - LIFO. Collections: Elements of some proper type T Operations: Feature: Last In, First Out void push(T t) void pop() T top() bool empty() unsigned int size()
E N D
Chapter 3 Lists, Stacks, and QueuesAbstract Data Types, Vectors Stacks and Queues Sections 3.6 and 3.7
Stack ADT - LIFO • Collections: • Elements of some proper type T • Operations: • Feature: Last In, First Out • void push(T t) • void pop() • T top() • bool empty() • unsigned int size() • constructor and destructor
Stack Model—LIFO • Empty stack S • S.empty() is true • S.top() not defined • S.size() == 0 food chain stack
Stack Model—LIFO • S.push(“mosquito”) • S.empty() is false • S.top() == “mosquito” • S.size() == 1 food chain stack
Stack Model—LIFO • S.push(“fish”) • S.empty() is false • S.top() == “fish” • S.size() == 2 food chain stack
Stack Model—LIFO • S.push(“raccoon”) • S.empty() is false • S.top() == “raccoon” • S.size() == 3 food chain stack
Stack Model—LIFO • S.pop() • S.empty() is false • S.top() == “fish” • S.size() == 2 food chain stack
Implementations and Uses of Stack ADT • Implementations • Any list implementation • list and vector C++ STL • Vector/List ADTs • push_back()/pop_back() • push_front()/? • Uses • Depth first search / backtracking • Evaluating postfix expressions • Converting infix to postfix • Function calls (runtime stack) • Recursion
Problem Discover a path from start to goal Solution Start from Node start Stop If node is goal Go deep If there is an unvisited neighbor, go there Backtrack Retreat along the path to find an unvisited neighbor, if cannot go deeper Outcome If there is a path from start to goal, DFS finds one such path Depth First Search—Backtracking 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Depth First Search—Backtracking (2) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 goal Push
Depth First Search—Backtracking (3) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push
Depth First Search—Backtracking (4) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (5) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (6) • Stack 1 start Push 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (7) • Stack 1 start Pop 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (8) • Stack 1 start 2 3 4 Pop 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (9) • Stack 1 start 2 3 4 5 6 7 8 Pop 9 10 11 12 Push goal Push
Depth First Search—Backtracking (10) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Pop goal Push
Depth First Search—Backtracking (11) • Stack 1 start 2 3 4 5 6 7 8 9 10 11 12 Push goal Push
Depth First Search—Backtracking (12) • Stack 1 start 2 3 4 5 6 7 8 Push 9 10 11 12 Push goal Push
Depth First Search—Backtracking (13) • Stack 1 start 2 3 4 Push 5 6 7 8 Push 9 10 11 12 Push goal Push
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 n // mark n visited; S.push(n); } else { BackTrack(S); } } Failure(S); }
DFS Implementation (2) 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(); } }
Runtime Stack • Runtime environment • Static • Executable code • Global variables • Stack • Push for each function call • Pop for each function return • Local variables • Heap • Dynamically allocated memories • new and delete heap stack static program memory
Queue ADT - FIFO • Collection • Elements of some proper type T • Operations • Feature: First In, First Out • void push(T t) • void pop() • T front() • bool empty() • unsigned int size() • Constructors and destructors
Queue Model—FIFO • Empty Q animal parade queue
front back Queue Model—FIFO • Q.Push(“ant”) animal parade queue
front back Queue Model—FIFO • Q.Push(“bee”) animal parade queue
front back Queue Model—FIFO • Q.Push(“cat”) animal parade queue
front back Queue Model—FIFO • Q.Push(“dog”) animal parade queue
front back Queue Model—FIFO • Q.Pop() animal parade queue
front back Queue Model—FIFO • Q.Pop() animal parade queue
front back Queue Model—FIFO • Q.Push(“eel”) • Q.Pop() • Q.Pop() animal parade queue
Implementations and Uses of Queue ADT • Implementations • Any list implementation • push_front()/pop_back() • push_back()/? • Uses • Buffers • Breadth first search • Simulations • Producer-Consumer Problems
Breadth First Search • Problem • Find a shortest path from start to goal • Solution • Start from • Node start • Visit • All neighbors of the node • Stop • If a neighbor is goal • Otherwise • Visit neighbors two hops away • Repeat (Stop/Otherwise) • Visiting neighbors N hops away 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (2) • Queue Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (3) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (4) • Queue Push Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (5) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (6) • Queue Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (7) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (8) • Queue Push Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (9) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (10) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (11) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (12) • Queue Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (13) • Queue Pop 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
Breadth First Search (14) • Queue Push 1 start 2 3 4 5 6 7 8 9 10 11 12 goal
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 of node t) { Q.push(n); if (n == goal) Success(S); } Q.pop(); } Failure(Q); }
Adaptor Class • Adapts the public interface of another class • Adaptee: the class being used • Adaptor: the new class being defined • Uses protected object of the adaptee type • Uses the adaptee’s methods to define adaptor methods • Stack and Queue implemented via adaptor classes