Lecture06 methods for-making_data_structures_v2

110
1 Chapter 6 Methods for Making Data Structures

description

 

Transcript of Lecture06 methods for-making_data_structures_v2

Page 1: Lecture06 methods for-making_data_structures_v2

1

Chapter 6Methods for Making

Data Structures

Page 2: Lecture06 methods for-making_data_structures_v2

2

Dynamic Arrays in Data Structures

• In almost every data structure, we want functions for inserting and removing data.

• When dynamic arrays are used, the insertion function would add data to the array, while the removal function would “eliminate” data from the array (make it unusable).

• When the array becomes full, we would want to do an expansion – when many elements have been removed, we would want to do a contraction, so that only the used elements remain.

Page 3: Lecture06 methods for-making_data_structures_v2

3

Array Expansion/Contraction

• One possible method:– When an element is inserted by the client,

increase the size of the array by 1.– When an element is removed by the client,

decrease the size of the array by 1.• The problem with this method is that it is

inefficient – every time an element is inserted or removed, the changeSize function is called…

Page 4: Lecture06 methods for-making_data_structures_v2

4

changeSize Function

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445

33

New element needs to be put into array, so changeSize function is called

Page 5: Lecture06 methods for-making_data_structures_v2

5

changeSize Function(cont.)

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445

… 0 1 2 3 432 433 444 445 446

new array is made

Page 6: Lecture06 methods for-making_data_structures_v2

6

changeSize Function(cont.)

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445

elements are copied over one by one using a for loop

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446

Page 7: Lecture06 methods for-making_data_structures_v2

7

changeSize Function(cont.)

Then, the new element can be put in

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446

33

33

This process would take place every time a new element needs to be inserted.

Page 8: Lecture06 methods for-making_data_structures_v2

8

changeSize Function(cont.)

Suppose the element at the end of the array needs to be removed.

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446

33

Likewise, when an element needs to be removed, this method contracts the array by one to conserve memory.

Page 9: Lecture06 methods for-making_data_structures_v2

9

changeSize Function(cont.)

The changeSize function is called and a new, smaller array is made.

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446

33

… 0 1 2 3 432 433 444 445

Page 10: Lecture06 methods for-making_data_structures_v2

10

changeSize Function(cont.)

The elements are copied over one by one, using a for loop.

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445 446

33

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445

Page 11: Lecture06 methods for-making_data_structures_v2

11

changeSize Function(cont.)

This method of array expansion/contraction is largely inefficient, because there is too much element copying.

25 75 10 12 56 32 73 87… 0 1 2 3 432 433 444 445

Page 12: Lecture06 methods for-making_data_structures_v2

12

Linked Structures

• Sometimes it is best to store data in a linked structure (an alternative to an Array)

• A linked structure consists of a group of nodes – each node is made from a struct.

• An object of the Node struct contains an element of data.

Page 13: Lecture06 methods for-making_data_structures_v2

13

A Node Struct Template

template <typename T>struct Node {

T info;Node<T> *next;

};

The info member is for the data. It can anything (T), but it is often the object of another struct, used as a record of information.

The next pointer stores the address of a Node of the same type! This means that each node can point to another node.

Page 14: Lecture06 methods for-making_data_structures_v2

14

Nodes

• In a data structure, each node is made in the heap; therefore, a node can only be accessed by a pointer.

• The client does not deal with nodes. • When the client uses an insertion function,

an element of data is passed into the insert function, and the function places it in a node.

Page 15: Lecture06 methods for-making_data_structures_v2

15

Nodes (cont.)

• When the client wants to retrieve data, the data in a node is returned to the client (but not the node itself).

• The node struct template exists for use by the data structure.

Page 16: Lecture06 methods for-making_data_structures_v2

16

Example of a Linked Structure

start

Each blue node is divided into two sections, for the two members of the Node struct.

Page 17: Lecture06 methods for-making_data_structures_v2

17

Example of a Linked Structure (cont.)

start

The right section is the pointer called “next”.

The left section is the info member.

Page 18: Lecture06 methods for-making_data_structures_v2

18

Example of a Linked Structure (cont.)

start

The last node doesn’t point to another node, so its pointer (called next) is set to NULL (indicated by slash).

The start pointer would be saved in the private section of a data structure class.

Page 19: Lecture06 methods for-making_data_structures_v2

19

Linked Lists

• The arrangement of nodes in the linked structure on the previous slide is often called a linked list.

• We can access any element of the linked list, for retrieval of information.

• We can also remove any element from the linked list (which would shorten the list).

• We can also insert any element into any position in the linked list.

Page 20: Lecture06 methods for-making_data_structures_v2

20

Linked ListAdvantages

… …5 3 7 2 1

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

Page 21: Lecture06 methods for-making_data_structures_v2

21

Linked ListAdvantages (cont.)

… …5 3 2 1

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

Page 22: Lecture06 methods for-making_data_structures_v2

22

Removal Problem in Array

… …

Removing elements from the middle of an array (without leaving gaps) is more problematic.

25 75 10 12

211 212 213 214 215 216 217 218

33 49 29 87

Page 23: Lecture06 methods for-making_data_structures_v2

23

Removal Problem in Array (cont.)

… …

A loop must be used to slide each element on the right one slot to the left, one at a time…

25 75 10

211 212 213 214 215 216 217 218

33 49 29 87

Page 24: Lecture06 methods for-making_data_structures_v2

24

Removal Problem in Array (cont.)

… …25 75 10

211 212 213 214 215 216 217 218

49 29 8733

… …25 75 10

211 212 213 214 215 216 217 218

49 29 8733

… …25 75 10

211 212 213 214 215 216 217 218

49 29 8733

Page 25: Lecture06 methods for-making_data_structures_v2

25

Removal Problem in Array (cont.)

… …25 75 10

211 212 213 214 215 216 217 218

49 29 8733

Only 100,000 more to go!

Page 26: Lecture06 methods for-making_data_structures_v2

26

Linked ListAdvantages (cont.)

• Linked lists also waste less memory for large elements (records of information).

• Wasted memory is memory space in the data structure not used for data.

• In arrays, the wasted memory is the part of the array not being utilized.

• In linked lists, the wasted memory is the pointer in each node.

Page 27: Lecture06 methods for-making_data_structures_v2

27

Linked ListAdvantages (cont.)

start

Linked List

Array

Page 28: Lecture06 methods for-making_data_structures_v2

28

Accessing info

To access the info in the first node:

(*start).info

Or (better yet)

start->info

start

dereference and member access in one shot

Page 29: Lecture06 methods for-making_data_structures_v2

29

Accessing info(cont.)

To access the info in the second node:

start->next->info

start

Page 30: Lecture06 methods for-making_data_structures_v2

30

Finding a Possible Mercedes

Let’s solve the problem, but let’s assume that item is passed in as a parameter (of type T). This is normally what would happen.Instead of the CarType struct having an overloaded != operator, it will have an overloaded == operator.

itemmaker: Mercedes price: year:operator ==

start

Me

rce

de

s

Page 31: Lecture06 methods for-making_data_structures_v2

31

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) // overloaded ==found = true;

if ( !found )ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 32: Lecture06 methods for-making_data_structures_v2

32

CarType item;item.maker = "Mercedes";

Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) // overloaded ==found = true;

if ( !found )ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 33: Lecture06 methods for-making_data_structures_v2

33

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;

bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) // overloaded ==found = true;

if ( !found )ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 34: Lecture06 methods for-making_data_structures_v2

34

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;

while (ptr != NULL && !found ) {if ( ptr->info == item ) // overloaded ==

found = true;if ( !found )

ptr = ptr->next;}

itemmaker: Mercedes price: year:operator ==

ptr

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 35: Lecture06 methods for-making_data_structures_v2

35

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) // overloaded ==found = true;

if ( !found )ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 36: Lecture06 methods for-making_data_structures_v2

36

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found )ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 37: Lecture06 methods for-making_data_structures_v2

37

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found )

ptr = ptr->next;}

itemmaker: Mercedes price: year:operator ==

ptr

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 38: Lecture06 methods for-making_data_structures_v2

38

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;

while (ptr != NULL && !found ) { if ( ptr->info == item )

found = true;if ( !found )

ptr = ptr->next;}

itemmaker: Mercedes price: year:operator ==

ptr

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 39: Lecture06 methods for-making_data_structures_v2

39

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) // overloaded ==found = true;

if ( !found )ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

Page 40: Lecture06 methods for-making_data_structures_v2

40

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found ) ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

found: false

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

ptr

Page 41: Lecture06 methods for-making_data_structures_v2

41

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;

while (ptr != NULL && !found ) { if ( ptr->info == item )

found = true;if ( !found )

ptr = ptr->next;}

itemmaker: Mercedes price: year:operator ==

found: false

After going through the loop several times…

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

ptr

Page 42: Lecture06 methods for-making_data_structures_v2

42

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found ) ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

found: false

Notice that found is only set to true if ptr is not NULL and Mercedes is found …

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

ptr

Page 43: Lecture06 methods for-making_data_structures_v2

43

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;

while (ptr != NULL && !found ) { if ( ptr->info == item )

found = true;if ( !found )

ptr = ptr->next;}

itemmaker: Mercedes price: year:operator ==

found: false

then, !found is false and the loop exits

Finding a Possible Mercedes (cont.)

start

Me

rce

de

s

ptr

Page 44: Lecture06 methods for-making_data_structures_v2

44

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found ) ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

found: false

If Mercedes is not found, ptr eventually gets set to NULL.

What If Mercedes Does Not Exist?

start

ptr

Page 45: Lecture06 methods for-making_data_structures_v2

45

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found )

ptr = ptr->next;}

itemmaker: Mercedes price: year:operator ==

ptr is set to NULL

found: false

What If Mercedes Does not Exist? (cont.)

start

If Mercedes is not found, ptr eventually gets set to NULL.

Page 46: Lecture06 methods for-making_data_structures_v2

46

CarType item;item.maker = "Mercedes";Node<T> *ptr = start;bool found = false;

while (ptr != NULL && !found ) { if ( ptr->info == item )

found = true;if ( !found )

ptr = ptr->next;}

itemmaker: Mercedes price: year:operator ==

ptr is set to NULL

found: false

What If Mercedes Does not Exist? (cont.)

start

Exit from loop because ptr is NULL.

Page 47: Lecture06 methods for-making_data_structures_v2

47

What If Finding in an Empty Linked List?

• When a linked list is empty, the start pointer should always be set to NULL.

• The start pointer would be set to NULL inside the constructor, when an empty linked list is first made.

Page 48: Lecture06 methods for-making_data_structures_v2

48

start is set to NULL

Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found ) ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

SAME CODE

Finding in an Empty List

Page 49: Lecture06 methods for-making_data_structures_v2

49

start is set to NULL

Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found ) ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr is set to NULL

Finding in an Empty List (cont.)

Page 50: Lecture06 methods for-making_data_structures_v2

50

start is set to NULL

Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found ) ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr is set to NULL

found: false

Finding in an Empty List (cont.)

Page 51: Lecture06 methods for-making_data_structures_v2

51

start is set to NULL

Node<T> *ptr = start;bool found = false;while (ptr != NULL && !found ) {

if ( ptr->info == item ) found = true;

if ( !found ) ptr = ptr->next;

}

itemmaker: Mercedes price: year:operator ==

ptr is set to NULL

found: false

Finding in an Empty List (cont.)

Exit loop because ptr is NULL.

Page 52: Lecture06 methods for-making_data_structures_v2

52

Inserting a New Node

• Let’s assume that we want to insert a new node at the beginning of a linked list.

• Assume that the client passes in a parameter called element (of type T).

• We would like to place the element into a node and insert the node at the beginning of the linked list.

Page 53: Lecture06 methods for-making_data_structures_v2

53

Inserting a Node at Front

element

start

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

Page 54: Lecture06 methods for-making_data_structures_v2

54

element

start

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

ptr

Inserting a Node at Front (cont.)

Page 55: Lecture06 methods for-making_data_structures_v2

55

element

start

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

ptr

Now we have to store element into the node

Inserting a Node at Front (cont.)

Page 56: Lecture06 methods for-making_data_structures_v2

56

element

start

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

ptr

Inserting a Node at Front (cont.)

Page 57: Lecture06 methods for-making_data_structures_v2

57

element

start

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

ptrNow we have to think about how to make the pointer called “next” point to the first node in the list, to link it in

Inserting a Node at Front (cont.)

Page 58: Lecture06 methods for-making_data_structures_v2

58

element

start

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

ptrYou can’t successfully write code like this without thinking about addresses.

Inserting a Node at Front (cont.)

Page 59: Lecture06 methods for-making_data_structures_v2

59

element

start

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

ptrREMEMBER…when you want to change the way a pointer points, you HAVE to assign a different address to it

Inserting a Node at Front (cont.)

Page 60: Lecture06 methods for-making_data_structures_v2

60

element

start

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

ptrRight now, the pointer called “next” doesn’t have a valid address assigned to it.

Inserting a Node at Front (cont.)

Page 61: Lecture06 methods for-making_data_structures_v2

61

element

start

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

ptrTo store the correct address in it, we have to find the address of the first node of the linked list.

Inserting a Node at Front (cont.)

Page 62: Lecture06 methods for-making_data_structures_v2

62

element

start

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

ptr Where is the address of the first node stored?

Inserting a Node at Front (cont.)

Page 63: Lecture06 methods for-making_data_structures_v2

63

element

start

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

ptrNow think, the address would be stored in something that points to it. So where is it stored?

Inserting a Node at Front (cont.)

Page 64: Lecture06 methods for-making_data_structures_v2

64

element

start

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

ptr That’s right, in the start pointer.

Inserting a Node at Front (cont.)

Page 65: Lecture06 methods for-making_data_structures_v2

65

element

start

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

ptr So now, all we have to do is copy that address into the pointer called “next”

Inserting a Node at Front (cont.)

Page 66: Lecture06 methods for-making_data_structures_v2

66

element

start

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

ptr

Inserting a Node at Front (cont.)

Page 67: Lecture06 methods for-making_data_structures_v2

67

element

start

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

ptr

Inserting a Node at Front (cont.)

Well, it’s been inserted. But start should point to the first node now.

Page 68: Lecture06 methods for-making_data_structures_v2

68

element

start

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

ptr

Inserting a Node at Front (cont.)

REMEMBER…when you want to change the way a pointer points, you have to assign a different address to it

Page 69: Lecture06 methods for-making_data_structures_v2

69

element

start

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

ptr

Inserting a Node at Front (cont.)

We’d like start to point to the new node, so what stores the address of the new node?

Page 70: Lecture06 methods for-making_data_structures_v2

70

element

start

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

ptr

Inserting a Node at Front (cont.)

That’s right, ptr. So now all we have to do is assign the address stored in ptr to the start pointer.

Page 71: Lecture06 methods for-making_data_structures_v2

71

element

start

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

ptr

Inserting a Node at Front (cont.)

Page 72: Lecture06 methods for-making_data_structures_v2

72

element

start

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

ptr

Inserting a Node at Front (cont.)

Easy, right?

Page 73: Lecture06 methods for-making_data_structures_v2

73

REMEMBER…

• Use drawings when working with linked lists, until you become an expert.

• When you want to change the way a pointer points, you have to assign a different address to it.

• You can find the address you need by looking at other pointers (remember that they store addresses).

Page 74: Lecture06 methods for-making_data_structures_v2

74

Inserting into the Middle of a Linked List

• Suppose we know that there is a Mercedes in a linked list.

• We would like to insert a node containing Honda right after it.

• We first find the Mercedes, using code that we looked at before.

Page 75: Lecture06 methods for-making_data_structures_v2

75

Inserting a Node at Middleelement

maker: Mercedes price: year:operator !=

Node<T> *ptr = start;while ( ptr->info != element ) // element is a parameter

ptr = ptr->next;

start

After this code executes, ptr points to the node that has Mercedes.

ptr

Mer

ced

es

Page 76: Lecture06 methods for-making_data_structures_v2

76

elementmaker: Mercedes price: year:operator !=

Now we would like to insert a CarType object called elementToInsert (containing Honda), which would also be passed in as a parameter, right after the Mercedes

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 77: Lecture06 methods for-making_data_structures_v2

77

maker: Honda price: 5000year: 1985operator !=

Well, all new nodes are created in the heap, SO…..

Inserting a Node at Middle (cont.)start ptr

elementToInsert

Mer

ced

es

Page 78: Lecture06 methods for-making_data_structures_v2

78

maker: Honda price: 5000year: 1985operator !=

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

newNode

Inserting a Node at Middle (cont.)start ptr

elementToInsert

Mer

ced

es

Page 79: Lecture06 methods for-making_data_structures_v2

79

maker: Honda price: 5000year: 1985operator !=

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

newNode

Now, how about placing elementToInsert into the new node?

Inserting a Node at Middle (cont.)start ptr

elementToInsert

Mer

ced

es

Page 80: Lecture06 methods for-making_data_structures_v2

80

maker: Honda price: 5000year: 1985operator !=

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

Inserting a Node at Middle (cont.)start ptr

elementToInsert

Mer

ced

es

Page 81: Lecture06 methods for-making_data_structures_v2

81

maker: Honda price: 5000year: 1985operator !=

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

Inserting a Node at Middle (cont.)start ptr

elementToInsert

Mer

ced

es

Page 82: Lecture06 methods for-making_data_structures_v2

82

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

Now, what we want is shown by the dashed arrows; this would cause the insertion of the node

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 83: Lecture06 methods for-making_data_structures_v2

83

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

We have two pointers we need to change – but we have to be careful about the way we change them

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 84: Lecture06 methods for-making_data_structures_v2

84

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

If we change the left pointer first, we will no longer be able to access the last node (memory leak)

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 85: Lecture06 methods for-making_data_structures_v2

85

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

So, we first have to assign the address of the last node into the “next” pointer of the new node

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 86: Lecture06 methods for-making_data_structures_v2

86

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

Where is the address of the last node stored?

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 87: Lecture06 methods for-making_data_structures_v2

87

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;

newNode

That’s right, it is stored in ptr->next

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 88: Lecture06 methods for-making_data_structures_v2

88

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;newNode->next = ptr->next;

newNode

Inserting a Node at Middle (cont.)start ptr

Mer

ced

es

Page 89: Lecture06 methods for-making_data_structures_v2

89

Node<T> *newNode = new Node<T>;newNode->info = elementToInsert;newNode->next = ptr->next;ptr->next = newNode;

newNode

Inserting a Node at Middle (cont.)start

Mer

cede

s

ptr

Mer

ced

es

Page 90: Lecture06 methods for-making_data_structures_v2

90

Removing a Node

• Suppose we definitely know there is a Mercedes in the linked list and we wish to remove the node that contains it.

• We need to find the node first.

Page 91: Lecture06 methods for-making_data_structures_v2

91

Removing the First Node

start

Me

rce

des

Node<T> *ptr = start;if ( ptr->info == element )

start = start->next;

Mercedes is in the first node

Page 92: Lecture06 methods for-making_data_structures_v2

92

Removing the First Node(cont.)

start

Node<T> *ptr = start;if ( ptr->info == element )

start = start->next;

ptr

Me

rce

des

Page 93: Lecture06 methods for-making_data_structures_v2

93

Removing the First Node(cont.)

start

Node<T> *ptr = start;if ( ptr->info == element )

start = start->next;

ptr

Me

rce

des

Page 94: Lecture06 methods for-making_data_structures_v2

94

Removing the First Node(cont.)

Me

rce

des

Node<T> *ptr = start;if ( ptr->info == element )

start = start->next;

ptr start

Page 95: Lecture06 methods for-making_data_structures_v2

95

Removing the First Node(cont.)

Node<T> *ptr = start;if ( ptr->info == element ) {

start = start->next;delete ptr;

}

startptr

Well, start points to the beginning of the new linked list, but a node isn’t removed unless we free it.

Page 96: Lecture06 methods for-making_data_structures_v2

96

Removing the First Node(cont.)

Node<T> *ptr = start;if ( ptr->info == element ) {

start = start->next;delete ptr;

}

startptr

Now, let’s consider the other case whereby Mercedes is in the middle of the list.

Page 97: Lecture06 methods for-making_data_structures_v2

97

Removing a Middle Node

start

else {while ( ptr->next->info != element )

ptr = ptr->next;

The while loop points ptr to the node BEFORE the node that has Mercedes.

ptr

Me

rce

des

Page 98: Lecture06 methods for-making_data_structures_v2

98

Removing a Middle Node (cont.)

start

else {while ( ptr->next->info != element )

ptr = ptr->next;

ptr

Me

rce

des

We need to join the node before Mercedes to the node after Mercedes

Page 99: Lecture06 methods for-making_data_structures_v2

99

Removing a Middle Node (cont.)

start

else {while ( ptr->next->info != element )

ptr = ptr->next;

Node<T> *ptr2 = ptr->next;

ptr ptr2

Me

rce

des

But we mus keep a pointer to the node that has Mercedes

Page 100: Lecture06 methods for-making_data_structures_v2

100

Removing a Middle Node (cont.)

start

else {while ( ptr->next->info != element )

ptr = ptr->next;

Node<T> *ptr2 = ptr->next;

ptr->next = ptr2->next;

ptr ptr2

Me

rce

des

We now join the node before Mercedes to the node after Mercedes

Page 101: Lecture06 methods for-making_data_structures_v2

101

Removing a Middle Node (cont.)

start

else {while ( ptr->next->info != element )

ptr = ptr->next;

Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;

delete ptr2;

ptr ptr2

We then delete the node that has Mercedes

We did it!

Page 102: Lecture06 methods for-making_data_structures_v2

102

What If Mercedes is the Last Node?

• Would our code still work?• Try to consider every possible situation in

code design.

Page 103: Lecture06 methods for-making_data_structures_v2

103

Removing the Last Node

else {

while ( ptr->next->info != element ) ptr = ptr->next;

Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;delete ptr2;}

start

Me

rce

des

ptr

After looping, ptr stops on the next-to-the-last node

Page 104: Lecture06 methods for-making_data_structures_v2

104

else {while ( ptr->next->info != element )

ptr = ptr->next;

Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;delete ptr2;}

start ptr ptr2

Removing the Last Node (cont.)

Me

rce

des

Page 105: Lecture06 methods for-making_data_structures_v2

105

else {while ( ptr->next->info != element )

ptr = ptr->next;

Node<T> *ptr2 = ptr->next;

ptr->next = ptr2->next;delete ptr2;}

start ptr ptr2

Removing the Last Node (cont.)

Me

rce

des

Page 106: Lecture06 methods for-making_data_structures_v2

106

else {while ( ptr->next->info != element )

ptr = ptr->next;

Node<T> *ptr2 = ptr->next;ptr->next = ptr2->next;

delete ptr2;}

start ptr ptr2

Removing the Last Node (cont.)

The code works in removing the last node.

Page 107: Lecture06 methods for-making_data_structures_v2

107

Working With Linked Lists

• As you can see, sometimes you have to do a lot of thinking and problem-solving when working with linked lists.

• It is not always obvious how to write code.• You can’t memorize the code, because it

will not quite fit situations that you will encounter.

• It is a matter of using logic (and knowing a few tricks of the trade).

Page 108: Lecture06 methods for-making_data_structures_v2

108

Code for Removing a Node

1 Node<T> *ptr = start;2 if ( ptr->info == element ) { 3 start = start->next;4 delete ptr;5 }6 else {7 while ( ptr->next->info != element ) 8 ptr = ptr->next;9 Node<T> *ptr2 = ptr->next;10 ptr->next = ptr2->next;11 delete ptr2;12 }

Here is the resulting code for removing a node. It is assumes the node we are looking for is in the linked list.

Page 109: Lecture06 methods for-making_data_structures_v2

109

Speed

• In some situations, an array can be faster than a linked list, such as when a calculated index is used to access an element.

• In other situations, a linked list can be faster than an array, such as when removing an element from the middle (as we saw before).– we usually need to search for the element to remove,

but we search for it in both the array and linked list.

Page 110: Lecture06 methods for-making_data_structures_v2

Reference

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

110