1 / 8

CSC 205 Programming II

CSC 205 Programming II. Linked List – Other Variations. The First Node – a special case. Add to or delete from index = 1 is special You don’t have to find first!. Node newNode = new Node(nObj, head); head = newNode;. obj1. head. nObj. inserted item. obj1. obj2. head.

nevaeh
Download Presentation

CSC 205 Programming II

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. CSC 205 Programming II Linked List – Other Variations

  2. The First Node – a special case • Add to or delete from index = 1 is special • You don’t have to find first! Node newNode = new Node(nObj, head); head = newNode; obj1 head nObj inserted item obj1 obj2 head deleted item head = head.getNext();

  3. Write a Linked List Backward • Two recursive strategies • writeListBackward: • Write the last node • Write the list minus its last node backward • writeListBackward2: • Write the list minus its first node backward • Write the first node obj1 obj2 obj1 obj2 head item next null head item next null writeListBackward writeListBackward2

  4. Which One Is More Efficient? • writeListBackward2! • There is no efficient ways to get the last node • There is no efficient ways to get the list minus the last node void writeListBackward2(Node nextNode) { if (nextNode != null) { writeListBackward2(nextNode.getNext()); //write the first node System.out.println(nextNode.getItem()); } }

  5. Variations • The normal version • (singly) linked list (with a head reference) • Other variations • Circular linked list • Doubly linked list with both head and tail references • Circular doubly linked list

  6. Circular Linked List • The last node has its next referencing the first node (head) in the list … obj1 obj2 obj3 objx head //display contents in a circular linked list if (head != null) { Node curr = head; do { System.out.println(curr.getItem()); curr = curr.getNext(); } while (curr != head) }

  7. Doubly Linked List • A node has a reference to its successor and one to the node precedes it • Node(Object item, Node next, Node precede) … obj1 obj2 objx head

  8. Circular Doubly Linked List • The one implemented in the java.util package … obj1 obj2 objx head

More Related