Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 Programming II Lecture 15 Linked List – Other Variations.

Similar presentations


Presentation on theme: "CSC 205 Programming II Lecture 15 Linked List – Other Variations."— Presentation transcript:

1 CSC 205 Programming II Lecture 15 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! head obj1 nObj inserted item obj1obj2 deleted item head Node newNode = new Node(nObj, head); head = newNode; 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 next obj2 null obj2 null obj1 nextheaditem writeListBackwardwriteListBackward2 head

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 … obj1obj2obj3objx 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) … obj1obj2objx head

8 Circular Doubly Linked List … obj1obj2objx head The one implemented in the java.util package


Download ppt "CSC 205 Programming II Lecture 15 Linked List – Other Variations."

Similar presentations


Ads by Google