linked lists
play

Linked lists Insert Delete Lookup Doubly-linked lists Lecture 6: - PowerPoint PPT Presentation

Linked lists Insert Delete Lookup Doubly-linked lists Lecture 6: Linked Lists Object References When you declare a variable of a non-primitive type you are really declaring a reference to that object String foo; Complex c; new


  1. Linked lists Insert Delete Lookup Doubly-linked lists Lecture 6: Linked Lists

  2. Object References � When you declare a variable of a non-primitive type you are really declaring a reference to that object � String foo; � Complex c; � new actually creates the object � String foo = new String(“Hello”); � Complex c = new Complex(5, 2); � Multiple references can refer to the same object � String bar = foo; � References can be reassigned � foo = new String(“Goodbye”); � null is a special reference that refers to nothing Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 2

  3. Growing an Array � See List2 code Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 3

  4. Common operation: keep a list of items � It’s extremely common in programs to want to store a collection of items � Example: array � Collection of items of the same type � Easy to access elements of the array, but… � Size set at array creation time � Size (or max size) must be known at creation time � Difficult to make the array larger � Making the array smaller wastes space � Sorting requires actually moving the data around � Insert and delete also require copying the data Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 4

  5. Abstract data type: list � Common ADT: list (of objects) � List supports several operations � Insert � Delete � Lookup � Index � Length � Implementation of a list may vary � Array can be used to implement a list � Index & length are fast � Insert can be very slow (and waste memory) � Alternative: linked list Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 5

  6. Linked Lists � Variable length list data structure � Each list element contains � Its data � A reference to the next element of the list � NULL if it is the last element of the list � Head or Start points to the first element of the list � Inserting an element � Previous element refers to new element, new element refers to the one the previous element used to refer to � Removing an element � Previous element refers to whatever the removed element used to refer to Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 6

  7. Linked lists versus arrays Start � Array: objects occupy 0 2 contiguous memory 1 3 2 � Linked list: objects need not 5 3 be contiguous � Each object refers to others in 4 the list 4 5 � Singly-linked list: each object 0 has a reference to the next one in the list � Doubly-linked list: each 1 object has a reference to both the previous and next ones in the list Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 7

  8. So how is this done in Java? � Java: reference is a name pub l i c c l ass SLL i s t{ for an object SLLNode s ta r t ; � One object can have multiple i n tcoun t ; names } � Changes in the object seen through all names � Two types make up a list c lass SLLNode { SLLNode nex t ; � Header type: used for the list itself Ob jec t ob j ; � Node type: used for elements } in the list � Object being stored: may store reference or new object � Reference to other nodes Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 8

  9. Why use Object in the list? � Lists contain values � Strings � Numbers � More complex data structures � We want to build a list that’ll work with anything! � Write the code once and reuse it! � All types in Java except builtins are descended from Object � Builtins like int must use provided classes like Integer � List can now be used for anything! Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 9

  10. Definitions for SLList and SLLNode pub l i c c l ass SLL i s t{ p r i va te SLLNode s ta r t ; p r i va te i n tcoun t ; pub l i c vo id add ( i n ti ndex , Ob jec t i t em) t h rows A r ray IndexOutOfBoundsExcep t i on ; pub l i c vo id remove ( i n ti ndex ) t h rows A r ray IndexOu tOfBoundsExcep t i on ; pub l i c Ob jec t ge t ( i n ti ndex ) t h rows A r ray IndexOu tOfBoundsExcep t i on ; p r i va te SLLNode ge t I ndex ( i n ti ndex ) t h rows A r ray IndexOutO fBoundsExcep t i on ; pub l i c vo id removeA l l ( ) ; p r i va te c l ass SLLNode { SLLNode nex t ; Ob jec t ob j ; } } Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 10

  11. Inserting into a singly linked list � Inserting into a link list has head two cases � First in the list 2 � Not first in the list � If going at head, modify 1 next head reference (only) next � If going elsewhere, need reference to node before insertion point 5 � New node.next = cur node.next next � Cur node.next = ref to new 9 node � Must be done in this order! next Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 11

  12. Deleting from a singly linked list � Deleting a link list has two head cases � First in the list 2 � Not first in the list � If deleting from head, 1 next modify head reference next (only) � If deleting elsewhere, simply “point around” the 5 deleted node next � Space for deleted nodes is automatically reclaimed 9 (garbage collection) next Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 12

  13. Traversing a singly linked list � Start at the head head � Use a “current” reference for the current node 2 � Use the “next” reference to find the next node in the list 1 next � Repeat this to find the next desired node � N times to find the n th node � Until the object matches if Node 2 5 looking for a particular object � Caution: objects can next “match” even if the references aren’t the same… 9 � Don’t forget to check to see next if this is the last node Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 13

  14. More on traversing singly linked lists � Check for “end of list” before moving on to the next element! � Trying to dereference a null reference generates an exception � Java (and C) ignore statements in logical operations if they’re not “needed” � Example while (n != null && n.obj != whatWeWant) { n = n.next } � If n is null , the second half of the expression is not evaluated � Be careful with object comparisons! � String s = “hello”; � String t = “hello”; � At this point, s is not equal to t: (s == t) results in false � Reason: s and t refer to different objects with the same content � Solution: use String.equals or similar method to compare object contents rather than references Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 14

  15. Doubly-linked lists Each node in the list refers to its � head predecessor as well as successor tail � Twice as many references � Easier to insert / delete nodes � List can be traversed in either 2 direction List typically has both head and � prev tail references next Insertion only needs a reference � to an adjacent node Deletion only needs a reference � 5 to the node being deleted prev next Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 15

  16. Inserting into a doubly-linked list As with singly linked lists, � head special case for head tail � Also special case for tail Need to update two nodes � � Node before new node 2 � Node after new node Hook up new node before prev � modifying other nodes next � Don’t overwrite necessary information before relocating it! 4 Head & tail: if a link is null, � 5 update the head or tail as prev prev appropriate next next Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 16

  17. Deleting from a doubly-linked list As with singly linked lists, � head special case for head tail � Also special case for tail Need to update two nodes � � Node before new node 2 � Node after new node Hook up new node before prev � modifying other nodes next � Don’t overwrite necessary information before relocating it! 4 Head & tail: if a link is null, � 5 update the head or tail as prev prev appropriate next next Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 17

  18. Summary of list operations � Insert objects � May insert at any position � Special methods to insert at head or tail? � Delete objects � Delete by index � Delete by content � Find objects � Find by index number � Find by (incomplete?) content � Find the length of the list Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 18

  19. Other � Let’s write a linked list class � Tail reference � Circular linked list � Circular doubly linked list � Dummy head node Lecture 6: Linked Lists CMPS 12B, UC Santa Cruz 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend