Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element.

Similar presentations


Presentation on theme: "Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element."— Presentation transcript:

1 Chapter 6 Lists Plus Lecture 12

2 What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

3 External Pointer to the Last Node

4 What is a Doubly Linked List? A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

5 Linking the New Node into the List

6 Deleting from a Doubly Linked List What are the advantages of a circular doubly linked list?

7 What are Header and Trailer Nodes? A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key. A Trailer Node is a node at the end of a list that contains a key larger than any possible key. Both header and trailer are placeholding nodes used to simplify list processing.

8 A linked list in static storage ? A Linked List as an Array of Records

9 A Sorted list Stored in an Array of Nodes

10 An Array with Linked List of Values and Free Space

11 An Array with Three Lists (Including the Free List)

12 What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters. The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets,

13 //-------------------------------------------------------- // CLASS TEMPLATE DEFINITION //-------------------------------------------------------- #include "ItemType.h" // for MAX_ITEMS and ItemType template // formal parameter list class StackType { public: StackType( ); bool IsEmpty( ) const; bool IsFull( ) const; void Push( ItemType item ); void Pop( ItemType item ); ItemType Top( ); private: int top; ItemType items[MAX_ITEMS]; };

14 StackType numStack; top 3 [MAX_ITEMS-1]. [ 3 ] 789 [ 2 ] -56 [ 1 ] 132 items [ 0 ] 5670 ACTUAL PARAMETER

15 top 3 [MAX_ITEMS-1]. [ 3 ] 3456.8 [ 2 ] -90.98 [ 1 ] 98.6 items [ 0 ] 167.87 StackType numStack; ACTUAL PARAMETER

16 top 3 [MAX_ITEMS-1]. [ 3 ] Bradley [ 2 ] Asad [ 1 ] Rodrigo items [ 0 ] Max StackType numStack; ACTUAL PARAMETER

17 //-------------------------------------------------------- // SAMPLE CLASS MEMBER FUNCTIONS //-------------------------------------------------------- template // formal parameter list StackType ::StackType( ) { top = -1; } template // formal parameter list void StackType ::Push ( ItemType newItem ) { if (IsFull()) throw FullStack(); top++; items[top] = newItem;// STATIC ARRAY IMPLEMENTATION }

18 Using class templates The actual parameter to the template is a data type. Any type can be used, either built-in or user-defined. Templates are instantiated at run time. Can you see how this might cause a problem?

19 When creating class template Put.h and.cpp code in the same.h file or Have.h include.cpp file at the end

20 Recall Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack. A stack is a LIFO “last in, first out” structure.

21 class StackType StackType Top Pop Push IsFull IsEmpty Private data: topPtr ~StackType 20 30

22 What happens... When a function is called that uses pass by value for a class object like our dynamically linked stack?

23 23 Pass by value makes a shallow copy 20 30 StackType myStack; // CLIENT CODE. MyFunction( myStack ); // function call Private data: 7000 6000 topPtr 7000 Private data: topPtr 7000 myStack shallow copy momeStack

24 What’s the difference? A shallow copy shares the pointed to data with the original class object. A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.

25 Making a deep copy 20 30 Private data: 7000 6000 topPtr 7000 someStack 20 30 Private data: 5000 2000 topPtr 5000 myStack deep copy

26 // FUNCTION CODE template void MyFunction( StackType SomeStack ) // Uses pass by value { ItemType item; SomeStack.Pop(item);. } Suppose MyFunction Uses Pop What happens in a shallow copy?

27 27 MyStack.topPtr is left dangling ? 30 StackType myStack; // CLIENT CODE. MyFunction( myStack ); Private data: topPtr 6000 myStack someStack shallow copy Private data: 7000 6000 topPtr 7000

28 28 MyStack.topPtr is left dangling ? 30 Private data: topPtr 6000 myStack someStack shallow copy Private data: 7000 6000 topPtr 7000 Notice that the shallow copy and the actual parameter myStack, have changed!

29 As a result... This default method used for pass by value is not the best way when a data member points to dynamic data. Instead, you should write what is called a copy constructor, which makes a deep copy of the dynamic data in a different memory location.

30 More about copy constructors When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value. You do not call the copy constructor. Like other constructors, it has no return type. Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition.

31 Copy Constructor Copy constructor is a special member function of a class that is implicitly called in these three situations: passing object parameters by value, initializing an object variable in a declaration, returning an object as the return value of a function.

32 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template class StackType { public: StackType( ); // Default constructor. // Post: Stack is created and empty. StackType( const StackType & anotherStack ); // Copy constructor. // Implicitly called for pass by value.. ~StackType( ); // Destructor. // Post: Memory for nodes has been deallocated. private: NodeType * topPtr; };

33 CLASS CONSTRUCTOR CLASS COPY CONSTRUCTOR CLASS DESTRUCTOR Classes with Data Member Pointers Need

34 template // COPY CONSTRUCTOR StackType :: StackType( const StackType & anotherStack ) {NodeType * ptr1; NodeType * ptr2; if ( anotherStack.topPtr == NULL ) topPtr = NULL; else // allocate memory for first node {topPtr = new NodeType ; topPtr->info = anotherStack.topPtr->info; ptr1 = anotherStack.topPtr->next; ptr2 = topPtr; while ( ptr1 != NULL )// deep copy other nodes {ptr2->next = new NodeType ; ptr2 = ptr2->next; ptr2->info = ptr1->info; ptr1 = ptr1->next; } ptr2->next = NULL; } }

35 What About the Assignment Operator? The default method used for assignment of class objects makes a shallow copy. If your class has a data member pointer to dynamic data, you should write a member function to overload the assignment operator to make a deep copy of the dynamic data.

36 // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template class StackType { public: StackType( ); // Default constructor. StackType(const StackType & anotherStack ); // Copy constructor. void operator= ( StackType ); // Overloads assignment operator.. ~StackType( ); // Destructor. private: NodeType * topPtr; };

37 Using Overloaded Binary operator+ When a Member Function was defined myStack + yourStack myStack.operator+(yourStack) When a Friend Function was defined myStack + yourStack operator+(myStack, yourStack)

38 C++ Operator Overloading Guides 1All operators except these ::. sizeof ?: may be overloaded. 2At least one operand must be a class instance. 3You cannot change precedence, operator symbols, or number of operands. 4Overloading ++ and -- requires prefix form use by default. 6An operator can be given multiple meanings if the data types of operands differ.

39 Polymorphism with Virtual Functions formalParameter.MemberFunction(...); then 1.If MemberFunction is not a virtual function, the type of the formal parameter determines which function to call. Static binding is used. 2.If MemberFunction is a virtual function, the type of the actual parameter determines which function to call. Dynamic binding is used.

40 class ItemType { public: virtual RelationType ComparedTo(ItemType) const; private: char lastName[50] ; } ; class NewItemType : public ItemType { public: RelationType ComparedTo( ItemType) const; private: char firstName[50] ; } ; void PrintResult( ItemType& first, ItemType& second) { using namespace std; if ( first. ComparedTo( second) ==LESS) cout << " First comes before second" ; else cout << " First does not come before second" ; } ItemType item1, item2; NewItemType item3, item4: PrintResult( item1, item2) ; PrintResult( item3, item4) ; You should pass by reference to access derived data members

41 Chapter 7 Programming with Recursion

42

43

44

45 What Is Recursion? Recursive call A method call in which the method being called is the same as the one making the call Direct recursion Recursion in which a method directly calls itself Indirect recursion Recursion in which a chain of two or more method calls returns to the method that originated the chain

46 Recursion You must be careful when using recursion. Recursive solutions are typically less efficient than iterative solutions. Still, many problems lend themselves to simple, elegant, recursive solutions. We must avoid making an infinite sequence of function calls infinite recursion

47 Some Definitions Base case The case for which the solution can be stated nonrecursively General (recursive) case The case for which the solution is expressed in terms of a smaller version of itself Recursive algorithm A solution that is expressed in terms of (a) smaller instances of itself and (b) a base case

48 Writing a recursive function to find n factorial DISCUSSION The function call Factorial(4) should have value 24, because that is 4 * 3 * 2 * 1. For a situation in which the answer is known, the value of 0! is 1. So our base case could be along the lines of if ( number == 0 ) return 1;

49 General format for many recursive functions if (some condition for which answer is known) // base case solution statement else // general case recursive function call Each successive recursive call should bring you closer to a situation in which the answer is known. Each recursive algorithm must have at least one base case, as well as the general (recursive) case

50 Writing a recursive function to find Factorial(n) Now for the general case... The value of Factorial(n) can be written as n * the product of the numbers from (n - 1) to 1, that is, n * (n - 1) *... * 1 or, n * Factorial(n - 1) And notice that the recursive call Factorial(n - 1) gets us “closer” to the base case of Factorial(0).

51 Recursive Solution int Factorial ( int number ) // Pre: number is assigned and number >= 0. { if ( number == 0)// base case return 1 ; else// general case return number + Factorial ( number - 1 ) ; }

52 Three-Question Method of verifying recursive functions Base-Case Question: Is there a nonrecursive way out of the function? Smaller-Caller Question: Does each recursive function call involve a smaller case of the original problem leading to the base case? General-Case Question: Assuming each recursive call works correctly, does the whole function work correctly?

53 Recursive function to determine if value is in list PROTOTYPE bool ValueInList( ListType list, int value, int startIndex ) ; Already searched Needs to be searched 74 36... 95 list[0] [1] [startIndex] 75 29 47... [length -1] index of current element to examine

54 bool ValueInList ( ListType list, int value, int startIndex ) // Searches list for value between positions startIndex // and list.length-1 // Pre: list.info[ startIndex ].. list.info[ list.length - 1 ] // contain values to be searched // Post: Function value = // ( value exists in list.info[ startIndex ].. // list.info[ list.length - 1 ] ) { if ( list.info[startIndex] == value ) // one base case return true ; else if (startIndex == list.length -1 ) // another base case return false ; else // general case return ValueInList( list, value, startIndex + 1 ) ; }

55 “Why use recursion?” Those examples could have been written without recursion, using iteration instead. The iterative solution uses a loop, and the recursive solution uses an if statement. However, for certain problems the recursive solution is the most natural solution. This often occurs with pointer variables.

56 An example where recursion comes naturally Combinations how many combinations of a certain size can be made out of a total group of elements

57 int Combinations(int group, int members) // Pre: group and members are positive. group >= members // Post: Function value = number of combinations of members // size that can be constructed from the total group size. { if (members == 1) return group; // Base case 1 else if (members == group) return 1; // Base case 2 else return (Combinations(group-1, members-1) + Combinations(group-1, members) ) ; }

58 Combinations(4, 3)

59 struct NodeType { int info ; NodeType* next ; } class SortedType { public :...// member function prototypes private : NodeType* listData ; } ; struct ListType

60 RevPrint(listData); A B C D E FIRST, print out this section of list, backwards THEN, print this element listData

61 61 Using recursion with a linked list void RevPrint ( NodeType* listPtr ) // Pre: listPtr points to an element of a list. // Post: all elements of list pointed to by listPtr // have been printed out in reverse order. { if ( listPtr != NULL ) // general case { RevPrint ( listPtr-> next ); // process the rest std::cout info << std::endl; // print this element } // Base case : if the list is empty, do nothing }

62 Function BinarySearch( ) BinarySearch takes sorted array info, and two subscripts, fromLoc and toLoc, and item as arguments. It returns false if item is not found in the elements info[fromLoc…toLoc]. Otherwise, it returns true. BinarySearch can be written using iteration, or using recursion.

63 found = BinarySearch(info, 25, 0, 14 ); item fromLoc toLoc indexes 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 info 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 16 18 20 22 24 26 28 24 26 28 24 NOTE: denotes element examined

64 template bool BinarySearch ( ItemType info[ ], ItemType item, int fromLoc, int toLoc ) // Pre: info [ fromLoc.. toLoc ] sorted in ascending order // Post: Function value = ( item in info [ fromLoc.. toLoc] ) { int mid ; if ( fromLoc > toLoc ) // base case -- not found return false ; else { mid = ( fromLoc + toLoc ) / 2 ; if ( info [ mid ] == item ) //base case-- found at mi return true ; else if ( item < info [ mid ] ) // search lower half return BinarySearch ( info, item, fromLoc, mid-1 ) ; else // search upper half return BinarySearch( info, item, mid + 1, toLoc ) ; } }

65 Static allocation of space Recursion is not possible variable names should be bound to actual addresses in memory at run time

66 When a function is called... A transfer of control occurs from the calling block to the code of the function. It is necessary that there be a return to the correct place in the calling block after the function code is executed. This correct place is called the return address. When any function is called, the run-time stack is used. On this stack is placed an activation record (stack frame) for the function call.

67 Stack Activation Frames The activation record stores the return address for this function call, the parameters, local variables, and the function’s return value, if non-void. The activation record for a particular function call is popped off the run-time stack when the final closing brace in the function code is reached, or when a return statement is reached in the function code. At this time the function’s return value, if non-void, is brought back to the calling block return address for use there.

68 // Another recursive function int Func ( int a, int b ) // Pre: a and b have been assigned values // Post: Function value = ?? { int result; if ( b == 0 ) // base case result = 0; else if ( b > 0 ) // first general case result = a + Func ( a, b - 1 ) ); // instruction 50 else // second general case result = Func ( - a, - b ); // instruction 70 return result; } What operation does Func(a, b) simulate?

69 FCTVAL ? result ? b 2 a 5 Return Address 100 x = Func(5, 2); // original call is instruction 100 original call at instruction 100 pushes on this record for Func(5,2) Run-Time Stack Activation Records

70 FCTVAL ? result ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2) call in Func(5,2) code at instruction 50 pushes on this record for Func(5,1) x = Func(5, 2); // original call at instruction 100 Run-Time Stack Activation Records

71 FCTVAL ? result ? b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2)record for Func(5,1) call in Func(5,1) code at instruction 50 pushes on this record for Func(5,0) x = Func(5, 2); // original call at instruction 100 Run-Time Stack Activation Records

72 FCTVAL 0 result 0 b 0 a 5 Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2) record for Func(5,1) record for Func(5,0) is popped first with its FCTVAL x = Func(5, 2); // original call at instruction 100 Run-Time Stack Activation Records

73 FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5 Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5 Return Address 100 record for Func(5,2)record for Func(5,1) is popped next with its FCTVAL x = Func(5, 2); // original call at instruction 100 Run-Time Stack Activation Records

74 FCTVAL 10 result 5+Func(5,1) = 5+5 b 2 a 5 Return Address 100 x = Func(5, 2); // original call at line 100 record for Func(5,2) is popped last with its FCTVAL Run-Time Stack Activation Records

75 Tail Recursion The case in which a function contains only a single recursive call and it is the last statement to be executed in the function. Tail recursion can be replaced by iteration to remove recursion from the solution as in the next example.

76 // USES TAIL RECURSION bool ValueInList ( ListType list, int value, int startIndex ) // Searches list for value between positions startIndex // and list.length-1 // Pre: list.info[ startIndex ].. list.info[ list.length - 1 ] // contain values to be searched // Post: Function value = // ( value exists in list.info[ startIndex ].. // list.info[ list.length - 1 ] ) { if ( list.info[startIndex] == value )// one base case return true; else if (startIndex == list.length -1 ) // another base case return false; else return ValueInList( list, value, startIndex + 1 ); }

77 // ITERATIVE SOLUTION bool ValueInList ( ListType list, int value, int startIndex ) // Searches list for value between positions startIndex // and list.length-1 // Pre: list.info[ startIndex ].. list.info[ list.length - 1 ] // contain values to be searched // Post: Function value = // ( value exists in list.info[ startIndex ].. // list.info[ list.length - 1 ] ) { bool found = false; while ( !found && startIndex < list.length ) { if ( value == list.info[ startIndex ] ) found = true; elsestartIndex++; } return found; }

78 Use a recursive solution when: The depth of recursive calls is relatively “shallow” compared to the size of the problem. The recursive version does about the same amount of work as the nonrecursive version. The recursive version is [much] shorter and simpler than the nonrecursive solution. SHALLOW DEPTH EFFICIENCY CLARITY


Download ppt "Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element."

Similar presentations


Ads by Google