2Dynamic 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 rewrittenArray size is fixed, must be estimated before useIf only few items held, much of array (hence memory) is wastedSolution: dynamic data structures (linked data structures)- don’t need to know how many items to expect- can increase/decrease memory when items added/deleted
3Examples Linked lists Trees Binary trees Binary search trees AVL trees B treesWill look at all of theseAll use objects which are created dynamically, using memory from a special storage area (pool/heap).
4Singly Linked Lists (Goodrich § 3.2) A singly linked list is a concrete data structure consisting of a sequence of nodesEach node storeselementlink to the next node (or null)First node in list referred to as headnextelemnodeABCDhead4
5next node For an easy introduction we will have a list of Strings elementThe thing above is our Node
6The Node Class for String objects nextnodeelement
7The Node Class for String objects Sometimes called “head”nextnodeelement
8The Node Class for String objects Sometimes called “tail”nextnodeelement
9The Node Class for String objects constructorsnextnodeelement
10The Node Class for String objects getnextnodeelement
11The Node Class for String objects setnextnodeelement
27Inserting at the HeadAllocate a new nodeNew node points to old head
28Inserting at the Head Allocate a new node New node points to old head Head points to new node
29Inserting at the Head Allocate a new node New node points to old head Head points to new nodeIncrease size counter
30Removing at the HeadUpdate head to point to next node in the list
31Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node
32head StringList L = size = 4 head StringList L = size = 3 YIKES! dog owlcatpigheadsize = 3StringList L =dogowlcatpigYIKES!
33Removing at the TailRemoving 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 can’t do it in constant time!)
34Java 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.
43The toString methodSo we don’t have a list(pig,dog,owl,cat,)Iterating over a list
44Iterating over a list… and this is a very nice template (pattern) for iterating over a linked listIterating over a list
45Linear Search: isPresent 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”return truetemptemptempIt’seasywhenyouknowhowhead> Searching for “bucket”temptemptemptemptemptemptempIt’seasywhenyouknowhowheadADS2 Lecture 4return false45(Alice’s slide)
54Linear Search: isPresent note naming conventions (toString, isPresent, …)note that we do NOT do things likeif (x > y) then b = true else b = falseplease …note, no assumptions about order in datahow could we use order if we had it?note, I use “cursor” rather than “temp” (why?)
62Inserting at the Tail Allocate a new node Insert new element 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.Allocate a new nodeInsert new elementHave new node point to nullHave old tail node point to new nodeUpdate tail to point to new nodeExercise 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”, “it’s”ADS2 Lecture 3