1 / 23

Chapter 10 – part 2

Chapter 10 – part 2. Dynamic Data Structures. Linked Data Structures. head. "and". Linked Lists. head of the list is not a node. node in list. a Linked List consists of objects known as "nodes" each node has a data segment and a link to another node (links shown as arrows)

cheryl
Download Presentation

Chapter 10 – part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 – part 2 Dynamic Data Structures Linked Data Structures

  2. head "and" Linked Lists head of the list is not a node node in list • a Linked List consists of objects known as "nodes" • each node has a data segment and a link to another node (links shown as arrows) • each node is an object of a class that has two instance variables: • one for the data • could be more vars. • one for the link "Duey" data in a node "Cheatem" link in a node "Howe" Null link signifies "end of list" null

  3. "Duey" ListNode Class:Instance Variables and Constructor (page 629) public class ListNode { private String data; private ListNode link; public ListNode( String newData, ListNode linkValue) { data = newData; link = linkValue; } Two parameters for the constructor: • data value for the new node • Link value for the new node (link to other node)

  4. head pos "and" Stepping through a List(visiting the nodes) Excerpt from showList() in StringLinkedList Start at beginning of list ListNode position; pos = head; while (pos != null) //not end { ... pos = pos.getLink(); } "Duey" "Cheatem" this reference is returned by .getLink() statement moves pos to next node in list. "Howe" When position is at last node, pos.getLink() is null and the loop terminates. null

  5. Adding a Node to add a node at the beginning of the list: public void addANodeToStart(String addData) { // add new node pointing to head, repoint head // to point to this node head = new ListNode(addData, head); }// end of addANodeToStart() • new node points to the old head of the list: head • value of head is changed to point to the new node, which is now the new head of the list

  6. Deleting a Node to delete a node from the beginning of the list: public void deleteHeadNode() { if (head != null) // if list not empty head = head.getLink(); // point head to next else // list is empty // nothing to delete // perform relevant actions and return }// end of deleteHeadNode() • it will not try to delete if the list is empty • removes first element, sets head to point to the next node; first element has nothing pointing to it, so GC takes over • sometimes the deleted element needs to return its data, which changes the definition of deleteHeadNode()

  7. Null Pointer Exception on a Linked-List • NullPointerException occurs when attempt is made to access a member (var. or method) of an object and the object does not exist (the memory variable = null) • list nodes use null to indicate a link to no next node • NullPointerException should not an exception that is caught or declared • well-written code will never encounter this exception!!

  8. (page 631)Example: a Linked-List class StringLinkedList • StringLinkedList uses an linked-list structure (ListNode to store a sequence of strings • the linked list is private and identified by one inst. var: head • methods of StringLinkedList manipulate the linked-list • StringLinkedList()- constructor (set head to null) • length()- visits all nodes and counts visits • addANodeToStart(addData)- create and add new node to beginning of list • deleteHeadNode()- removes the first node, but does not remove from an empty list (but should the program crash?!) • onList(target)- true/false: is string target in the list? • find(target)- returns pointer to node matching string target • showList()- displays list data values to screen, uses same visitation algorithm as length()

  9. Defining the Node Classinside the List Class • placing the class ListNode within StringLinkedList encapsulated the definition where it is used (and does not require a separate class file) • also, making the inner class private makes it safer since it can never be modified without modifying the list class as well public class StringLinkedList { private ListNode head; <methods for StringLinkedList inserted here> private class ListNode { <Define ListNode instance variables and methods here> } } (page 631)

  10. Iterators – automatically stepping through a list • an object that allows stepping through a collection of objects, and perform some action on each is an: iterator • iteration – looping in defined steps • Recall: for arrays, the index/loop var. is used as an iterator; the loop body performs the action on each array element • in a linked list, the node reference is used as an iterator • StringLinkedListSelfContained has an instance variable current that "points" to the "current" node • the method goToNext() moves to the next node in the list by using the statement: current = current.next; // next node

  11. current previous head head current previous "and" "and" List After List Before goToNext() "Duey" "Duey" "Cheatem" "Cheatem" "Howe" "Howe" current.link null null current = current.link gives current a reference to the next node

  12. Iterator: other methods (page 650) • getDataAtCurrent()—returns the data part of the node that the iterator is pointing to (current) • moreToIterate()—returns a boolean value as to whether the iterator is at "end of list" (true if not at end) • resetIteration()—moves the iterator to the beginning of the list (head) • methods can included to add and delete nodes at the iterator position (see next slides) • in general, a "complete" linked-list allows for both addition and deletion at the head, middle, end of list • with a "backpointer" on each node, a "doubly-linked list" can visit nodes both forward and backward

  13. current head newNode newNode current head "and" "and" "Howe" null List After List Before Adding a NodeStep 1 "Duey" "Duey" "Cheatem" "Cheatem" "Howe" null null - create the node with reference newNode - add data to the node - set link: newNode.link = current.link

  14. List After head current newNode current head newNode null "and" "and" "Howe" "Howe" null null List Before "Duey" "Duey" "Cheatem" "Cheatem" - set link: current.link = newNode.link - detach new node: newNode = null - node successfully added to the list (data struct. is in order, only diagram is messy) Adding a NodeStep 2

  15. Adding a Node: Observation! • after creating the node, the statements that add the node to the list are: newNode.link = current.link; current.link = newNode; • what if these were reversed? effect? current.link = newNode; newNode.link = current.link; • when it comes to the algorithm steps in manipulating Linked-Lists: ORDER MATTERS! • and why is newnodeassignment to null afterwards?

  16. previous current current previous "and" "and" "Howe" null Deleting a NodeStep 1 List After List Before "Duey" "Duey" "Cheatem" "Cheatem" this node is to be deleted "Howe" null previous.link = current.link What should be done next?

  17. current previous current previous "and" "and" "Howe" "Howe" null null List After List Before Deleting a NodeStep 2 "Duey" "Duey" this node can not be visited from the head of the list. "Cheatem" "Cheatem" current = current.link The node has been deleted from the list although it is still shown in this picture.

  18. previous current toDelete "and" "Howe" null What happens to the deleted node? List Now "Duey" • Cheatem node has been deleted from the list • If there are no other references to the node, it's memory must be released • in C/C++, Pascal, Modula, the programmer is responsible for garbage collection • in Java, it automatically garbage collections any memory not being pointed to "Cheatem" null null (note: the programmer can still use atoDeletevariable topoint to the node todelete, then:toDelete = current; current = current.link; toDelete.link = null; // remove references toDelete = null;

  19. previous current "and" "Howe" null Node is Deleted! "Duey" • previous described deleting from the middle of the list • deleting the head just means moving the head pointer (and GC handles the lost head) • head = head.link; • delete the tail (last node) just means not referencing the last node: • visit the list until the second to last node points to null • set that node to link to null, and GC handles the node

  20. Exception Handling with Linked Lists see page 653 • LinkedListException Class • when the linked-list detects a problem, it can throw a specific exception: "end of list", "duplicate node", etc. • programmer using the linked-list can decided what to do based on the error • to help in creating iterators, use the Iterator interface • it uses exceptions to handle error-type situations • forces certain methods and conditions on your class • (see Appendix 7, page 912)

  21. Advanced List Structure: Doubly Linked-List • doubly linked-list allows visitation to occur in both directions on the list • all examples so far are single-direction • two "links" are required per node private class ListNode { private Object data; private ListNode next; private ListNode previous; • and that head and tail pointers are needed to maintain the list; yet, technically only one pointer is needed: current a data reference as class Object: any kind of data can be stored in a node** null null

  22. null null null null null null null Advanced List Structure: Tree • tree data structure • each node links to multiple other tree nodes • binary tree • each node leads to at most two other nodes • root—top node of tree (purpose same as head) • binary treeexample: root node

  23. Final Thoughts • Vectors can be thought of as arrays that can grow in length as needed during run time. • the base type of all vectors is Object • vector elements can be of any type, except primitives • linked list –a data structure consisting of objects (nodes), such that each node contains a data segment and reference (link) to another node in the list • all nodes are of the same node-type, meaning the data is of the same type (although Object can be used!) • for security, consider placing the node class inside the class of the general linked-list • iterator: a list that can automatically step through nodes

More Related