Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 Lists/Green Coding ADS2 Lecture 5

2 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

3 Singly linked lists with head and last/tail nodes 3 Makes insertion from end easier Sounds like a great idea … Baltimore head Rome Seattle Toronto  last ADS2 Lecture 5

4 Adding a new node at the “last/tail” 4 Add node containing the string “Zurich” to tail of list head Rome Seattle Toronto  last Zurich create 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

5 Adding a new node at the “last/tail” Psuedo code snippet Node node = new Node(e,null) tail.setNext(node) tail = node size++

6 Singly linked lists with head and last/tail nodes 6 Makes insertion from end easier Sounds like a great idea … Baltimore head Rome Seattle Toronto  last ADS2 Lecture 5 But what happens if we delete the last node 

7 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 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 AB C  first  last

9 9 ADS2 Lecture 5 DNode

10 10 ADS2 Lecture 5 DNode

11 11 ADS2 Lecture 5 DNode

12 12 ADS2 Lecture 5 DNode

13 13 ADS2 Lecture 5 DNode

14 14 ADS2 Lecture 5 DNode

15 15 ADS2 Lecture 5 DNode

16 16 ADS2 Lecture 5 DList

17 17 ADS2 Lecture 5 DList

18 18 ADS2 Lecture 5 DList

19 19 ADS2 Lecture 5 DList

20 20 ADS2 Lecture 5 DList

21 21 ADS2 Lecture 5 DList

22 22 ADS2 Lecture 5 DList

23 iteration refresher

24 Iteration

25 Declare loop variable and initialise

26 Iteration Continuing condition

27 Iteration Action at end of loop on each iteration

28 Iteration

29 Declare loop variable and initialise

30 Iteration Continuing condition

31 Iteration Action at end of loop on each iteration

32 Iteration

33 Declare loop variable and initialise

34 Iteration Continuing condition

35 Iteration Action at end of loop on each iteration

36 36 ADS2 Lecture 5 DList This is an example of really tight coding Could we make it tighter? (maybe remove found?) Any comments?

37 37 ADS2 Lecture 5 DList As tight as it gets?

38 end of iteration refresher

39 39 ADS2 Lecture 5 Insertion into the middle of a doubly linked list insert here node d node a node b node c node anode 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

40 DList insert

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

42 DList insert

43

44

45 A1B3 C2  first  A0

46 DList insert A1B3 C2  first  A0

47 DList insert A1B3 C2  first A0

48 DList insert A1B3 C2  first  A0 

49 DList insert A1B3 C2  first  A0 

50 DList insert

51

52 A1B3 C2  first  last D4

53 DList insert A1B3 C2 first  last D4

54 DList insert A1B3 C2 first  last D4

55 DList insert A1B3 C2 first  last D4 

56 DList insert A1B3 C2 first  last D4 

57 DList insert

58

59 Find insertion point

60 DList insert Insert new node

61 DList insert Insert new node

62 DList insert A1B3 C2  first  B0 x y z

63 DList insert A1B3 C2  first  B0 x y z

64 DList insert A1B3 C2  first  B0 x y z

65 DList insert A1B3 C2  first  B0 x y z

66 DList insert A1B3 C2  first  B0 x y z

67 DList insert Done!

68

69 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” … Passports no longer have “Profession”  Random Fact #1 Computer Programmer

70 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” … Passports no longer have “Profession”  Random Fact #1 Still Learning!!!!

71 Random Fact #2 What computers looked like when I was a young man working at Burroughs

72 The dark side of Burroughs (William S.) Random Fact #3

73 Random Fact #4

74 end of random facts #1 to #4

75 ADS2 Lecture 575 Removal from the middle of a doubly linked list node anode 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

76 DList delete But again, do you see the case analysis?

77 DList delete

78

79 Private … only used here, not by user

80 DList delete

81 node A  head  last

82 DList delete garbage A  head  last

83 DList delete node AB C  first  last

84 DList delete node AB C  first  last

85 DList delete node AB C  first  last 

86 DList delete garbage AB C  first  last 

87 DList delete node AB C  first  last

88 DList delete node AB C  first  last

89 DList delete node AB C  first  last 

90 DList delete garbage AB C  first  last 

91 DList delete node AB C  first  last

92 DList delete node AB C  first  last

93 DList delete node AB C  first  last

94 DList delete garbage AB C  first  last

95 DList Test

96

97 Generic linked lists ADS2 Lecture 597 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

98 ADS2 Lecture 598 Java code for a node of a generic singly linked list: GList.java

99 ADS2 Lecture 599 Java code for a node of a generic singly linked list: GList.java

100 ADS2 Lecture 5100 Java code for a node of a generic singly linked list: GList.java

101 ADS2 Lecture 5101 Java code for a node of a generic singly linked list: GList.java

102 ADS2 Lecture 5102 Java code for a node of a generic singly linked list: GList.java

103 ADS2 Lecture 5103 Java code for a node of a generic singly linked list: GList.java

104

105

106 ADS2 Lecture 5106

107

108

109

110 Comparing: array, linked list, doubly linked list insert an item in order delete an item get the ith item

111 “green” coding? When we delete an element of the list it becomes garbage. Could we recycle deleted nodes?

112 ADS2 Lecture 5112


Download ppt "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."

Similar presentations


Ads by Google