lecture06 methods for-making_data_structures_v2
DESCRIPTION
TRANSCRIPT
![Page 1: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/1.jpg)
1
Chapter 6Methods for Making
Data Structures
![Page 2: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/2.jpg)
2
Dynamic Arrays in Data Structures
• 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.
![Page 3: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/3.jpg)
3
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…
![Page 4: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/4.jpg)
4
changeSize Function
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445
33
New element needs to be put into array, so changeSize function is called
![Page 5: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/5.jpg)
5
changeSize Function(cont.)
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445
… 0 1 2 3 432 433 444 445 446
new array is made
![Page 6: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/6.jpg)
6
changeSize Function(cont.)
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445
elements are copied over one by one using a for loop
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446
![Page 7: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/7.jpg)
7
changeSize Function(cont.)
Then, the new element can be put in
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446
33
33
This process would take place every time a new element needs to be inserted.
![Page 8: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/8.jpg)
8
changeSize Function(cont.)
Suppose the element at the end of the array needs to be removed.
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446
33
Likewise, when an element needs to be removed, this method contracts the array by one to conserve memory.
![Page 9: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/9.jpg)
9
changeSize Function(cont.)
The changeSize function is called and a new, smaller array is made.
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446
33
… 0 1 2 3 432 433 444 445
![Page 10: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/10.jpg)
10
changeSize Function(cont.)
The elements are copied over one by one, using a for loop.
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446
33
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445
![Page 11: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/11.jpg)
11
changeSize Function(cont.)
This method of array expansion/contraction is largely inefficient, because there is too much element copying.
25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445
![Page 12: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/12.jpg)
12
Linked Structures
• 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.
![Page 13: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/13.jpg)
13
A Node Struct Template
template <typename T>struct Node {
T info;Node<T> *next;
};
The info member is for the data. It can anything (T), but it is often the object of another struct, used as a record of information.
The next pointer stores the address of a Node of the same type! This means that each node can point to another node.
![Page 14: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/14.jpg)
14
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 insert function, and the function places it in a node.
![Page 15: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/15.jpg)
15
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.
![Page 16: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/16.jpg)
16
Example of a Linked Structure
start
Each blue node is divided into two sections, for the two members of the Node struct.
![Page 17: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/17.jpg)
17
Example of a Linked Structure (cont.)
start
The right section is the pointer called “next”.
The left section is the info member.
![Page 18: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/18.jpg)
18
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).
The start pointer would be saved in the private section of a data structure class.
![Page 19: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/19.jpg)
19
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.
![Page 20: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/20.jpg)
20
Linked ListAdvantages
… …5 3 7 2 1
Removing an element from the middle of a linked list is fast.
![Page 21: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/21.jpg)
21
Linked ListAdvantages (cont.)
… …5 3 2 1
Removing an element from the middle of a linked list is fast.
![Page 22: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/22.jpg)
22
Removal Problem in Array
… …
Removing elements from the middle of an array (without leaving gaps) is more problematic.
25 75 10 12
211 212 213 214 215 216 217 218
33 49 29 87
![Page 23: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/23.jpg)
23
Removal Problem in Array (cont.)
… …
A loop must be used to slide each element on the right one slot to the left, one at a time…
25 75 10
211 212 213 214 215 216 217 218
33 49 29 87
![Page 24: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/24.jpg)
24
Removal Problem in Array (cont.)
… …25 75 10
211 212 213 214 215 216 217 218
49 29 8733
… …25 75 10
211 212 213 214 215 216 217 218
49 29 8733
… …25 75 10
211 212 213 214 215 216 217 218
49 29 8733
![Page 25: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/25.jpg)
25
Removal Problem in Array (cont.)
… …25 75 10
211 212 213 214 215 216 217 218
49 29 8733
Only 100,000 more to go!
![Page 26: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/26.jpg)
26
Linked ListAdvantages (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.
![Page 27: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/27.jpg)
27
Linked ListAdvantages (cont.)
start
Linked List
Array
![Page 28: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/28.jpg)
28
Accessing info
To access the info in the first node:
(*start).info
Or (better yet)
start->info
start
dereference and member access in one shot
![Page 29: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/29.jpg)
29
Accessing info(cont.)
To access the info in the second node:
start->next->info
start
![Page 30: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/30.jpg)
30
Finding a Possible Mercedes
Let’s solve the problem, but let’s assume that item is passed in as a parameter (of type T). This is normally what would happen.Instead of the CarType struct having an overloaded != operator, it will have an overloaded == operator.
itemmaker: Mercedes price: year:operator ==
start
…
Me
rce
de
s
![Page 31: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/31.jpg)
31
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 32: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/32.jpg)
32
CarType item;item.maker = "Mercedes";
Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 33: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/33.jpg)
33
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;
bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 34: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/34.jpg)
34
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;
while (ptr != NULL && !found ) {if ( ptr->info == item ) // overloaded ==
found = true;if ( !found )
ptr = ptr->next;}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 35: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/35.jpg)
35
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 36: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/36.jpg)
36
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 37: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/37.jpg)
37
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found )
ptr = ptr->next;}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 38: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/38.jpg)
38
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;
while (ptr != NULL && !found ) { if ( ptr->info == item )
found = true;if ( !found )
ptr = ptr->next;}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 39: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/39.jpg)
39
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
![Page 40: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/40.jpg)
40
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
found: false
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
ptr
![Page 41: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/41.jpg)
41
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;
while (ptr != NULL && !found ) { if ( ptr->info == item )
found = true;if ( !found )
ptr = ptr->next;}
itemmaker: Mercedes price: year:operator ==
found: false
After going through the loop several times…
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
ptr
![Page 42: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/42.jpg)
42
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
found: false
Notice that found is only set to true if ptr is not NULL and Mercedes is found …
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
ptr
![Page 43: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/43.jpg)
43
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;
while (ptr != NULL && !found ) { if ( ptr->info == item )
found = true;if ( !found )
ptr = ptr->next;}
itemmaker: Mercedes price: year:operator ==
found: false
then, !found is false and the loop exits
Finding a Possible Mercedes (cont.)
start
…
Me
rce
de
s
ptr
![Page 44: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/44.jpg)
44
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
found: false
If Mercedes is not found, ptr eventually gets set to NULL.
What If Mercedes Does Not Exist?
start
…
ptr
![Page 45: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/45.jpg)
45
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found )
ptr = ptr->next;}
itemmaker: Mercedes price: year:operator ==
ptr is set to NULL
found: false
What If Mercedes Does not Exist? (cont.)
start
…
If Mercedes is not found, ptr eventually gets set to NULL.
![Page 46: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/46.jpg)
46
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;
while (ptr != NULL && !found ) { if ( ptr->info == item )
found = true;if ( !found )
ptr = ptr->next;}
itemmaker: Mercedes price: year:operator ==
ptr is set to NULL
found: false
What If Mercedes Does not Exist? (cont.)
start
…
Exit from loop because ptr is NULL.
![Page 47: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/47.jpg)
47
What If Finding in an Empty Linked List?
• 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.
![Page 48: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/48.jpg)
48
start is set to NULL
Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
SAME CODE
Finding in an Empty List
![Page 49: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/49.jpg)
49
start is set to NULL
Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to NULL
Finding in an Empty List (cont.)
![Page 50: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/50.jpg)
50
start is set to NULL
Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to NULL
found: false
Finding in an Empty List (cont.)
![Page 51: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/51.jpg)
51
start is set to NULL
Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {
if ( ptr->info == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to NULL
found: false
Finding in an Empty List (cont.)
Exit loop because ptr is NULL.
![Page 52: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/52.jpg)
52
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 T).
• We would like to place the element into a node and insert the node at the beginning of the linked list.
![Page 53: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/53.jpg)
53
Inserting a Node at Front
element
start
All new nodes must be made in the heap, SO…
![Page 54: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/54.jpg)
54
element
start
Node<T> *ptr = new Node<T>;
ptr
Inserting a Node at Front (cont.)
![Page 55: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/55.jpg)
55
element
start
Node<T> *ptr = new Node<T>;
ptr
Now we have to store element into the node
Inserting a Node at Front (cont.)
![Page 56: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/56.jpg)
56
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptr
Inserting a Node at Front (cont.)
![Page 57: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/57.jpg)
57
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptrNow we have to think about how to make the pointer called “next” point to the first node in the list, to link it in
Inserting a Node at Front (cont.)
![Page 58: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/58.jpg)
58
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptrYou can’t successfully write code like this without thinking about addresses.
Inserting a Node at Front (cont.)
![Page 59: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/59.jpg)
59
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptrREMEMBER…when you want to change the way a pointer points, you HAVE to assign a different address to it
Inserting a Node at Front (cont.)
![Page 60: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/60.jpg)
60
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptrRight now, the pointer called “next” doesn’t have a valid address assigned to it.
Inserting a Node at Front (cont.)
![Page 61: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/61.jpg)
61
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptrTo store the correct address in it, we have to find the address of the first node of the linked list.
Inserting a Node at Front (cont.)
![Page 62: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/62.jpg)
62
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptr Where is the address of the first node stored?
Inserting a Node at Front (cont.)
![Page 63: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/63.jpg)
63
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptrNow think, the address would be stored in something that points to it. So where is it stored?
Inserting a Node at Front (cont.)
![Page 64: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/64.jpg)
64
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptr That’s right, in the start pointer.
Inserting a Node at Front (cont.)
![Page 65: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/65.jpg)
65
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptr So now, all we have to do is copy that address into the pointer called “next”
Inserting a Node at Front (cont.)
![Page 66: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/66.jpg)
66
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;
ptr
Inserting a Node at Front (cont.)
![Page 67: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/67.jpg)
67
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;
ptr
Inserting a Node at Front (cont.)
Well, it’s been inserted. But start should point to the first node now.
![Page 68: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/68.jpg)
68
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;
ptr
Inserting a Node at Front (cont.)
REMEMBER…when you want to change the way a pointer points, you have to assign a different address to it
![Page 69: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/69.jpg)
69
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;
ptr
Inserting a Node at Front (cont.)
We’d like start to point to the new node, so what stores the address of the new node?
![Page 70: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/70.jpg)
70
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;
ptr
Inserting a Node at Front (cont.)
That’s right, ptr. So now all we have to do is assign the address stored in ptr to the start pointer.
![Page 71: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/71.jpg)
71
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;start = ptr;
ptr
Inserting a Node at Front (cont.)
![Page 72: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/72.jpg)
72
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;start = ptr;
ptr
Inserting a Node at Front (cont.)
Easy, right?
![Page 73: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/73.jpg)
73
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).
![Page 74: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/74.jpg)
74
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.
![Page 75: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/75.jpg)
75
Inserting a Node at Middleelement
maker: Mercedes price: year:operator !=
Node<T> *ptr = start;while ( ptr->info != element ) // element is a parameter
ptr = ptr->next;
start
After this code executes, ptr points to the node that has Mercedes.
ptr
…
Mer
ced
es
![Page 76: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/76.jpg)
76
elementmaker: Mercedes price: year:operator !=
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
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 77: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/77.jpg)
77
maker: Honda price: 5000year: 1985operator !=
Well, all new nodes are created in the heap, SO…..
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
ced
es
![Page 78: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/78.jpg)
78
maker: Honda price: 5000year: 1985operator !=
Node<T> *newNode = new Node<T>;
newNode
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
ced
es
![Page 79: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/79.jpg)
79
maker: Honda price: 5000year: 1985operator !=
Node<T> *newNode = new Node<T>;
newNode
Now, how about placing elementToInsert into the new node?
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
ced
es
![Page 80: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/80.jpg)
80
maker: Honda price: 5000year: 1985operator !=
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
ced
es
![Page 81: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/81.jpg)
81
maker: Honda price: 5000year: 1985operator !=
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
ced
es
![Page 82: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/82.jpg)
82
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
Now, what we want is shown by the dashed arrows; this would cause the insertion of the node
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 83: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/83.jpg)
83
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
We have two pointers we need to change – but we have to be careful about the way we change them
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 84: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/84.jpg)
84
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
If we change the left pointer first, we will no longer be able to access the last node (memory leak)
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 85: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/85.jpg)
85
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
So, we first have to assign the address of the last node into the “next” pointer of the new node
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 86: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/86.jpg)
86
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
Where is the address of the last node stored?
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 87: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/87.jpg)
87
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;
newNode
That’s right, it is stored in ptr->next
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 88: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/88.jpg)
88
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;newNode->next = ptr->next;
newNode
Inserting a Node at Middle (cont.)start ptr
…
Mer
ced
es
![Page 89: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/89.jpg)
89
Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;newNode->next = ptr->next;ptr->next = newNode;
newNode
Inserting a Node at Middle (cont.)start
Mer
cede
s
ptr
…
Mer
ced
es
![Page 90: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/90.jpg)
90
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.
![Page 91: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/91.jpg)
91
Removing the First Node
start
…
Me
rce
des
Node<T> *ptr = start;if ( ptr->info == element )
start = start->next;
Mercedes is in the first node
![Page 92: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/92.jpg)
92
Removing the First Node(cont.)
start
Node<T> *ptr = start;if ( ptr->info == element )
start = start->next;
ptr
…
Me
rce
des
![Page 93: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/93.jpg)
93
Removing the First Node(cont.)
start
Node<T> *ptr = start;if ( ptr->info == element )
start = start->next;
ptr
…
Me
rce
des
![Page 94: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/94.jpg)
94
Removing the First Node(cont.)
…
Me
rce
des
Node<T> *ptr = start;if ( ptr->info == element )
start = start->next;
ptr start
![Page 95: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/95.jpg)
95
Removing the First Node(cont.)
…
Node<T> *ptr = start;if ( ptr->info == element ) {
start = start->next;delete ptr;
}
startptr
Well, start points to the beginning of the new linked list, but a node isn’t removed unless we free it.
![Page 96: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/96.jpg)
96
Removing the First Node(cont.)
…
Node<T> *ptr = start;if ( ptr->info == element ) {
start = start->next;delete ptr;
}
startptr
Now, let’s consider the other case whereby Mercedes is in the middle of the list.
![Page 97: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/97.jpg)
97
Removing a Middle Node
start
else {while ( ptr->next->info != element )
ptr = ptr->next;
The while loop points ptr to the node BEFORE the node that has Mercedes.
ptr
…
Me
rce
des
![Page 98: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/98.jpg)
98
Removing a Middle Node (cont.)
start
else {while ( ptr->next->info != element )
ptr = ptr->next;
ptr
…
Me
rce
des
We need to join the node before Mercedes to the node after Mercedes
![Page 99: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/99.jpg)
99
Removing a Middle Node (cont.)
start
else {while ( ptr->next->info != element )
ptr = ptr->next;
Node<T> *ptr2 = ptr->next;
ptr ptr2
…
Me
rce
des
But we mus keep a pointer to the node that has Mercedes
![Page 100: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/100.jpg)
100
Removing a Middle Node (cont.)
start
else {while ( ptr->next->info != element )
ptr = ptr->next;
Node<T> *ptr2 = ptr->next;
ptr->next = ptr2->next;
ptr ptr2
…
Me
rce
des
We now join the node before Mercedes to the node after Mercedes
![Page 101: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/101.jpg)
101
Removing a Middle Node (cont.)
start
else {while ( ptr->next->info != element )
ptr = ptr->next;
Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;
delete ptr2;
ptr ptr2
…
We then delete the node that has Mercedes
We did it!
![Page 102: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/102.jpg)
102
What If Mercedes is the Last Node?
• Would our code still work?• Try to consider every possible situation in
code design.
![Page 103: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/103.jpg)
103
Removing the Last Node
else {
while ( ptr->next->info != element ) ptr = ptr->next;
Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;delete ptr2;}
start
…
Me
rce
des
ptr
After looping, ptr stops on the next-to-the-last node
![Page 104: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/104.jpg)
104
else {while ( ptr->next->info != element )
ptr = ptr->next;
Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;delete ptr2;}
start ptr ptr2
Removing the Last Node (cont.)
…
Me
rce
des
![Page 105: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/105.jpg)
105
else {while ( ptr->next->info != element )
ptr = ptr->next;
Node<T> *ptr2 = ptr->next;
ptr->next = ptr2->next;delete ptr2;}
start ptr ptr2
Removing the Last Node (cont.)
…
Me
rce
des
![Page 106: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/106.jpg)
106
else {while ( ptr->next->info != element )
ptr = ptr->next;
Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;
delete ptr2;}
start ptr ptr2
Removing the Last Node (cont.)
…
The code works in removing the last node.
![Page 107: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/107.jpg)
107
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).
![Page 108: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/108.jpg)
108
Code for Removing a Node
1 Node<T> *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<T> *ptr2 = ptr->next;10 ptr->next = ptr2->next;11 delete ptr2;12 }
Here is the resulting code for removing a node. It is assumes the node we are looking for is in the linked list.
![Page 109: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/109.jpg)
109
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.
![Page 110: Lecture06 methods for-making_data_structures_v2](https://reader034.vdocuments.net/reader034/viewer/2022051610/5481c7bdb4af9f99108b474d/html5/thumbnails/110.jpg)
Reference
• Childs, J. S. (2008). Methods for Making Data Structures. C++ Classes and Data Structures. Prentice Hall.
110