Lecture 5 maybe Linked Lists continued Copying and
Lecture 5 (maybe) Linked Lists (continued)) • Copying and sorting singly linked lists • Lists with head and last nodes • Doubly linked lists • Append/Circular Lists/Green Coding ADS 2 Lecture 5 1
Sorting a singly linked list Two options (of some) - keep it sorted (insert in order) - we could use a bubble sort with 2 cursors head null
Singly linked lists with head and last/tail nodes Makes insertion from end easier Sounds like a great idea … head Baltimore Rome Seattle Toronto ADS 2 Lecture 5 last 3
Adding a new node at the “last/tail” Add node containing the string “Zurich” to tail of list head Rome Zurich Toronto Seattle create new node containing (reference to) string “Zurich”, with next =null last Zurich head Rome Seattle Toronto ADS 2 Lecture 5 Redirect last. next to new node Reallocate last to new node 4
Adding a new node at the “last/tail” Psuedo code snippet Node node = new Node(e, null) tail. set. Next(node) tail = node size++
Singly linked lists with head and last/tail nodes Makes insertion from end easier Sounds like a great idea … But what happens if we delete the last node head Baltimore Rome Seattle Toronto ADS 2 Lecture 5 last 6
Doubly linked lists • 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. ADS 2 Lecture 5 7
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 first A prev next elem B ADS 2 Lecture 5 C node last 8
DNode 9 ADS 2 Lecture 5
DNode 10 ADS 2 Lecture 5
DNode 11 ADS 2 Lecture 5
DNode 12 ADS 2 Lecture 5
DNode 13 ADS 2 Lecture 5
DNode 14 ADS 2 Lecture 5
DNode 15 ADS 2 Lecture 5
DList 16 ADS 2 Lecture 5
DList 17 ADS 2 Lecture 5
DList 18 ADS 2 Lecture 5
DList 19 ADS 2 Lecture 5
DList 20 ADS 2 Lecture 5
DList 21 ADS 2 Lecture 5
DList 22 ADS 2 Lecture 5
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
DList This is an example of really tight coding Could we make it tighter? (maybe remove found? ) Any comments? 36 ADS 2 Lecture 5
DList As tight as it gets? 37 ADS 2 Lecture 5
end of iteration refresher
Insertion into the middle of a doubly linked list node d insert here node a 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 node a node d node b ADS 2 Lecture 5 node c 39
DList insert
DList insert NOTE: case analysis. This can make coding a bit less complicated
DList insert
DList insert
DList insert
DList insert first A 1 A 0 B 3 C 2
DList insert first A 1 A 0 B 3 C 2
DList insert first A 1 A 0 B 3 C 2
DList insert first A 1 A 0 B 3 C 2
DList insert first A 1 A 0 B 3 C 2
DList insert
DList insert
DList insert first A 1 C 2 B 3 D 4 last
DList insert first A 1 C 2 B 3 D 4 last
DList insert first A 1 C 2 B 3 D 4 last
DList insert first A 1 C 2 B 3 D 4 last
DList insert first A 1 C 2 B 3 D 4 last
DList insert
DList insert
DList insert Find insertion point
DList insert Insert new node
DList insert Insert new node
DList insert first x z A 1 B 3 B 0 y C 2
DList insert first x z A 1 B 3 B 0 y C 2
DList insert first x z A 1 B 3 B 0 y C 2
DList insert first x z A 1 B 3 B 0 y C 2
DList insert first x z A 1 B 3 B 0 y C 2
DList insert Done!
Random Fact #1 Irrelevant fact … my 1 st passport was paid for by Burroughs Machines Corporation. It came with an indefinite US visa and had as my “Profession” … Computer Programmer Passports no longer have “Profession”
Random Fact #1 Irrelevant fact … my 1 st passport was paid for by Burroughs Machines Corporation. It came with an indefinite US visa and had as my “Profession” … Still Learning!!!! Passports no longer have “Profession”
Random Fact #2 What computers looked like when I was a young man working at Burroughs
Random Fact #3 The dark side of Burroughs (William S. )
Random Fact #4
end of random facts #1 to #4
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 ADS 2 Lecture 5 75
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 head A node last
DList delete head A garbage last
DList delete first A node B C last
DList delete first A node B C last
DList delete first A node B C last
DList delete first A garbage B C last
DList delete first A B C node last
DList delete first A B C node last
DList delete first A B C node last
DList delete first A B C garbage last
DList delete first A B node C last
DList delete first A B node C last
DList delete first A B node C last
DList delete first A B garbage C last
DList Test
Generic linked lists • 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 ADS 2 Lecture 5 97
Java code for a node of a generic singly linked list: GList. java ADS 2 Lecture 5 98
Java code for a node of a generic singly linked list: GList. java ADS 2 Lecture 5 99
Java code for a node of a generic singly linked list: GList. java ADS 2 Lecture 5 100
Java code for a node of a generic singly linked list: GList. java ADS 2 Lecture 5 101
Java code for a node of a generic singly linked list: GList. java ADS 2 Lecture 5 102
Java code for a node of a generic singly linked list: GList. java ADS 2 Lecture 5 103
ADS 2 Lecture 5 106
Comparing: array, linked list, doubly linked list insert an item in order delete an item get the ith item
“green” coding? When we delete an element of the list it becomes garbage. Could we recycle deleted nodes?
ADS 2 Lecture 5 112
- Slides: 112