1 chapter 6 methods for making data structures. 2 dynamic arrays in data structures in almost every...
DESCRIPTION
3 Dynamic Arrays in Data Structures If we allocate a size or expand to a size that is too big, memory wastage will occur when the actual usage is less than the allocation, or when many elements have been removed.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).
• The correct size of a dynamic array may not be determined at the beginning.
• If we allocate a size that is too small, we need to expand the size when the array becomes full.
3
Dynamic Arrays in Data Structures
• If we allocate a size or expand to a size that is too big, memory wastage will occur when the actual usage is less than the allocation, or when many elements have been removed.
4
Array Expansion/Contraction
• One possible method to avoid memory wastage:– 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
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
6
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
7
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
8
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.
9
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.
10
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
11
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
12
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
13
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 / class.
• An object of the Node struct contains an element of data.
14
A Node Struct Template
template <typename T>struct Node {
T item;Node<T> *next;
};
The item member is for the data. It can anything (T), but it is often the object of a class, 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.
nextitem
15
A Node Struct Template
template <typename T>struct Node {
T item;Node<T> *next;
};
The item member is for the data. It can anything (T), but it is often the object of a class, 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.
Note that the Node can be also implemented as a
class.
16
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.
17
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/class template exists for use by the data structure.
18
Example of a Linked Structure (cont.)
nextitem
19
Example of a Linked Structure
start
nextitem
20
Example of a Linked Structure (cont.)
start
The last node doesn’t point to another node, so its pointer (called next) is set to nullptr (indicated by slash).
The start pointer would be saved in the private section of a data structure class.
21
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.
22
Linked ListAdvantages
… …5 3 7 2 1
Removing an element from the middle of a linked list is fast.
23
Linked ListAdvantages (cont.)
… …5 3 2 1
Removing an element from the middle of a linked list is fast.
24
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
25
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
26
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
27
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!
28
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.
29
Linked ListAdvantages (cont.)
start
Linked List
Array
30
Accessing item
To access the item in the first node:
start->item
start
dereference and member access in one shot
31
Accessing item(cont.)
To access the item in the second node:
start->next->item
start
32
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 class having an overloaded != operator, it will have an overloaded == operator.
itemmaker: Mercedes price: year:operator ==
start
…
Mer
cede
s
33
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
34
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
35
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
36
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
37
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
38
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
39
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
40
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
41
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) // overloaded ==found = true;
if ( !found )ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
42
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
found: false
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
ptr
43
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == 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
…
Mer
cede
s
ptr
44
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == 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 nullptr and Mercedes is found …
Finding a Possible Mercedes (cont.)
start
…
Mer
cede
s
ptr
45
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == 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
…
Mer
cede
s
ptr
46
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == 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 nullptr.
What If Mercedes Does Not Exist?
start
…
ptr
47
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to nullptr
found: false
What If Mercedes Does not Exist? (cont.)
start
…
If Mercedes is not found, ptr eventually gets set to nullptr.
48
CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to nullptr
found: false
What If Mercedes Does not Exist? (cont.)
start
…
Exit from loop because ptr is nullptr.
49
What If Finding in an Empty Linked List?
• When a linked list is empty, the start pointer should always be set to nullptr.
• The start pointer would be set to nullptr inside the constructor, when an empty linked list is first made.
50
start is set to nullptr
Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
SAME CODE
Finding in an Empty List
51
start is set to nullptr
Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to nullptr
Finding in an Empty List (cont.)
52
start is set to nullptr
Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to nullptr
found: false
Finding in an Empty List (cont.)
53
start is set to nullptr
Node<T> *ptr = start;bool found = false;while (ptr != nullptr && !found ) {
if ( ptr->item == item ) found = true;
if ( !found ) ptr = ptr->next;
}
itemmaker: Mercedes price: year:operator ==
ptr is set to nullptr
found: false
Finding in an Empty List (cont.)
Exit loop because ptr is nullptr.
54
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:1. place the element into a node and 2. insert the node at the beginning of the
linked list.
55
Inserting a Node at Frontelement
start
All new nodes must be made in the heap, SO…
56
element
start
Node<T> *ptr = new Node<T>;
ptr
Inserting a Node at Front (cont.)
57
element
start
Node<T> *ptr = new Node<T>;
ptr
Now we have to store element into the node
Inserting a Node at Front (cont.)
58
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;
ptr
Inserting a Node at Front (cont.)
59
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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.)
60
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;
ptrYou can’t successfully write code like this without thinking about addresses.
Inserting a Node at Front (cont.)
61
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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.)
62
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;
ptrRight now, the pointer called “next” doesn’t have a valid address assigned to it.
Inserting a Node at Front (cont.)
63
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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.)
64
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;
ptr Where is the address of the first node stored?
Inserting a Node at Front (cont.)
65
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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.)
66
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;
ptr That’s right, in the start pointer.
Inserting a Node at Front (cont.)
67
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;
ptr So now, all we have to do is copy that address into the pointer called “next”
Inserting a Node at Front (cont.)
68
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;ptr->next = start;
ptr
Inserting a Node at Front (cont.)
69
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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.
70
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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
71
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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?
72
element
start
Node<T> *ptr = new Node<T>;ptr->item = 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.
73
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;ptr->next = start;start = ptr;
ptr
Inserting a Node at Front (cont.)
74
element
start
Node<T> *ptr = new Node<T>;ptr->item = element;ptr->next = start;start = ptr;
ptr
Inserting a Node at Front (cont.)
Easy, right?
75
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).
76
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.
77
Inserting a Node at Middleelement
maker: Mercedes price: year:operator !=
Node<T> *ptr = start;while ( ptr->item != element ) // element is a parameter
ptr = ptr->next;
start
After this code executes, ptr points to the node that has Mercedes.
ptr
…
Mer
cede
s
78
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
cede
s
79
Well, all new nodes are created in the heap, SO…..
Inserting a Node at Middle (cont.)start ptr
maker: Honda price: 5000year: 1985operator !=
elementToInsert
…
Mer
cede
s
80
maker: Honda price: 5000year: 1985operator !=
Node<T> *newNode = new Node<T>;
newNode
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
cede
s
81
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
cede
s
82
maker: Honda price: 5000year: 1985operator !=
Node<T> *newNode = new Node<T>;newNode->item = elementToInsert;
newNode
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
cede
s
83
maker: Honda price: 5000year: 1985operator !=
Node<T> *newNode = new Node<T>;newNode->item = elementToInsert;
newNode
Inserting a Node at Middle (cont.)start ptr
elementToInsert
…
Mer
cede
s
84
Node<T> *newNode = new Node<T>;newNode->item = 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
cede
s
85
Node<T> *newNode = new Node<T>;newNode->item = 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
cede
s
86
Node<T> *newNode = new Node<T>;newNode->item = 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
cede
s
87
Node<T> *newNode = new Node<T>;newNode->item = 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
cede
s
88
Node<T> *newNode = new Node<T>;newNode->item = elementToInsert;
newNode
Where is the address of the last node stored?
Inserting a Node at Middle (cont.)start ptr
…
Mer
cede
s
89
Node<T> *newNode = new Node<T>;newNode->item = elementToInsert;
newNode
That’s right, it is stored in ptr->next
Inserting a Node at Middle (cont.)start ptr
…
Mer
cede
s
90
Node<T> *newNode = new Node<T>;newNode->item = elementToInsert;newNode->next = ptr->next;
newNode
Inserting a Node at Middle (cont.)start ptr
…
Mer
cede
s
91
Node<T> *newNode = new Node<T>;newNode->item = elementToInsert;newNode->next = ptr->next;ptr->next = newNode;
newNode
Inserting a Node at Middle (cont.)start
Mer
cede
s
ptr
…
Mer
cede
s
92
Removing a Node• Let’s assume that we want to remove a
new node at the beginning of a linked list.• We would like to:
1. create a new pointer to point to the first node,
2. point the start node to the second node and
3. delete the first node by freeing the memory and set the pointer to nullptr
93
Removing the First Node
start
…
Node<T> *ptr = start;start = start->next;delete ptr;ptr = nullptr
94
Removing the First Node(cont.)
start
Node<T> *ptr = start;start = start->next;delete ptr;ptr = nullptr
ptr
…
95
Removing the First Node(cont.)
…
Mer
cede
s
Node<T> *ptr = start;start = start->next;delete ptr;ptr = nullptr;
ptr start
96
Removing the First Node(cont.)
…
Node<T> *ptr = start;start = start->next;delete ptr; ptr = nullptr;
startptr
Well, start points to the beginning of the new linked list, but a node isn’t removed unless we free it.
97
Removing the First Node(cont.)
…
Node<T> *ptr = start;start = start->next;delete ptr;ptr = nullptr;
startptr
Well, start points to the beginning of the new linked list, but a node isn’t removed unless we free it.
98
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).
99
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.
100