1 linked lists (continued (continued)) lecture 5 (maybe) copying and sorting singly linked lists...

112
1 Linked Lists (continued (continued)) Lecture 5 (maybe) • Copying and sorting singly linked lists • Lists with head and last nodes • Doubly linked lists • Append/Circular Lists/Green Coding ADS2 Lecture 5

Upload: rosamund-elliott

Post on 30-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

1

Linked Lists (continued (continued))

Lecture 5 (maybe)

• Copying and sorting singly linked lists• Lists with head and last nodes• Doubly linked lists• Append/Circular Lists/Green Coding

ADS2 Lecture 5

Page 2: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Sorting a singly linked list

nullhead

Two options (of some)

- keep it sorted (insert in order)- we could use a bubble sort with 2 cursors

Page 3: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Singly linked lists with head and last/tail nodes

3

Makes insertion from end easierSounds like a great idea …

Baltimorehead

Rome Seattle Toronto

lastADS2 Lecture 5

Page 4: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Adding a new node at the “last/tail”

4

Add node containing the string “Zurich” to tail of list

head Rome Seattle Toronto

last

Zurichcreate new node containing (reference to) string “Zurich”, with next =null

head Rome Seattle Toronto

last

Zurich

Redirect last.next to new node

head Rome Seattle Toronto

last

Zurich Reallocate last to new node

ADS2 Lecture 5

Page 5: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Adding a new node at the “last/tail”

Psuedo code snippet

Node node = new Node(e,null)tail.setNext(node)tail = nodesize++

Page 6: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Singly linked lists with head and last/tail nodes

6

Makes insertion from end easierSounds like a great idea …

Baltimorehead

Rome Seattle Toronto

lastADS2 Lecture 5

But what happens if we delete the last node

Page 7: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Doubly linked lists

7

• Removing an element from the tail of a singly linked list is not easy Whether we have just a head, or a head and a last node, need to always traverse the whole list to remove the end. Why?

• In general it is hard to remove any node other than the

head We don’t have a quick way of working out which is the node in front of the one we want to remove.

• For applications where we want quick access to the predecessor node of any node, we use a doubly linked list. A list in which we can go in both directions.

ADS2 Lecture 5

Page 8: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

8

Doubly Linked Lists (Goodrich § 3.3)

• A doubly linked list is a concrete data structure consisting of a sequence of nodes

• Each node stores– element– link to the next node– link to previous node

ADS2 Lecture 5

next

elem node

prev

A B Cfirst

last

Page 9: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

9

ADS2 Lecture 5

DNode

Page 10: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

10

ADS2 Lecture 5

DNode

Page 11: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

11

ADS2 Lecture 5

DNode

Page 12: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

12

ADS2 Lecture 5

DNode

Page 13: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

13

ADS2 Lecture 5

DNode

Page 14: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

14

ADS2 Lecture 5

DNode

Page 15: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

15

ADS2 Lecture 5

DNode

Page 16: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

16

ADS2 Lecture 5

DList

Page 17: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

17

ADS2 Lecture 5

DList

Page 18: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

18

ADS2 Lecture 5

DList

Page 19: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

19

ADS2 Lecture 5

DList

Page 20: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

20

ADS2 Lecture 5

DList

Page 21: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

21

ADS2 Lecture 5

DList

Page 22: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

22

ADS2 Lecture 5

DList

Page 23: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

iteration refresher

Page 24: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Page 25: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Declare loop variable and initialise

Page 26: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Continuing condition

Page 27: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Action at end of loop on each iteration

Page 28: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Page 29: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Declare loop variable and initialise

Page 30: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Continuing condition

Page 31: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Action at end of loop on each iteration

Page 32: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Page 33: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Declare loop variable and initialise

Page 34: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Continuing condition

Page 35: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Iteration

Action at end of loop on each iteration

Page 36: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

36

ADS2 Lecture 5

DList

This is an example of really tight coding

Could we make it tighter? (maybe remove found?)

Any comments?

Page 37: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

37

ADS2 Lecture 5

DList

As tight as it gets?

Page 38: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

end of iteration refresher

Page 39: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

39ADS2 Lecture 5

Insertion into the middle of a doubly linked list

insert here

node d

node a node b node c

node a node d node b node c

• make node d’s prev link point to node a• make node d’s next link point to node b• make node b’s prev link point to node d• make node a’s next link point to node d

Page 40: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 41: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

NOTE: case analysis. This can make coding a bit less complicated

Page 42: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 43: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 44: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 45: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

A0

Page 46: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

A0

Page 47: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

A0

Page 48: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

A0

Page 49: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

A0

Page 50: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 51: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 52: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first last

D4

Page 53: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2first last

D4

Page 54: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2first last

D4

Page 55: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2first last

D4

Page 56: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2first last

D4

Page 57: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 58: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Page 59: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Find insertion point

Page 60: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Insert new node

Page 61: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Insert new node

Page 62: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

B0

x

y

z

Page 63: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

B0

x

y

z

Page 64: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

B0

x

y

z

Page 65: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

B0

x

y

z

Page 66: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

A1 B3 C2 first

B0

x

y

z

Page 67: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList insert

Done!

Page 68: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular
Page 69: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Irrelevant fact … my 1st passport was paid for by Burroughs MachinesCorporation. It came with an indefinite US visa and had as my “Profession” …

Passports no longer have “Profession”

Random Fact #1

Computer Programmer

Page 70: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Irrelevant fact … my 1st passport was paid for by Burroughs MachinesCorporation. It came with an indefinite US visa and had as my “Profession” …

Passports no longer have “Profession”

Random Fact #1

Still Learning!!!!

Page 71: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Random Fact #2

What computers looked like when I was a young man working at Burroughs

Page 72: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

The dark side of Burroughs (William S.)

Random Fact #3

Page 73: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Random Fact #4

Page 74: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

end of random facts #1 to #4

Page 75: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 75

Removal from the middle of a doubly linked list

node a node d node b node c

remove this node

• make node a’s next link point to node d.next• make node b’s prev link point to node d.prev

node a node b node c

Page 76: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

But again, do you see the case analysis?

Page 77: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

Page 78: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

Page 79: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

Private … only used here, not by user

Page 80: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

Page 81: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

Ahead

last

Page 82: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

garbage

Ahead

last

Page 83: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 84: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 85: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 86: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

garbage

A B C first last

Page 87: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 88: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 89: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 90: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

garbage

A B C first last

Page 91: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 92: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 93: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

node

A B C first last

Page 94: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList delete

garbage

A B C first last

Page 95: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

DList Test

Page 96: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular
Page 97: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Generic linked lists

ADS2 Lecture 5 97

• Rather than use a linked list that can only store objects of a certain type, can use a generic linked list (either generic singly linked list or generic doubly linked list).

• Need a generic node to implement the list

Page 98: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 98

Java code for a node of a generic singly linked list: GList.java

Page 99: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 99

Java code for a node of a generic singly linked list: GList.java

Page 100: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 100

Java code for a node of a generic singly linked list: GList.java

Page 101: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 101

Java code for a node of a generic singly linked list: GList.java

Page 102: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 102

Java code for a node of a generic singly linked list: GList.java

Page 103: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 103

Java code for a node of a generic singly linked list: GList.java

Page 104: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular
Page 105: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular
Page 106: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 106

Page 107: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular
Page 108: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular
Page 109: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular
Page 110: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

Comparing: array, linked list, doubly linked list

insert an item in orderdelete an itemget the ith item

Page 111: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

“green” coding?

When we delete an element of the list it becomes garbage.

Could we recycle deleted nodes?

Page 112: 1 Linked Lists (continued (continued)) Lecture 5 (maybe) Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists Append/Circular

ADS2 Lecture 5 112