Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.

Similar presentations


Presentation on theme: "1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and."— Presentation transcript:

1 1 Chapter 6 Methods for Making Data Structures

2 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 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 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 5 changeSize Function 2575101256327387 … 0 1 2 3 432 433 444 445 33 New element needs to be put into array, so changeSize function is called

6 6 changeSize Function (cont.) 2575101256327387 … 0 1 2 3 432 433 444 445 … 0 1 2 3 432 433 444 445 446 new array is made

7 7 changeSize Function (cont.) 2575101256327387 … 0 1 2 3 432 433 444 445 elements are copied over one by one using a for loop 2575101256327387 … 0 1 2 3 432 433 444 445 446

8 8 changeSize Function (cont.) Then, the new element can be put in 2575101256327387 … 0 1 2 3 432 433 444 445 446 33 This process would take place every time a new element needs to be inserted.

9 9 changeSize Function (cont.) Suppose the element at the end of the array needs to be removed. 2575101256327387 … 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 10 changeSize Function (cont.) The changeSize function is called and a new, smaller array is made. 2575101256327387 … 0 1 2 3 432 433 444 445 446 33 … 0 1 2 3 432 433 444 445

11 11 changeSize Function (cont.) The elements are copied over one by one, using a for loop. 2575101256327387 … 0 1 2 3 432 433 444 445 446 33 2575101256327387 … 0 1 2 3 432 433 444 445

12 12 changeSize Function (cont.) This method of array expansion/contraction is largely inefficient, because there is too much element copying. 2575101256327387 … 0 1 2 3 432 433 444 445

13 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 14 A Node Struct Template template struct Node { T item; Node *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. next item

15 15 A Node Struct Template template struct Node { T item; Node *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 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 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 18 Example of a Linked Structure (cont.) next item

19 19 Example of a Linked Structure start next item

20 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 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 22 Linked List Advantages … … 53721 Removing an element from the middle of a linked list is fast.

23 23 Linked List Advantages (cont.) … … 5321 Removing an element from the middle of a linked list is fast.

24 24 Removal Problem in Array …… Removing elements from the middle of an array (without leaving gaps) is more problematic. 25751012 211 212 213 214 215 216 217 218 33492987

25 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… 257510 211 212 213 214 215 216 217 218 33492987

26 26 Removal Problem in Array (cont.) …… 257510 211 212 213 214 215 216 217 218 49298733 …… 257510 211 212 213 214 215 216 217 218 49298733 …… 257510 211 212 213 214 215 216 217 218 49298733

27 27 Removal Problem in Array (cont.) …… 257510 211 212 213 214 215 216 217 218 49298733 Only 100,000 more to go!

28 28 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.

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

30 30 Accessing item To access the item in the first node: start->item start dereference and member access in one shot

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

32 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. item maker: Mercedes price: year: operator == start … Mercedes

33 33 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == Finding a Possible Mercedes (cont.) start … Mercedes

34 34 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr Finding a Possible Mercedes (cont.) start … Mercedes

35 35 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

36 36 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

37 37 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

38 38 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

39 39 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

40 40 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

41 41 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) // overloaded == found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr found: false Finding a Possible Mercedes (cont.) start … Mercedes

42 42 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == found: false Finding a Possible Mercedes (cont.) start … Mercedes ptr

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

44 44 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: 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 … Mercedes ptr

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

46 46 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: 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 47 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: 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 48 CarType item; item.maker = "Mercedes"; Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: 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 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 50 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == SAME CODE Finding in an Empty List

51 51 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr Finding in an Empty List (cont.)

52 52 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr found: false Finding in an Empty List (cont.)

53 53 start is set to nullptr Node *ptr = start; bool found = false; while (ptr != nullptr && !found ) { if ( ptr->item == item ) found = true; if ( !found ) ptr = ptr->next; } item maker: Mercedes price: year: operator == ptr is set to nullptr found: false Finding in an Empty List (cont.) Exit loop because ptr is nullptr.

54 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 55 Inserting a Node at Front element start All new nodes must be made in the heap, SO…

56 56 element start Node *ptr = new Node ; ptr Inserting a Node at Front (cont.)

57 57 element start Node *ptr = new Node ; ptr Now we have to store element into the node Inserting a Node at Front (cont.)

58 58 element start Node *ptr = new Node ; ptr->item = element; ptr Inserting a Node at Front (cont.)

59 59 element start Node *ptr = new Node ; ptr->item = element; ptr 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 Inserting a Node at Front (cont.)

60 60 element start Node *ptr = new Node ; ptr->item = element; ptr You can’t successfully write code like this without thinking about addresses. Inserting a Node at Front (cont.)

61 61 element start Node *ptr = new Node ; ptr->item = element; ptr REMEMBER…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 62 element start Node *ptr = new Node ; ptr->item = element; ptr Right now, the pointer called “next” doesn’t have a valid address assigned to it. Inserting a Node at Front (cont.)

63 63 element start Node *ptr = new Node ; ptr->item = element; ptr To 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 64 element start Node *ptr = new Node ; ptr->item = element; ptr Where is the address of the first node stored? Inserting a Node at Front (cont.)

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

66 66 element start Node *ptr = new Node ; ptr->item = element; ptr That’s right, in the start pointer. Inserting a Node at Front (cont.)

67 67 element start Node *ptr = new Node ; 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 68 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; ptr Inserting a Node at Front (cont.)

69 69 element start Node *ptr = new Node ; 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 70 element start Node *ptr = new Node ; 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 71 element start Node *ptr = new Node ; 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 72 element start Node *ptr = new Node ; 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 73 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; start = ptr; ptr Inserting a Node at Front (cont.)

74 74 element start Node *ptr = new Node ; ptr->item = element; ptr->next = start; start = ptr; ptr Inserting a Node at Front (cont.) Easy, right?

75 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 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 77 Inserting a Node at Middle element maker: Mercedes price: year: operator != Node *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 … Mercedes

78 78 element maker: 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 … Mercedes

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

80 80 maker: Honda price: 5000 year: 1985 operator != Node *newNode = new Node ; newNode Inserting a Node at Middle (cont.) start ptr elementToInsert … Mercedes

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

82 82 maker: Honda price: 5000 year: 1985 operator != Node *newNode = new Node ; newNode->item = elementToInsert; newNode Inserting a Node at Middle (cont.) start ptr elementToInsert … Mercedes

83 83 maker: Honda price: 5000 year: 1985 operator != Node *newNode = new Node ; newNode->item = elementToInsert; newNode Inserting a Node at Middle (cont.) start ptr elementToInsert … Mercedes

84 84 Node *newNode = new Node ; 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 … Mercedes

85 85 Node *newNode = new Node ; 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 … Mercedes

86 86 Node *newNode = new Node ; 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 … Mercedes

87 87 Node *newNode = new Node ; 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 … Mercedes

88 88 Node *newNode = new Node ; newNode->item = elementToInsert; newNode Where is the address of the last node stored? Inserting a Node at Middle (cont.) start ptr … Mercedes

89 89 Node *newNode = new Node ; newNode->item = elementToInsert; newNode That’s right, it is stored in ptr->next Inserting a Node at Middle (cont.) start ptr … Mercedes

90 90 Node *newNode = new Node ; newNode->item = elementToInsert; newNode->next = ptr->next; newNode Inserting a Node at Middle (cont.) start ptr … Mercedes

91 91 Node *newNode = new Node ; newNode->item = elementToInsert; newNode->next = ptr->next; ptr->next = newNode; newNode Inserting a Node at Middle (cont.) start Mercedes ptr … Mercedes

92 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 93 Removing the First Node start … Node *ptr = start; start = start->next; delete ptr; ptr = nullptr

94 94 Removing the First Node (cont.) start Node *ptr = start; start = start->next; delete ptr; ptr = nullptr ptr …

95 95 Removing the First Node (cont.) … Mercedes Node *ptr = start; start = start->next; delete ptr; ptr = nullptr; ptrstart

96 96 Removing the First Node (cont.) … Node *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 97 Removing the First Node (cont.) … Node *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 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 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.

100 Reference Childs, J. S. (2008). Methods for Making Data Structures. C++ Classes and Data Structures. Prentice Hall. 100


Download ppt "1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and."

Similar presentations


Ads by Google