Linked data structures - ch15 Flashcards
linked data structure
consists of capsules of data known as nodes that are connected via links
link
references
- memory address and is stored in a variable of a class type
- link is an instance variable of the node class type itself
nodes
objects of the node class
data in a node is stored as,,,
stored via instance variables
linked list
consist of single chain of nodes each connected to the next via a link - first node: head node, last node: end marker
how to traverse through a linked list
while (position != null) { count++ /count is an int initialized to 0/; position = position.getLink( ); return count; }
getLink( )
returns an instance of a class - someone can change the class? Privacy leak -- to fix this: make the node class private and inside linked list class -create node class All you need for a node class- (you don’t need accessor or mutator methods)
indicating the end of the linked list
link instance variable equal to null… == not .equals
adding a node
adds node to the start of the linked list - the new node is now the head node
deleting a head node
deleteHeadNode removes the first node from the linked list
what happens when using deleteHeadNode()
leaves the head var pointing to the old second node
the deletes one will automatically be collected and its memory recycled along with any other nodes that are no longer accessible - automatic garbage collection
node inner classes
have the node class be inner classes of the linked list class
generic linked list
can hold objects of any class type - including types that contains multiple instance variables
the copyOf method
The private helping method copyOf takes an argument that is a reference to a head node of a linked list, and returns a reference to the head node of a copy of that list
shallows and deep copy constructors, publicly cloneable , clone method, equals method
do examples - see notes