Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Linked Lists CSC 172 SPRING 2004 LECTURE 6

2 ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 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” } datanext

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 datanext dataNext null 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 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 public void remove(){ if (position == first) removeFirst(); else{ if (previous == null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; }


Download ppt "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."

Similar presentations


Ads by Google