Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Classes and Data Structures Jeffrey S. Childs

Similar presentations


Presentation on theme: "C++ Classes and Data Structures Jeffrey S. Childs"— Presentation transcript:

1 C++ Classes and Data Structures Jeffrey S. Childs
Chapter 7 Methods for Making Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall

2 Dynamic Arrays in Data Structures
In the class for a data structure, we can add an Array object to the private section to store data The functions of the data structure can expand or shrink the Array to conserve memory – this relieves the client from thinking about doing this

3 Dynamic Arrays in Data Structures (cont.)
In almost every data structure, we want functions for inserting and removing data When dynamic arrays are used, the insertion function would add data to the array, while the removal function would “eliminate” data from the array (make it unusable) When the array becomes full, we would want to do an expansion – when many elements have been removed, we would want to do a contraction, so that only the used elements remain

4 Array Expansion/Contraction
One possible method: When an element is inserted by the client, increase the size of the array by 1 When an element is removed by the client, decrease the size of the array by 1 The problem with this method is that it is inefficient – every time an element is inserted or removed, the changeSize function is called…

5 changeSize Function 33 25 75 10 12 56 32 73 87 New element needs to be put into array, so changeSize function is called

6 changeSize Function (cont.)
25 75 10 12 56 32 73 87 new array is made

7 changeSize Function (cont.)
25 75 10 12 56 32 73 87 25 75 10 12 56 32 73 87 elements are copied over one by one using a for loop

8 changeSize Function (cont.)
33 25 75 10 12 56 32 73 87 33 Then, the new element can be put in

9 changeSize Function (cont.)
25 75 10 12 56 32 73 87 33 This process would take place every time a new element needs to be inserted.

10 changeSize Function (cont.)
25 75 10 12 56 32 73 87 33 Likewise, when an element needs to be removed, this method contracts the array by one to conserve memory.

11 changeSize Function (cont.)
25 75 10 12 56 32 73 87 33 Suppose the element at the end of the array needs to be removed.

12 changeSize Function (cont.)
25 75 10 12 56 32 73 87 33 The changeSize function is called and a new, smaller array is made.

13 changeSize Function (cont.)
25 75 10 12 56 32 73 87 33 25 75 10 12 56 32 73 87 The elements are copied over one by one, using a for loop.

14 changeSize Function (cont.)
25 75 10 12 56 32 73 87 This method of array expansion/contraction is largely inefficient, because there is too much element copying.

15 A Better Method When the Array is full, double the size of it
When the number of elements used in the Array falls to 25% of the Array’s capacity, cut the size of the Array in half (it will be half full after the cut)

16 A Better Method (cont.) 25 75

17 A Better Method (cont.) 25 75 10

18 A Better Method (cont.) 25 75 10 12

19 A Better Method (cont.) 33 25 75 10 12 Array is full, so call changeSize function to double the size.

20 A Better Method (cont.) 25 75 10 12 33

21 A Better Method (cont.) 25 75 10 12 33 49 29 87 This array is full, but if we removed elements (made them inaccessible), we would cut the size of the array in half when its utilization drops to 25%

22 A Better Method (cont.) 25 75 10 12 33 49 29

23 A Better Method (cont.) 25 75 10 12 33 49

24 A Better Method (cont.) 25 75 10 12 33

25 A Better Method (cont.) 25 75 10 12

26 A Better Method (cont.) 25 75 10

27 A Better Method (cont.) 25 75 Array is 25% utilized, so use changeSize function to cut the size of the array in half.

28 A Better Method (cont.) 25 75 Array is 25% utilized, so use changeSize function to cut the size of the array in half.

29 A Better Method (cont.) 25 75 Using this method, memory is still conserved. There is element copying every time changeSize is called, but it isn’t bad.

30 A Better Method (cont.) 25 75 It can be proven that, on average, there are no more than a couple of elements being copied on each insertion/deletion with this method.

31 Linked Structures In a data structure, data is not always stored in an Array object Sometimes it is best to store data in a linked structure (an alternative to an Array) A linked structure consists of a group of nodes – each node is made from a struct. An object of the Node struct contains an element of data.

32 A Node Struct Template template <class DataType> struct Node {
DataType info; Node<DataType> *next; }; The info member is for the data. It can anything (DataType), but it is often the object of another struct, used as a record of information.

33 A Node Struct Template (cont.)
template <class DataType> struct Node { DataType info; Node<DataType> *next; }; The next pointer stores the address of a Node of the same type! This means that each node can point to another node.

34 Nodes In a data structure, each node is made in the heap; therefore, a node can only be accessed by a pointer. The client does not deal with nodes. When the client uses an insertion function, an element of data is passed into the function, and the function places it in a node.

35 Nodes (cont.) When the client wants to retrieve data, the data in a node is returned to the client (but not the node itself). The node struct template exists for use by the data structure.

36 Example of a Linked Structure
start Each blue node is divided into two sections, for the two members of the Node struct.

37 Example of a Linked Structure (cont.)
start The left section is the info member.

38 Example of a Linked Structure (cont.)
start The right section is the pointer called “next”.

39 Example of a Linked Structure (cont.)
start The start pointer would be saved in the private section of a data structure class.

40 Example of a Linked Structure (cont.)
start The last node doesn’t point to another node, so its pointer (called next) is set to NULL (indicated by slash).

41 Linked Lists The arrangement of nodes in the linked structure on the previous slide is often called a linked list. We can access any element of the linked list, for retrieval of information. We can also remove any element from the linked list (which would shorten the list). We can also insert any element into any position in the linked list.

42 Linked List Advantages
5 3 7 2 1 Removing an element from the middle of a linked list is fast.

43 Linked List Advantages (cont.)
5 3 2 1 Removing an element from the middle of a linked list is fast.

44 Linked List Advantages (cont.)
25 75 10 12 33 49 29 87 Removing elements from the middle of an array (without leaving gaps) is more problematic.

45 Linked List Advantages (cont.)
25 75 10 33 49 29 87 Removing elements from the middle of an array (without leaving gaps) is more problematic.

46 Linked List Advantages (cont.)
25 75 10 33 49 29 87 A loop must be used to slide each element on the right one slot to the left, one at a time…

47 Linked List Advantages (cont.)
25 75 10 33 49 29 87

48 Linked List Advantages (cont.)
25 75 10 33 49 29 87

49 Linked List Advantages (cont.)
25 75 10 33 49 29 87

50 Linked List Advantages (cont.)
25 75 10 33 49 29 87

51 Linked List Advantages (cont.)
25 75 10 33 49 29 87 Only 100,000 more to go!

52 Linked List Advantages (cont.)
Linked lists also waste less memory for large elements (records of information). Wasted memory is memory space in the data structure not used for data. In arrays, the wasted memory is the part of the array not being utilized. In linked lists, the wasted memory is the pointer in each node.

53 Linked List Advantages (cont.)
start Linked List Array

54 Accessing info start To access the info in the first node:
(*start).info

55 Accessing info (cont.) start To access the info in the first node:
(*start).info Or (better yet) start->info dereference and member access in one shot

56 Accessing info (cont.) start To access the info in the second node:
start->next->info

57 Finding the Mercedes start
Mercedes Consider a linked list, where the info member (left half of blue box) is a CarType object We wish to find a Mercedes stored in one of the objects.

58 Finding the Mercedes (cont.)
start Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

59 Finding the Mercedes (cont.)
start Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

60 Finding the Mercedes (cont.)
start item maker: price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

61 Finding the Mercedes (cont.)
start item maker: price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

62 Finding the Mercedes (cont.)
start item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

63 Finding the Mercedes (cont.)
start item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

64 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

65 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

66 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

67 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

68 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes Loop repeats, ptr moves through list CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

69 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

70 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next; FOUND!

71 Finding the Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator != Mercedes Loop stops ptr points to the node containing Mercedes Can return ptr->info to the client, so that the client can access data members, such as price

72 What if no Mercedes? The code we looked at assumes there is a Mercedes in the linked list The following slides show the error that will occur if Mercedes is not in the linked list…

73 No Mercedes item start ptr maker: Mercedes price: year: operator != …
CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

74 No Mercedes (cont.) item start ptr maker: Mercedes price: year:
operator != CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

75 No Mercedes (cont.) item start ptr maker: Mercedes price: year:
operator != CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

76 No Mercedes (cont.) item start ptr maker: Mercedes price: year:
operator != CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

77 No Mercedes (cont.) item start ptr maker: Mercedes price: year:
operator != CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

78 No Mercedes (cont.) item start ptr is set to NULL maker: Mercedes
price: year: operator != CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

79 No Mercedes (cont.) item start ptr is set to NULL maker: Mercedes
price: year: operator != CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) // overloaded operator ptr = ptr->next;

80 No Mercedes (cont.) item start ptr is set to NULL maker: Mercedes
price: year: operator != CarType item; item.maker = “Mercedes”; Node<CarType> *ptr = start; while ( ptr->info != item ) ptr = ptr->next; Runtime error!

81 Finding a Possible Mercedes
start item maker: Mercedes price: year: operator == Let’s solve the problem, but let’s assume that item is passed in as a parameter (of type DataType). This is normally what would happen. Instead of the CarType struct having an overloaded != operator, it will have an overloaded == operator.

82 Finding a Possible Mercedes (cont.)
start item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; }

83 Finding a Possible Mercedes (cont.)
start item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; }

84 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; }

85 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; }

86 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } found: false

87 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } found: false

88 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } found: false

89 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

90 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

91 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

92 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

93 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false Notice that found is only set to true if Mercedes is found …

94 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false then, !found is false and the loop exits

95 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false If Mercedes is not found, ptr eventually gets set to NULL, as before.

96 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false After going through the loop several times…

97 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

98 Finding a Possible Mercedes (cont.)
start ptr item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

99 Finding a Possible Mercedes (cont.)
start ptr is set to NULL item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

100 Finding a Possible Mercedes (cont.)
start ptr is set to NULL item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

101 Finding a Possible Mercedes (cont.)
start ptr is set to NULL item maker: Mercedes price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false Exit from loop with no runtime error

102 Yet Another Case What if the linked list is empty to begin with?
Try to consider all possible cases When a linked list is empty, the start pointer should always be set to NULL The start pointer would be set to NULL inside the constructor, when an empty linked list is first made

103 Empty List Case item start is set to NULL maker: Mercedes price: year:
operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; }

104 Empty List Case (cont.) item start is set to NULL maker: Mercedes
price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } SAME CODE

105 Empty List Case (cont.) item start is set to NULL maker: Mercedes
price: year: operator == Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; }

106 Empty List Case (cont.) item start is set to NULL maker: Mercedes
price: year: operator == ptr is set to NULL Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; }

107 Empty List Case (cont.) item start is set to NULL maker: Mercedes
price: year: operator == ptr is set to NULL Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; }

108 Empty List Case (cont.) item start is set to NULL maker: Mercedes
price: year: operator == ptr is set to NULL Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

109 Empty List Case (cont.) item start is set to NULL maker: Mercedes
price: year: operator == ptr is set to NULL Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false

110 Empty List Case (cont.) item start is set to NULL maker: Mercedes
price: year: operator == ptr is set to NULL Node<DataType> *ptr = start; bool found = false; while (ptr != NULL && !found ) { if ( ptr->info == item ) found = true; if ( !found ) ptr = ptr->next; } found: false Exit loop without runtime error

111 Inserting a New Node Let’s assume that we want to insert a new node at the beginning of a linked list Assume that the client passes in a parameter called element (of type DataType) We would like to place the element into a node and insert the node at the beginning of the linked list

112 Inserting a Node element start

113 Inserting a Node (cont.)
element start All new nodes must be made in the heap, SO…

114 Inserting a Node (cont.)
element start Node<DataType> *ptr = new Node<DataType>;

115 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>;

116 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; Now we have to store element into the node

117 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

118 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

119 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

120 Inserting a Node (cont.)
element start Now we have to think about how to make the pointer called “next” point to the first node in the list, to link it in ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

121 Inserting a Node (cont.)
element start You can’t successfully write code like this without thinking about addresses. ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

122 Inserting a Node (cont.)
element start REMEMBER…when you want to change the way a pointer points, you HAVE to assign a different address to it ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

123 Inserting a Node (cont.)
element start Right now, the pointer called “next” doesn’t have a valid address assigned to it. ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

124 Inserting a Node (cont.)
element start To store the correct address in it, we have to find the address of the first node of the linked list. ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

125 Inserting a Node (cont.)
element start Where is the address of the first node stored? ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

126 Inserting a Node (cont.)
element start Now think, the address would be stored in something that points to it. So where is it stored? ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

127 Inserting a Node (cont.)
element start ptr That’s right, in the start pointer. Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

128 Inserting a Node (cont.)
element start So now, all we have to do is copy that address into the pointer called “next” ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

129 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element;

130 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start;

131 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start;

132 Inserting a Node (cont.)
element start Well, it’s been inserted. But start should point to the first node now. ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start;

133 Inserting a Node (cont.)
element start REMEMBER…when you want to change the way a pointer points, you have to assign a different address to it ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start;

134 Inserting a Node (cont.)
element start We’d like start to point to the new node, so what stores the address of the new node? ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start;

135 Inserting a Node (cont.)
element start That’s right, ptr. So now all we have to do is assign the address stored in ptr to the start pointer. ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start;

136 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start;

137 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start; start = ptr;

138 Inserting a Node (cont.)
element start ptr Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start; start = ptr;

139 Inserting a Node (cont.)
element start ptr Easy, right? Node<DataType> *ptr = new Node<DataType>; ptr->info = element; ptr->next = start; start = ptr;

140 REMEMBER… Use drawings when working with linked lists, until you become an expert When you want to change the way a pointer points, you have to assign a different address to it You can find the address you need by looking at other pointers (remember that they store addresses)

141 Inserting into the Middle of a Linked List
Suppose we know that there is a Mercedes in a linked list We would like to insert a node containing Honda right after it We first find the Mercedes, using code that we looked at before

142 Inserting a Node start element maker: Mercedes price: year:
operator != Mercedes Node<DataType> *ptr = start; while ( ptr->info != element ) // element is a parameter ptr = ptr->next;

143 Inserting a Node (cont.)
start element maker: Mercedes price: year: operator != Mercedes After this code executes, ptr points to the node that has Mercedes. Node<DataType> *ptr = start; while ( ptr->info != element ) // element is a parameter ptr = ptr->next;

144 Inserting a Node (cont.)
start element ptr maker: Mercedes price: year: operator != Mercedes After this code executes, ptr points to the node that has Mercedes. Node<DataType> *ptr = start; while ( ptr->info != element ) // element is a parameter ptr = ptr->next;

145 Inserting a Node (cont.)
start element ptr maker: Mercedes price: year: operator != Mercedes Now we would like to insert a CarType object called elementToInsert (containing Honda), which would also be passed in as a parameter, right after the Mercedes

146 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert

147 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert Well, all new nodes are created in the heap, SO…..

148 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert Node<DataType> *newNode = new Node<DataType>;

149 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert newNode Node<DataType> *newNode = new Node<DataType>;

150 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert newNode Node<DataType> *newNode = new Node<DataType>; Now, how about placing elementToInsert into the new node?

151 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

152 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

153 Inserting a Node (cont.)
start ptr maker: Honda price: 5000 year: 1985 operator != Mercedes elementToInsert newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

154 Inserting a Node (cont.)
start ptr Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

155 Inserting a Node (cont.)
start ptr Now, what we want is shown by the dashed arrows; this would cause the insertion of the node Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

156 Inserting a Node (cont.)
start ptr We have two pointers we need to change – but we have to be careful about the way we change them Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

157 Inserting a Node (cont.)
start ptr If we change the left pointer first, we will no longer be able to access the last node (memory leak) Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

158 Inserting a Node (cont.)
start ptr So, we first have to assign the address of the last node into the “next” pointer of the new node Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

159 Inserting a Node (cont.)
start ptr Where is the address of the last node stored? Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

160 Inserting a Node (cont.)
start ptr That’s right, it is stored in ptr->next Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert;

161 Inserting a Node (cont.)
start ptr Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert; newNode->next = ptr->next;

162 Inserting a Node (cont.)
start ptr Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert; newNode->next = ptr->next;

163 Inserting a Node (cont.)
start ptr Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert; newNode->next = ptr->next; ptr->next = newNode;

164 Inserting a Node (cont.)
start ptr Mercedes Mercedes newNode Node<DataType> *newNode = new Node<DataType>; newNode->info = elementToInsert; newNode->next = ptr->next; ptr->next = newNode;

165 Removing a Node Suppose we definitely know there is a Mercedes in the linked list and we wish to remove the node that contains it We need to find the node first

166 Removing a Node (cont.) start Mercedes

167 Removing a Node (cont.) ptr start Mercedes

168 Removing a Node (cont.) ptr start …
Mercedes When it is found, we need to join the node in front of it to the node after it.

169 Removing a Node (cont.) ptr start …
Mercedes When it is found, we need to join the node in front of it to the node after it.

170 Removing a Node (cont.) ptr start …
Mercedes But how can we access the “next” pointer of the node in front, so we can change the address it stores? (we can’t)

171 Removing a Node (cont.) ptr start …
Mercedes The way to solve the problem is by having ptr stop on the node BEFORE the node that has Mercedes

172 Removing a Node (cont.) ptr start Mercedes

173 Removing a Node (cont.) ptr start Mercedes

174 Removing a Node (cont.) ptr start …
Mercedes If ptr stops here, then we CAN change the address stored in the “next” pointer. So we have to set up a loop appropriately.

175 Removing a Node (cont.) start Mercedes

176 Removing a Node (cont.) start … element contains Mercedes
Node<DataType> *ptr = start; while ( ptr->next->info != element ) // overloaded != operator ptr = ptr->next;

177 Removing a Node (cont.) start …
Mercedes passed in from the client as a parameter Node<DataType> *ptr = start; while ( ptr->next->info != element ) // overloaded != operator ptr = ptr->next;

178 Removing a Node (cont.) start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; while ( ptr->next->info != element ) // overloaded != operator ptr = ptr->next;

179 Removing a Node (cont.) start …
Mercedes BUT…what if Mercedes is in the first node? Node<DataType> *ptr = start; while ( ptr->next->info != element ) // overloaded != operator ptr = ptr->next;

180 Removing a Node (cont.) start …
Mercedes BUT…what if Mercedes is in the first node? Node<DataType> *ptr = start; while ( ptr->next->info != element ) // overloaded != operator ptr = ptr->next;

181 Removing a Node (cont.) start …
Mercedes We need to handle the first node differently. Node<DataType> *ptr = start; while ( ptr->next->info != element ) // overloaded != operator ptr = ptr->next;

182 Removing a Node (cont.) start Mercedes

183 Removing a Node (cont.) start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

184 Removing a Node (cont.) start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

185 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

186 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

187 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

188 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

189 Removing a Node (cont.) ptr start …
Mercedes Well, start points to the beginning of the new linked list, but a node isn’t removed unless we free it. Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

190 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) start = start->next;

191 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) { start = start->next; delete ptr; }

192 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
Mercedes Node<DataType> *ptr = start; if ( ptr->info == element ) { start = start->next; delete ptr; }

193 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
if ( ptr->info == element ) { start = start->next; delete ptr; }

194 Removing a Node (cont.) ptr start … Node<DataType> *ptr = start;
if ( ptr->info == element ) { start = start->next; delete ptr; } Now, let’s consider the other case.

195 Removing a Node (cont.) start Mercedes

196 Removing a Node (cont.) start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next;

197 Removing a Node (cont.) start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; This is the loop that we thought of before.

198 Removing a Node (cont.) start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; It will find the node BEFORE the node that has Mercedes.

199 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; It will find the node BEFORE the node that has Mercedes.

200 Removing a Node (cont.) ptr start …
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Now we need to join the node before Mercedes to the node after Mercedes

201 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next;

202 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; ptr->next = ptr->next->next;

203 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; ptr->next = ptr->next->next;

204 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; ptr->next = ptr->next->next;

205 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; ptr->next = ptr->next->next;

206 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; ptr->next = ptr->next->next; But how will we free the node that has Mercedes?

207 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; ptr->next = ptr->next->next; This is memory leak. We better back up.

208 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next;

209 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next;

210 Removing a Node (cont.) ptr start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next;

211 Removing a Node (cont.) ptr ptr2 start … else {
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next;

212 Removing a Node (cont.) ptr ptr2 start … Mercedes else {
while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next;

213 Removing a Node (cont.) ptr ptr2 start … Mercedes else {
while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next;

214 Removing a Node (cont.) ptr ptr2 start … Mercedes else {
while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2;

215 Removing a Node (cont.) ptr ptr2 start … else {
while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2;

216 Removing a Node (cont.) ptr ptr2 start … else {
while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

217 Removing a Node (cont.) ptr ptr2 start … We did it! else {
while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; } We did it!

218 What If? What if Mercedes is the last node in the linked list
Would our code still work? Try to consider every possible situation in code design

219 Removing the Last Node ptr start …
Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; } After looping, ptr stops on the next-to-the-last node

220 Removing the Last Node (cont.)
ptr start Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

221 Removing the Last Node (cont.)
ptr ptr2 start Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

222 Removing the Last Node (cont.)
ptr ptr2 start Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

223 Removing the Last Node (cont.)
ptr ptr2 start Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

224 Removing the Last Node (cont.)
ptr ptr2 start Mercedes else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

225 Removing the Last Node (cont.)
ptr ptr2 start else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

226 Removing the Last Node (cont.)
ptr ptr2 start else { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; } The code works in removing the last node.

227 Working With Linked Lists
As you can see, sometimes you have to do a lot of thinking and problem-solving when working with linked lists It is not always obvious how to write code You can’t memorize the code, because it will not quite fit situations that you will encounter It is a matter of using logic (and knowing a few tricks of the trade)

228 Code for Removing a Node
Here is the resulting code for removing a node. It is assumes the node we are looking for is in the linked list. 1 Node<DataType> *ptr = start; 2 if ( ptr->info == element ) { 3 start = start->next; 4 delete ptr; 5 } 6 else { 7 while ( ptr->next->info != element ) 8 ptr = ptr->next; 9 Node<DataType> *ptr2 = ptr->next; 10 ptr->next = ptr2->next; 11 delete ptr2; 12 }

229 Code for Removing a Node (cont.)
1 Node<DataType> *ptr = start; 2 if ( ptr->info == element ) { 3 start = start->next; 4 delete ptr; 5 } 6 else { 7 while ( ptr->next->info != element ) 8 ptr = ptr->next; 9 Node<DataType> *ptr2 = ptr->next; 10 ptr->next = ptr2->next; 11 delete ptr2; 12 } This code can be simplified by using a header node…

230 Code for Removing a Node (cont.)
1 Node<DataType> *ptr = start; 2 if ( ptr->info == element ) { 3 start = start->next; 4 delete ptr; 5 } 6 else { 7 while ( ptr->next->info != element ) 8 ptr = ptr->next; 9 Node<DataType> *ptr2 = ptr->next; 10 ptr->next = ptr2->next; 11 delete ptr2; 12 } A header node is like any other node, except it doesn’t contain data.

231 Code for Removing a Node (cont.)
1 Node<DataType> *ptr = start; 2 if ( ptr->info == element ) { 3 start = start->next; 4 delete ptr; 5 } 6 else { 7 while ( ptr->next->info != element ) 8 ptr = ptr->next; 9 Node<DataType> *ptr2 = ptr->next; 10 ptr->next = ptr2->next; 11 delete ptr2; 12 } It is formed with the Node struct, like any other node, but the info data member isn’t set to anything.

232 Code for Removing a Node (cont.)
1 Node<DataType> *ptr = start; 2 if ( ptr->info == element ) { 3 start = start->next; 4 delete ptr; 5 } 6 else { 7 while ( ptr->next->info != element ) 8 ptr = ptr->next; 9 Node<DataType> *ptr2 = ptr->next; 10 ptr->next = ptr2->next; 11 delete ptr2; 12 } The header node would be the first node in the linked list.

233 Code for Removing a Node (cont.)
1 Node<DataType> *ptr = start; 2 if ( ptr->info == element ) { 3 start = start->next; 4 delete ptr; 5 } 6 else { 7 while ( ptr->next->info != element ) 8 ptr = ptr->next; 9 Node<DataType> *ptr2 = ptr->next; 10 ptr->next = ptr2->next; 11 delete ptr2; 12 } The header node eliminates this special case…

234 Code for Removing a Node (cont.)
1 Node<DataType> *ptr = start; 2 if ( ptr->info == element ) { 3 start = start->next; 4 delete ptr; 5 } 6 else { 7 while ( ptr->next->info != element ) 8 ptr = ptr->next; 9 Node<DataType> *ptr2 = ptr->next; 10 ptr->next = ptr2->next; 11 delete ptr2; 12 } Since we know the first node doesn’t contain data, it can’t contain Mercedes.

235 Code with Header Node 1 Node<DataType> *ptr = start;
2 while ( ptr->next->info != element ) 3 ptr = ptr->next; 4 Node<DataType> *ptr2 = ptr->next; 5 ptr->next = ptr2->next; 6 delete ptr2;

236 Removing a Node But this code assumes that there definitely is a Mercedes in the linked list Let’s refine it a little Suppose we are not really sure if there is a Mercedes in the linked list to delete Or suppose the linked list is empty to begin with Always try to handle every case

237 Refining Code with Header Node
Node<DataType> *ptr = start; while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2;

238 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; We’ll use our found variable, like before.

239 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; We’ll use our found variable, like before.

240 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; An empty list just has a header node.

241 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; This will give us a runtime error if we have an empty list. Let’s test for it in a loop heading.

242 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; We’ll make a new loop and shove this part inside.

243 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL ) { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

244 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL ) { while ( ptr->next->info != element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; } Now we want the removal code if ptr->next->info IS equal to element

245 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL ) { if ( ptr->next->info == element ) ptr = ptr->next; Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; } Now we want the removal code if ptr->next->info IS equal to element

246 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; }

247 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; } If we remove a node, we need to break out of the loop.

248 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL && !found ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; found = true; } If we remove a node, we need to break out of the loop.

249 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL && !found ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; found = true; } Now, we just need to advance ptr if we didn’t remove a node.

250 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL && !found ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; found = true; } if ( !found ) ptr = ptr->next; Now, we just need to advance ptr if we didn’t remove a node.

251 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL && !found ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; found = true; } if ( !found ) ptr = ptr->next; If ptr is pointing to the last node when this condition is checked, Mercedes can’t be in the last node…

252 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL && !found ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; found = true; } if ( !found ) ptr = ptr->next; It would have been found on the previous iteration. Therefore, we break out of the loop at the right time.

253 Refining Code with Header Node (cont.)
Node<DataType> *ptr = start; bool found = false; while ( ptr->next != NULL && !found ) { if ( ptr->next->info == element ) { Node<DataType> *ptr2 = ptr->next; ptr->next = ptr2->next; delete ptr2; found = true; } if ( !found ) ptr = ptr->next; Final result.

254 Linked List vs. Arrays When computer scientists decide on whether or not to use an array or linked list for data, the decision is usually based on two factors: Speed Conservation of Memory (RAM)

255 Speed In some situations, an array can be faster than a linked list, such as when a calculated index is used to access an element In other situations, a linked list can be faster than an array, such as when removing an element from the middle (as we saw before) we usually need to search for the element to remove, but we search for it in both the array and linked list

256 Memory Conservation start Linked List with Element Size of 32 Bytes

257 Memory Conservation (cont.)
When evaluating memory conservation between linked lists and arrays, we make use of dynamic space and static space within elements, which are usually records of information dynamic space – uses heap memory (example: the dynamic character array for a string) static space – space not in heap memory We also assume the array expansion/contraction scheme described earlier

258 Memory Conservation (cont.)
We may have three types of elements used in arrays: never-used elements (won’t have dynamic space) previously-used elements (may have dynamic space, but these elements are made inaccessible) used elements (may have dynamic space)

259 Memory Conservation (cont.)
Let h be the amount of housekeeping space within a linked list node (example: the “next” pointer) Let s be the amount of static space Let e be the total amount of space in an element (both static and dynamic space) Then, we have three cases to consider…

260 Memory Conservation (cont.)
Case I. If there is only static space within elements (no dynamic space), then: (1) if the element size is less than h, the array is expected to waste less memory than the linked list (2) if the element size is more than 3h, the linked list is expected to waste less memory than the array (3) if the element size is between h and 3h, there may not be a significant difference in wasting memory between the array and linked list

261 Memory Conservation (cont.)
Case II. If we would only add elements to the data structure and never remove them (so that we never have previously-used elements), and dynamic space may be used in elements, then: (1) if h < s/3, the linked list is expected to waste less memory than the array (2) if h > s/3, the array is expected to waste less memory than the linked list (3) if h = s/3, then there may not be a significant difference in wasting memory between the array and the linked list.

262 Memory Conservation (cont.)
Case III. If there are all three types of elements during the use of the array, and dynamic space is used in elements, then: (1) if h < s/3, the linked list is expected to waste less memory than the array (2) if h > e, the array is expected to waste less memory than the linked list (3) if s/3  h  e, then there may not be a significant difference in wasting memory between the array and the linked list

263 Additional Notes About These Cases
Case II is the most precise, and so it should be used when applicable Case III is really a generalization of Case I case I is case III with s = e (no dynamic space) case I is provided to avoid confusion Analysis about these cases is provided in the text


Download ppt "C++ Classes and Data Structures Jeffrey S. Childs"

Similar presentations


Ads by Google