Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists (continued). Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle Toronto  (1) Insert node containing (reference to.

Similar presentations


Presentation on theme: "Linked Lists (continued). Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle Toronto  (1) Insert node containing (reference to."— Presentation transcript:

1 Linked Lists (continued)

2 Maintaining Order

3 Creating an ordered list 3 Baltimore head Rome Seattle Toronto  (1) Insert node containing (reference to ) the string “Paris” Starting from head proceed until reach a node with string > ”Paris”. Then insert node containing “Paris” before this node. Need to insert node containing “Paris” between nodes containing “Baltimore” and “Rome” ADS2 Lecture 4

4 Creating an ordered list contd. 4 Baltimore head Rome Seattle Toronto  Paris Create node containing string “Paris” pointing to the “Rome” node. Baltimore head RomeSeattleToronto  Paris Make node “Baltimore” node point to “Paris” node. As we progress along the list, Need to keep a record of the previous node visited. ADS2 Lecture 4

5 5 (2) Go back to the original list. Insert node containing (reference to ) the string “Aberdeen” Starting from head proceed until reach a node with string>”Aberdeen”. Then insert node containing “Aberdeen” before this node. Need to insert node containing “Aberdeen” at the head of the list. No “previous node” in this case. Add to head as before. Creating an ordered list contd. (3) Go back to the original list. Insert node containing (reference to ) the string “Verona” Starting from head proceed until reach a node with string>”Verona”. But this doesn’t happen – we reach null. We can add to the end of the list without needing a “last” instance variable (see Lecture 3) as we are keeping copy of “previous node”. Baltimore head Rome Seattle Toronto  ADS2 Lecture 4

6 6 Baltimore head Rome Seattle Toronto  Verona Allocate new node containing “Verona”, pointing to null Creating an ordered list contd. Make previous node point to “Verona” node Baltimore head Rome Seattle Toronto  Verona So in general to insert a node containing string myString, starting from head proceed until reach null or a node with string>myString. Remember: Do not try to check the contents of a node if it is null. ADS2 Lecture 4

7 Creating an ordered list contd.

8

9

10

11

12

13

14

15

16

17

18

19

20 What do you think of this as a way of documenting code?

21 Know that we have order, how can isPresent exploit it?

22 Check it out

23

24

25

26 remove/delete

27 Removing all nodes containing a given value Baltimore head Rome Seattle Toronto  To remove all nodes containing string “Rome”: Start at head and proceed along list. If a node temp contains the String “Rome” - Redirect prev to point to the node pointed to by temp continue

28

29 pattern/template

30 Found what we are looking for?

31 The actual removal of a node

32 Removing head of list

33 The actual removal of a node Removing an arbitrary node

34 … otherwise, move on

35 … advance the cursor

36 dog cat owl doghen dog head s = “dog”

37 dog cat owl doghen dog head cursor prev s = “dog”

38 dog cat owl doghen dog head cursor prev s = “dog”

39 dog cat owl doghen dog head cursor prev s = “dog”

40 dog cat owl doghen dog head cursor prev s = “dog”

41 dog cat owl doghen dog head cursor prev s = “dog”

42 dog cat owl doghen dog head cursor prev s = “dog”

43 dog cat owl doghen dog head cursor prev s = “dog”

44 dog cat owl doghen dog head cursor prev s = “dog”

45 dog cat owl doghen dog head cursor prev s = “dog”

46 dog cat owl doghen dog head cursor prev s = “dog”

47 dog cat owl doghen dog head cursor prev s = “dog”

48 dog cat owl doghen dog head cursor prev s = “dog”

49 dog cat owl doghen dog head cursor prev s = “dog”

50 dog cat owl doghen dog head cursor prev s = “dog”

51 dog cat owl doghen dog head cursor prev s = “dog”

52 dog cat owl doghen dog head cursor prev s = “dog”

53 dog cat owl doghen dog head cursor prev s = “dog”

54 dog cat owl doghen dog head cursor prev s = “dog”

55 dog cat owl doghen dog head cursor prev s = “dog”

56 dog cat owl doghen dog head cursor prev s = “dog”

57 Know that we have order, how can isPresent exploit it? Below, linear search on a sorted array of strings. Note early stopping condition. Can we incorporate this into isPresent above for linked lists?

58


Download ppt "Linked Lists (continued). Maintaining Order Creating an ordered list 3 Baltimore head Rome Seattle Toronto  (1) Insert node containing (reference to."

Similar presentations


Ads by Google