Download presentation
Presentation is loading. Please wait.
Published byMerry Jones Modified over 9 years ago
1
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design
2
2 Objectives Create linked lists Create a stack Create a queue Understand recursion Write recursive methods
3
Visual Basic.NET Programming: From Problem Analysis to Program Design3 Introducing Data Structures Linked list –List of object instances that are linked together –Objects may be added to: Beginning End Somewhere in between
4
Visual Basic.NET Programming: From Problem Analysis to Program Design4
5
5 Introducing Data Structures (continued) Stack –Can be implemented as specialized case of linked list –Objects are added and removed only at beginning of list –Employs LIFO logic Last in, first out
6
Visual Basic.NET Programming: From Problem Analysis to Program Design6
7
7 Introducing Data Structures (continued) Queue –Implemented as linked list –Objects added at end and removed from beginning –Employ FIFO logic First in, first out
8
Visual Basic.NET Programming: From Problem Analysis to Program Design8
9
9 Creating Linked Lists Node –Each instance in linked list contains Data Reference attribute linking it to next node Maintain references to first and last item in list –To facilitate access
10
Visual Basic.NET Programming: From Problem Analysis to Program Design10 Creating Linked Lists (continued) Self-referencing class –Class that references instances of itself
11
Visual Basic.NET Programming: From Problem Analysis to Program Design11
12
Visual Basic.NET Programming: From Problem Analysis to Program Design12
13
Visual Basic.NET Programming: From Problem Analysis to Program Design13
14
Visual Basic.NET Programming: From Problem Analysis to Program Design14
15
Visual Basic.NET Programming: From Problem Analysis to Program Design15 Creating a Stack Stack can be implemented as LinkedList –LIFO structure: Add and remove nodes from only beginning of Stack –Employ inheritance to create Stack class As subclass of LinkedList
16
Visual Basic.NET Programming: From Problem Analysis to Program Design16
17
Visual Basic.NET Programming: From Problem Analysis to Program Design17 Exploring the FCL Stack Class Stores nodes as instances of Object Methods: –Push –Pop –Peek –ToArray
18
Visual Basic.NET Programming: From Problem Analysis to Program Design18 Creating a Queue Queue can be implemented as linked list Objects added only at end Removed only from beginning –Employing FIFO logic Enqueue –Add object Dequeue –Remove object
19
Visual Basic.NET Programming: From Problem Analysis to Program Design19
20
Visual Basic.NET Programming: From Problem Analysis to Program Design20 Creating a Queue (continued) Placing common attributes and methods in superclass eliminates redundancy in subclasses –Count and IsEmpty added to superclass
21
Visual Basic.NET Programming: From Problem Analysis to Program Design21 Exploring the FCL Queue class Queue class –Stores nodes as instances of Object Methods: –Enqueue –Dequeue –Peek –ToArray
22
Visual Basic.NET Programming: From Problem Analysis to Program Design22 Programming Example: Reservation Application Implements reservation system Reservation class methods: –MakeReservation –CancelReservation –ListReservations
23
Visual Basic.NET Programming: From Problem Analysis to Program Design23
24
Visual Basic.NET Programming: From Problem Analysis to Program Design24 Understanding Recursion Programming technique in which method calls itself For some problems recursive approach leads to simple and elegant solution Recursive problem-solving approach –Divides complex problem into successively smaller but similar subproblems –Solves each subproblem –Assembles result
25
Visual Basic.NET Programming: From Problem Analysis to Program Design25 Example 15-8: Using a Recursive Method to Simulate Opening Boxes Module OpenBoxesRecursively Sub Main() 'open largest box (size 10) OpenBox(10) System.Console.WriteLine(”Done”) End Sub
26
Visual Basic.NET Programming: From Problem Analysis to Program Design26 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Sub OpenBox(ByVal boxSize As Integer) If boxSize = 0 Then System.Console.WriteLine(”No more boxes”) Else System.Console.WriteLine(”Box “ & boxSize & _ “ opened”) OpenBox(boxSize - 1) End If
27
Visual Basic.NET Programming: From Problem Analysis to Program Design27
28
Visual Basic.NET Programming: From Problem Analysis to Program Design28 (Continued)
29
Visual Basic.NET Programming: From Problem Analysis to Program Design29 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Recursive algorithm must have stopping point –Called base case –When encountered, recursion stops
30
Visual Basic.NET Programming: From Problem Analysis to Program Design30 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Each time recursive method calls itself –Fresh copy of the method created –Each copy has own set of values and parameters –Remains unfinished until base case is recognized
31
Visual Basic.NET Programming: From Problem Analysis to Program Design31 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Upon each return –Execution resumes within that copy at line of code immediately following recursive call Recursion is similar to iteration –Any problem that can be solved iteratively can also be solved recursively, and vice versa –Recursive solutions are less efficient
32
Visual Basic.NET Programming: From Problem Analysis to Program Design32 Writing Recursive Methods Write recursive method: –Must think recursively –Think of problem in terms of series of successively smaller but virtually identical subproblems –Smallest subproblem must be one that can be solved directly
33
Visual Basic.NET Programming: From Problem Analysis to Program Design33 Computing Factorials Classic example of recursion involves computing factorials Definition: –n! = n * (n – 1) * (n – 2) *... * 1, where n is a positive integer –0! = 1 Thinking recursively: –4! can be defined as 4 * 3!
34
Visual Basic.NET Programming: From Problem Analysis to Program Design34 Computing Factorials (continued) Calculating factorials can produce a number that is too large to be represented in memory space allocated to a particular variable –12! is largest factorial that can be computed using Integer data type to hold result
35
Visual Basic.NET Programming: From Problem Analysis to Program Design35
36
Visual Basic.NET Programming: From Problem Analysis to Program Design36
37
Visual Basic.NET Programming: From Problem Analysis to Program Design37
38
Visual Basic.NET Programming: From Problem Analysis to Program Design38 (Continued)
39
Visual Basic.NET Programming: From Problem Analysis to Program Design39 Displaying a Directory Tree Generate and display tree containing directories and subdirectories on hard drive Depending on size of your hard drive and speed of your computer –Initially creating directory tree may take a few minutes
40
Visual Basic.NET Programming: From Problem Analysis to Program Design40
41
Visual Basic.NET Programming: From Problem Analysis to Program Design41 Sorting an Array Recursive methods are frequently used for array processing Quicksort –Recursive technique for sorting one-dimensional array –Begins by selecting array element to serve as pivot
42
Visual Basic.NET Programming: From Problem Analysis to Program Design42 Sorting an Array (continued) Quicksort (continued) –Position pivot such that All array elements to right are greater than or equal to pivot All array elements to left are less than or equal to pivot –Elements to right and left of pivot form subarrays Sort them using Quicksort
43
Visual Basic.NET Programming: From Problem Analysis to Program Design43 Programming Example: Towers of Hanoi Classic computer science problem Priests in Far Eastern temple were given task of moving stack of 64 disks from one pole to another Each disk was slightly different size –Disks were arranged on pole from top to bottom in order of increasing size –Intermediate pole also available to hold disks
44
Visual Basic.NET Programming: From Problem Analysis to Program Design44 Programming Example: Towers of Hanoi (continued) Priests were to move all 64 disks to third pole using the following rules: –Only one disk can be moved at a time –Removed disk must be placed on one of other poles –Under no circumstance may larger disk be placed upon smaller disk Priests were told that world would end when they completed their task
45
Visual Basic.NET Programming: From Problem Analysis to Program Design45
46
Visual Basic.NET Programming: From Problem Analysis to Program Design46
47
Visual Basic.NET Programming: From Problem Analysis to Program Design47 Programming Example: Towers of Hanoi (continued) Recursive algorithm: –Move top n-1 disks from pole 1 to pole 2, using pole 3 to temporarily hold disks –Move bottom (nth) disk from pole 1 to pole 3 –Move n-1 disks on pole 2 to pole 3, using pole 1 to temporarily hold disks Number of moves required to move the disks is 2 64 -1
48
Visual Basic.NET Programming: From Problem Analysis to Program Design48 Summary Linked list –List of object instances that are linked together –Each instance is called a node Stack –Can be implemented as special form of LinkedList –LIFO structure –FCL provides Stack class
49
Visual Basic.NET Programming: From Problem Analysis to Program Design49 Summary (continued) Queue –Implemented as linked list with objects added at end and removed from beginning –Employ FIFO logic –FCL includes Queue class Recursive problem-solving approach –Divides problem into series of smaller problems –Solves each subproblem –Assembles result
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.