Download presentation
Presentation is loading. Please wait.
1
Chapter 6 – Queues and Deques
Lab 06 – Railroad 7.1 Recursive Thinking Chapter 6 – Queues and Deques
2
Attendance Quiz #22 Recursion
3
Tip #24: For Speed Freaks Recursion 1) Checking even or odd without using the % operator: if (num & 1) cout << "ODD"; else cout << "EVEN"; 2) Fast Multiplication or Division by 2: Multiplying by 2 means shifting all the bits to left and dividing by 2 means shifting to the right. n = n << 1; // Multiply n with 2 n = n >> 1; // Divide n by 2 3) Swapping of 2 numbers using XOR: This method is fast and doesn’t require the use of 3rd variable. a ^= b; b ^= a; 4) Avoiding use of strlen(): Use of strlen() can be avoided by for (i = 0; s[i]; i++) // { }
4
Lab 06 – Railroad
5
Lab 06 - Railroad Recursion Create a railroad train station that uses a central turntable and roundhouses to store train cars as well as changes the order of cars entering and leaving the station. Cars may enter the train station if the turntable is empty. From the turntable cars can be moved to a "stack" roundhouse facility (LIFO) or a "queue" roundhouse facility (FIFO). Train cars leave the train station by moving from the turntable to a "vector" of outbound cars. What Objects? How to describe them? UML
6
DequeInterface<T>
Vector<T> -container__:Deque<T> +Vector() +~Vector() +push_back(const T&):void +pop_back():void +back():T& +size():size_t +at(size_t):T& +toString() const:string <<interface>> DequeInterface<T> +static const size_t DEFAULT_CAPACITY = 4 +DequeInterface() +~DequeInterface() +push_front(const T&):void +push_back(const T&):void +pop_front():void +pop_back():void +front():T& +back():T& +size():size_t +empty() const:bool +at(size_t):T& +toString() const:string Station<T> -train_:Vector<T> -stack_:Stack<T> -queue_:Queue<T> -turnTableCar_:T -empty:bool +Station() +~Station() +addCar(const T&):string +removeCar():string +topCar():string +addStack():string +removeStack():string +topStack():string +sizeStack():string +addQueue():string +removeQueue():string +topQueue():string +sizeQueue():string +find(T):string +toString() const:string Deque<T> -capacity:size_t -num_items:size_t -front_index:size_t -rear_index:size_t -the_data:T* -reallocate():void +Deque() +~Deque() +push_front(const T&):void +push_back(const T&):void +pop_front():void +pop_back():void +front():T& +back():T& +size():size_t +empty() const:bool +at(size_t):T& +toString() const:string Queue<T> -container__:Deque<T> +Queue() +~Queue() +push(const T&):string +pop():void +top():T& +size():size_t +at(size_t):T& +toString() const:string Stack<T> -container__:Deque<T> +Stack() +~Stack() +push(const T&):string +pop():void +top():T& +size():size_t +at(size_t):T& +toString() const:string
7
Circular Buffer capacity = 16 num_items = 5 the_data L O H E
Recursion capacity = 16 num_items = 5 the_data L O H E rear_index front_index front_index the_data rear_index
8
Specification of the Deque
Recursion Behavior Member Function Insert element at back void push_back(item) Insert element at front void push_front(item) Remove last element void pop_back(); Remove first element void pop_front(); Examine last element item& back(); Examine first element item& front(); Index item& at(index); Size size_t size(); *Insert element iterator insert(iterator, item); *Remove all items void remove(item); *Index item& [] *Iterator start iterator begin(); *Iterator end iterator end(); *Not Required for Lab 06 Deque
9
Wrapper Class Recursion A wrapper class is a class that encapsulates or contains another class and uses its functionality to define the class' behavior. #include "Deque.h" template <typename T> class Stack { private: Deque<T> stack_; public: void push(T data) stack_.push_back(data); } void pop(void) return stack_.pop_back(); ... }; #include "Deque.h" template <typename T> class Queue { private: Deque<T> queue_; public: void push(T data) queue_.push_back(data); } void pop(void) return queue_.pop_front(); ... };
10
L06 – Railroad Wrapper Classes
Recursion Input Commands Station 235 Station<T> string addCar(const T&); string addStack(); string addQueue(); string removeCar(); string removeStack(); string removeQueue(); string topCar(); string topStack(); string topQueue(); string sizeStack(); string sizeQueue(); string find(T); string toString() const; "Add:station xx" "Add:stack" "Add:queue" "Remove:station" "Remove:stack" "Remove:queue" "Top:station" "Top:stack" "Top:queue" "Size:stack" "Size:queue" "Find:xx" "Train:" Vector<T>, Queue<T>, Stack<T> string push(const T&); void pop(); void top(); size_t size(); T& at(size_t); string toString() const; Deque<T> void push_front(const T&); void push_back(const T&); void pop_front(); void pop_back(); T& front(); T& back(); size_t size(); bool empty() const; T& at(size_t); string toString() const; <<interface>> DequeInterface<T> typedef unsigned int size_t;
11
Station Commands Recursion COMMAND DESCRIPTION OUTPUT
Add:station <data> Add:queue <data> Add:stack <data> Train car enters the station turntable. Train car is removed from the turntable and pushed to the Queue roundhouse. Train car is removed from the turntable and pushed to the Stack roundhouse. OK Turntable occupied! Turntable empty! Remove:station Remove:queue Remove:stack A train car is removed from the turntable and pushed into the train vector. A train car is removed from Queue roundhouse and moved to the station turntable. A train car is removed from Stack roundhouse and moved to the station turntable. OK Turntable empty! Turntable occupied! Queue empty! Stack empty! Top:station Top:queue Top:stack Display the current train car on station turntable, Display the train car at head of Queue roundhouse. Display the train car at head of Stack roundhouse. <data> Turntable empty! Queue empty! Stack empty! Size:queue Size:stack Output number of train cars in Queue/Stack roundhouse. Size of Queue/Stack Find: <data> Find and display the current location and position of a car in the station data structures (turntable, queue, stack, or vector). Turntable Queue[<index>] Stack[<index>] Train[<index>] Not Found! Train: Output the contents of the train vector. List of train cars leaving the station.
12
Lab Requirements Recursion 5
argv[1] and argv[2] used for input / output streams respectively. A Deque template class is correctly implemented using a dynamic circular array and is derived from the abstract DequeInterface interface class. All station facility containers (stack, queue, vector) are separate classes that are implemented by "wrapping" your Deque template class. As train cars exit the station turntable, they are pushed on a Train vector template class that has-a Deque container and operates as described above (lab06_in_01.txt). The Stack roundhouse consists of a LIFO template class that has-a Deque container and operates as described above (lab06_in_02.txt). The Queue roundhouse consists of a FIFO template class that has-a Deque container and operates as described above (lab06_in_03.txt). All the station containers (Queue, Stack, Vector) resize correctly (lab06_in_04.txt). (5) BONUS: The Find command finds a train car in the Station data structures (queue or stack) or the train vector. If found, the name of the structure and the index within the structure is displayed (as described above.) (lab06_in_05.txt). VS_MEM_CHECK macro is included in main to detect memory leaks. No Memory leaks are reported.
13
Chapter 7 Recursion
14
Objectives To understand how to think recursively.
Recursion To understand how to think recursively. To learn how to trace a recursive function. To learn how to write recursive algorithms and functions for searching vectors. To understand how to use recursion to solve the Towers of Hanoi problem. To understand how to use recursion to process two- dimensional images. To learn how to apply backtracking to solve search problems such as finding a path through a maze.
15
Objectives Recursion Recursion can solve many programming problems that are difficult to conceptualize and solve linearly. In the field of artificial intelligence, recursion often is used to write programs that exhibit intelligent behavior: playing games of chess. proving mathematical theorems. recognizing patterns, and so on. Recursive algorithms can compute factorials. compute a greatest common divisor. process data structures (strings, vectors, linked lists, etc.). search efficiently using a binary search. find a path through a maze, and more.
16
7.1, pgs. 404-411 7.1 Recursive Thinking
Steps to Design a Recursive Algorithm Proving That a Recursive Function Is Correct Tracing a Recursive Function The Stack and Activation Frames 7.1, pgs
17
Recursive Thinking Recursion Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that are difficult to solve by other means. Recursion reduces a problem into one or more simpler versions of itself. Recursive Algorithm to Process Nested Figures: 1. if there is one figure in the nest Do whatever is required to the figure. else Do whatever is required to the outer figure in the nest. Process the nest of figures inside the outer figure in the same way.
18
Recursive Thinking Consider searching for a target value in an vector.
Recursion Consider searching for a target value in an vector. Assume the vector elements are sorted in increasing order. We compare the target to the middle element and, if the middle element does not match the target, search either the elements before the middle element or the elements after the middle element. Instead of searching n elements, we search n/2 elements. Recursive Algorithm to Search an vector if the vector is empty Return -1 as the search result else if the middle element matches the target Return the subscript of the middle element as the result else if the target is less than the middle element Recursively search the vector elements preceding the middle element and return the result else Recursively search the vector elements following the
19
Recursive Thinking General Recursive Algorithm:
Recursion General Recursive Algorithm: Step 1 involves a test for what is called the base case: a value of n for which the problem can be solved easily Whenever a split occurs, we revisit Step 1 for each new problem to see whether it is a base case or a recursive case if problem can be solved directly for the current value of n 2. then Solve it. else 3. Recursively apply the algorithm to one or more problems involving smaller values of n. 4. Combine the solutions to the smaller problems to get the solution to the original problem. Step 3 is the recursive case, because there we recursively apply the algorithm Because the value of n for each recursive case is smaller than the original value of n, each recursive case makes progress towards a base case
21
18 – Sequential Containers
22
L06 – Railroad Wrapper Classes
Recursion Station<T> string addCar(const T&); string addStack(); string addQueue(); string removeCar(); string removeStack(); string removeQueue(); string topCar(); string topStack(); string topQueue(); string sizeStack(); string sizeQueue(); string find(T); string toString() const; "Add:station xx" "Add:stack" "Add:queue" "Remove:station" "Remove:stack" "Remove:queue" "Top:station" "Top:stack" "Top:queue" "Size:stack" "Size:queue" "Find: xx" "Train:" Vector<T>, Queue<T>, Stack<T> string push(const T&); void pop(); void top(); size_t size(); T& at(size_t); string toString() const; Deque<T> void push_front(const T&); void push_back(const T&); void pop_front(); void pop_back(); T& front(); T& back(); size_t size(); bool empty() const; T& at(size_t); string toString() const; <<interface>> DequeInterface<T>
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.