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

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

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

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

6 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; }

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

8 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; }

9 A List Iterator

10 Iterator Functionality

11 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; }

12 Adding to middle

13 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; }

14 Removing from middle

15 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