Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3.

Similar presentations


Presentation on theme: "1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3."— Presentation transcript:

1 1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3

2 2 Dynamic data structures Data collections (e.g. stored library records) can vary considerably in size. Arrays not always best solution: –Inserting/deleting a new element requires much of array to be rewritten –Array size is fixed, must be estimated before use –If only few items held, much of array (hence memory) is wasted Solution: dynamic data structures (linked data structures) - dont need to know how many items to expect - can increase/decrease memory when items added/deleted

3 3 Examples Linked lists Trees –Binary trees –Binary search trees –AVL trees –B trees All use objects which are created dynamically, using memory from a special storage area (pool/heap). Will look at all of these

4 4 Singly Linked Lists (Goodrich § 3.2) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores –element –link to the next node (or null) First node in list referred to as head next elem node ABCD head

5 next node element For an easy introduction we will have a list of Strings The thing above is our Node

6 The Node Class for String objects next node element

7 The Node Class for String objects Sometimes called head next node element

8 The Node Class for String objects Sometimes called tail next node element

9 The Node Class for String objects next node element constructors

10 The Node Class for String objects next node element get

11 The Node Class for String objects next node element set

12 Over loading next node element

13 Dont like

14 better

15 The list

16

17 head size

18 head = null size = 0

19

20

21

22 if head == null return true; else return false; NO!

23

24 head size = 3 dogowlcat StringList L = L.addFront(pig) head size = 4 StringList L = dogowlcatpig

25 Inserting at the Head Allocate a new node

26 Inserting at the Head Allocate a new node

27 Inserting at the Head Allocate a new node New node points to old head

28 Inserting at the Head Allocate a new node New node points to old head Head points to new node

29 Inserting at the Head Allocate a new node New node points to old head Head points to new node Increase size counter

30 Removing at the Head 1.Update head to point to next node in the list

31 Removing at the Head 1.Update head to point to next node in the list 2.Allow garbage collector to reclaim the former first node

32 head size = 4 StringList L = dogowlcatpig head size = 3 StringList L = dogowlcatpig YIKES!

33 Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node -Would need to keep a record of which node is the last node and which node points to the last node (even then cant do it in constant time!)

34 Java code for a singly linked list of Strings In our implementation instance variables are head (reference to head node) and size (number of nodes currently in list) - Different implementations have different instance variables (some store the head and last/tail, some size (some not)). Will see later, depends what we actually want to do with our list. Our implementation is specific to strings. You will need to adapt it to accept values of other types - we will see a generic linked list later.

35 We have seen this

36 Simple Remove and Exception We have seen this But not this

37 The toString method Iterating over a list

38 The toString method Iterating over a list This is a pointer that traverses the list

39 The toString method Iterating over a list

40 The toString method Iterating over a list Whats happening here?

41 The toString method Iterating over a list Whats happening here? implicit cursor.toString()

42 The toString method Iterating over a list

43 The toString method Iterating over a list So we dont have a list (pig,dog,owl,cat,)

44 Iterating over a list … and this is a very nice template (pattern) for iterating over a linked list

45 45 Checking to see if a node containing a given value is in the list: Use a variable temp of type Node, initially pointing to (node pointed to by) head and progressively pointing to nodes along list until temp points to node containing value of interest, or temp points to null (i.e. end of list): > Searching for when Its easy whenyouknow how head temp return true Its easy whenyouknow how head > Searching for bucket temp return false ADS2 Lecture 4 Linear Search: isPresent (Alices slide)

46 Linear Search: isPresent

47 Assume we havent found what we are looking for

48 Linear Search: isPresent Our travelling cursor

49 Linear Search: isPresent Quit if we have hit end-of-list or we found what we are looking for

50 Linear Search: isPresent Quit if we have hit end-of-list or we found what we are looking for NOTE: we quit if X or Y is same as we continue if ¬X and ¬Y De Morgans law!!!!

51 Linear Search: isPresent Is this what we are looking for?

52 Linear Search: isPresent Move the cursor down the list

53 Linear Search: isPresent Did we find it?

54 Linear Search: isPresent note naming conventions (toString, isPresent, …) note that we do NOT do things like if (x > y) then b = true else b = false please … note, no assumptions about order in data how could we use order if we had it? note, I use cursor rather than temp (why?)

55 Linear Search: isPresent NOTE similarity

56 Linear Search: isPresent Is this better?

57 Linear Search: isPresent Is this better?

58 Linear Search: isPresent Is this better?

59 Linear Search: isPresent Is this better?

60 Linear Search: isPresent Is this better?

61 61 ADS2 Lecture 3

62 62 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old tail node point to new node 5. Update tail to point to new node Exercise for you: Start off with an empty list and add the following Strings, in the order given. This time insert at the tail each time. Starting from the head read off the Strings. how, know, you, when, easy, its Note, need to keep a record of the last (or tail) node. In Java do this via extra instance variable in list. See lecture 5. ADS2 Lecture 3


Download ppt "1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3."

Similar presentations


Ads by Google