100
1 Chapter 6 Methods for Making Data Structures

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

Embed Size (px)

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.

Citation preview

Page 1: 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

1

Chapter 6Methods for Making

Data Structures

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

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…

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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.

Page 9: 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

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.

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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.

Page 14: 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

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

Page 15: 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

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.

Page 16: 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

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.

Page 17: 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

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.

Page 18: 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

18

Example of a Linked Structure (cont.)

nextitem

Page 19: 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

19

Example of a Linked Structure

start

nextitem

Page 20: 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

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.

Page 21: 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

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.

Page 22: 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

22

Linked ListAdvantages

… …5 3 7 2 1

Removing an element from the middle of a linked list is fast.

Page 23: 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

23

Linked ListAdvantages (cont.)

… …5 3 2 1

Removing an element from the middle of a linked list is fast.

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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

Page 27: 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

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!

Page 28: 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

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.

Page 29: 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

29

Linked ListAdvantages (cont.)

start

Linked List

Array

Page 30: 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

30

Accessing item

To access the item in the first node:

start->item

start

dereference and member access in one shot

Page 31: 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

31

Accessing item(cont.)

To access the item in the second node:

start->next->item

start

Page 32: 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

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

Page 33: 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

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

Page 34: 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

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

Page 35: 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

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

Page 36: 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

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

Page 37: 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

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

Page 38: 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

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

Page 39: 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

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

Page 40: 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

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

Page 41: 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

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

Page 42: 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

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

Page 43: 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

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

Page 44: 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

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

Page 45: 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

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

Page 46: 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

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

Page 47: 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

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.

Page 48: 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

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.

Page 49: 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

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.

Page 50: 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

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

Page 51: 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

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

Page 52: 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

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

Page 53: 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

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.

Page 54: 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

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.

Page 55: 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

55

Inserting a Node at Frontelement

start

All new nodes must be made in the heap, SO…

Page 56: 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

56

element

start

Node<T> *ptr = new Node<T>;

ptr

Inserting a Node at Front (cont.)

Page 57: 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

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

Page 58: 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

58

element

start

Node<T> *ptr = new Node<T>;ptr->item = element;

ptr

Inserting a Node at Front (cont.)

Page 59: 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

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

Page 60: 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

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

Page 61: 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

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

Page 62: 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

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

Page 63: 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

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

Page 64: 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

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

Page 65: 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

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

Page 66: 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

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

Page 67: 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

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

Page 68: 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

68

element

start

Node<T> *ptr = new Node<T>;ptr->item = element;ptr->next = start;

ptr

Inserting a Node at Front (cont.)

Page 69: 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

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.

Page 70: 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

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

Page 71: 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

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?

Page 72: 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

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.

Page 73: 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

73

element

start

Node<T> *ptr = new Node<T>;ptr->item = element;ptr->next = start;start = ptr;

ptr

Inserting a Node at Front (cont.)

Page 74: 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

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?

Page 75: 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

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

Page 76: 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

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.

Page 77: 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

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

Page 78: 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

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

Page 79: 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

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

Page 80: 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

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

Page 81: 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

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

Page 82: 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

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

Page 83: 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

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

Page 84: 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

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

Page 85: 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

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

Page 86: 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

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

Page 87: 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

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

Page 88: 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

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

Page 89: 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

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

Page 90: 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

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

Page 91: 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

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

Page 92: 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

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

Page 93: 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

93

Removing the First Node

start

Node<T> *ptr = start;start = start->next;delete ptr;ptr = nullptr

Page 94: 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

94

Removing the First Node(cont.)

start

Node<T> *ptr = start;start = start->next;delete ptr;ptr = nullptr

ptr

Page 95: 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

95

Removing the First Node(cont.)

Mer

cede

s

Node<T> *ptr = start;start = start->next;delete ptr;ptr = nullptr;

ptr start

Page 96: 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

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.

Page 97: 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

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.

Page 98: 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

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

Page 99: 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

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.

Page 100: 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

Reference

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

100