lecture06 methods for-making_data_structures_v2
DESCRIPTION
TRANSCRIPT
1
Chapter 6Methods for Making
Data Structures
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.
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…
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
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
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
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.
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.
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
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
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
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.
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.
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.
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.
16
Example of a Linked Structure
start
Each blue node is divided into two sections, for the two members of the Node struct.
17
Example of a Linked Structure (cont.)
start
The right section is the pointer called “next”.
The left section is the info member.
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.
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.
20
Linked ListAdvantages
… …5 3 7 2 1
Removing an element from the middle of a linked list is fast.
21
Linked ListAdvantages (cont.)
… …5 3 2 1
Removing an element from the middle of a linked list is fast.
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
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
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
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!
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.
27
Linked ListAdvantages (cont.)
start
Linked List
Array
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
29
Accessing info(cont.)
To access the info in the second node:
start->next->info
start
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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.
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.
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
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.)
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.)
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.
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.
53
Inserting a Node at Front
element
start
All new nodes must be made in the heap, SO…
54
element
start
Node<T> *ptr = new Node<T>;
ptr
Inserting a Node at Front (cont.)
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.)
56
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;
ptr
Inserting a Node at Front (cont.)
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.)
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.)
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.)
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.)
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.)
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.)
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.)
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.)
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.)
66
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;
ptr
Inserting a Node at Front (cont.)
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.
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
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?
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.
71
element
start
Node<T> *ptr = new Node<T>;ptr->info = element;ptr->next = start;start = ptr;
ptr
Inserting a Node at Front (cont.)
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?
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).
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.
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
92
Removing the First Node(cont.)
start
Node<T> *ptr = start;if ( ptr->info == element )
start = start->next;
ptr
…
Me
rce
des
93
Removing the First Node(cont.)
start
Node<T> *ptr = start;if ( ptr->info == element )
start = start->next;
ptr
…
Me
rce
des
94
Removing the First Node(cont.)
…
Me
rce
des
Node<T> *ptr = start;if ( ptr->info == element )
start = start->next;
ptr start
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.
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.
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
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
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
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
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!
102
What If Mercedes is the Last Node?
• Would our code still work?• Try to consider every possible situation in
code design.
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
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
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
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.
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).
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.
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.
Reference
• Childs, J. S. (2008). Methods for Making Data Structures. C++ Classes and Data Structures. Prentice Hall.
110