1 / 28

Linked Lists

Linked Lists. CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS. Project 2 due Wed, Feb 18 th , 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10 Get some paper out. Self-referential data types. class Node { private Object data; // the “data”

Télécharger la présentation

Linked Lists

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. Linked Lists CSC 172 SPRING 2004 LECTURE 6

  2. ANNOUNCEMENTS • Project 2 due Wed, Feb 18th, 5PM, CSB • Read Weiss Chapter 17 • Department T shirts available $10 • Get some paper out

  3. Self-referential data types class Node { private Object data; // the “data” private Node next; // the “link” }

  4. Linked List • A linked list “has-a” reference to a node • The “head” of the list • The ending node has “null” for the next head

  5. Basic Linked List

  6. Exercise 1 Implement a method public static void printList(Node front){} That traverses and prints the list

  7. public static void printList(Node front) { while (front != null) { System.out.println(front.toString()); front = front.next; } }

  8. public static void printList(Node front) { if (front == null) return; else { System.out.println(front.toString()); printList(front.next); } }

  9. Exercise 2 Implement a method public static void printList(Node front){} That traverses and prints the list in reverse

  10. public static void printList(Node front) { if (front == null) return; else { printList(front.next); System.out.println(front.toString()); } }

  11. Exercise 3 Implement a method public static Object lookup(Node front, Comparable searchobj){ // return null if not found }

  12. Adding a first node public void addFirst(Obj obj)

  13. Adding a first node public void addFirst(Object obj){ Link newLink = new Link(); newLink.data = obj; newLink.next = first; first = newLink; }

  14. Deleting a first node public Object removeFirst() {}

  15. Deleting a first node public Object removeFirst(){ if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; }

  16. Doubly Linked List Ex 4 : Write the “DoubleNode” class

  17. An empty Doubly Linked List How does this (the existence of head & tail) change traverse & print?

  18. Deletion Ex 6: Write a method to delete from doubly linked list assume comparable Objects public void delete(Comparable obj)

  19. public void delete (Comparable obj) { Node current = head; while (current != tail) { if (obj.equals(current.data)) { (current.next).prev = current.prev; (current.prev).next = current.next; return; } current = current.next; } }

  20. Insertion Ex 6: Write a method to insert from doubly linked list public void insertAfter(Node n, Comparable obj)

  21. public void insertAfter(Node n, Object obj){ Node newNode = new DoubleNode(); newNode.data = obj; newNode.prev = n; newNode.next = n.next; (newNode.prev).next = newNode; (newNode.next).prev = newNode; }

  22. A List Iterator

  23. Iterator Functionality

  24. Iterator next public Object next() { if (position == null){ position = first; return getFirst(); } else { if (position.next == null) throw new NoSuchElementException(); previous = position; // remember for remove position = position.next; return position.data; } }

  25. Adding to middle

  26. Adding to middle public void add(Object obj){ if (position == null) addFirst(obj); else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next; position.next = newLink; position = newLink; previous = null; } }

  27. Removing from middle

  28. Removing from middle public void remove(){ if (position == first) removeFirst(); else{ if (previous == null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; } }

More Related