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

Post on 30-Jan-2016

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

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

Adding a new node at the “last/tail”

Psuedo code snippet

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

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

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

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

9

ADS2 Lecture 5

DNode

10

ADS2 Lecture 5

DNode

11

ADS2 Lecture 5

DNode

12

ADS2 Lecture 5

DNode

13

ADS2 Lecture 5

DNode

14

ADS2 Lecture 5

DNode

15

ADS2 Lecture 5

DNode

16

ADS2 Lecture 5

DList

17

ADS2 Lecture 5

DList

18

ADS2 Lecture 5

DList

19

ADS2 Lecture 5

DList

20

ADS2 Lecture 5

DList

21

ADS2 Lecture 5

DList

22

ADS2 Lecture 5

DList

iteration refresher

Iteration

Iteration

Declare loop variable and initialise

Iteration

Continuing condition

Iteration

Action at end of loop on each iteration

Iteration

Iteration

Declare loop variable and initialise

Iteration

Continuing condition

Iteration

Action at end of loop on each iteration

Iteration

Iteration

Declare loop variable and initialise

Iteration

Continuing condition

Iteration

Action at end of loop on each iteration

36

ADS2 Lecture 5

DList

This is an example of really tight coding

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

Any comments?

37

ADS2 Lecture 5

DList

As tight as it gets?

end of iteration refresher

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

DList insert

DList insert

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

DList insert

DList insert

DList insert

DList insert

A1 B3 C2 first

A0

DList insert

A1 B3 C2 first

A0

DList insert

A1 B3 C2 first

A0

DList insert

A1 B3 C2 first

A0

DList insert

A1 B3 C2 first

A0

DList insert

DList insert

DList insert

A1 B3 C2 first last

D4

DList insert

A1 B3 C2first last

D4

DList insert

A1 B3 C2first last

D4

DList insert

A1 B3 C2first last

D4

DList insert

A1 B3 C2first last

D4

DList insert

DList insert

DList insert

Find insertion point

DList insert

Insert new node

DList insert

Insert new node

DList insert

A1 B3 C2 first

B0

x

y

z

DList insert

A1 B3 C2 first

B0

x

y

z

DList insert

A1 B3 C2 first

B0

x

y

z

DList insert

A1 B3 C2 first

B0

x

y

z

DList insert

A1 B3 C2 first

B0

x

y

z

DList insert

Done!

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

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!!!!

Random Fact #2

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

The dark side of Burroughs (William S.)

Random Fact #3

Random Fact #4

end of random facts #1 to #4

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

DList delete

But again, do you see the case analysis?

DList delete

DList delete

DList delete

Private … only used here, not by user

DList delete

DList delete

node

Ahead

last

DList delete

garbage

Ahead

last

DList delete

node

A B C first last

DList delete

node

A B C first last

DList delete

node

A B C first last

DList delete

garbage

A B C first last

DList delete

node

A B C first last

DList delete

node

A B C first last

DList delete

node

A B C first last

DList delete

garbage

A B C first last

DList delete

node

A B C first last

DList delete

node

A B C first last

DList delete

node

A B C first last

DList delete

garbage

A B C first last

DList Test

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

ADS2 Lecture 5 98

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

ADS2 Lecture 5 99

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

ADS2 Lecture 5 100

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

ADS2 Lecture 5 101

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

ADS2 Lecture 5 102

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

ADS2 Lecture 5 103

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

ADS2 Lecture 5 106

Comparing: array, linked list, doubly linked list

insert an item in orderdelete an itemget the ith item

“green” coding?

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

Could we recycle deleted nodes?

ADS2 Lecture 5 112

top related