1 / 25

CS221

Week 3 - Friday. CS221. Last time. What did we talk about last time? Linked list implementations Skip, circular, and self-organizing lists. Questions?. Project 1. Bitmap Manipulator. Eclipse Debugger Tutorial. My programming philosophies. Keep it simple

symona
Download Presentation

CS221

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. Week 3 - Friday CS221

  2. Last time • What did we talk about last time? • Linked list implementations • Skip, circular, and self-organizing lists

  3. Questions?

  4. Project 1 Bitmap Manipulator

  5. Eclipse Debugger Tutorial

  6. My programming philosophies • Keep it simple • Never write a line of code you don’t fully understand • Draw pictures (especially when dealing with crazy references) • Run through a small example with pen and paper to test your assumptions • Don't create new objects unnecessarily

  7. Interview question • You are given a reference to a node in a singly linked list and some data • You need to insert the data into the node in front of the node you are given a reference to • You do not have the head reference, you do not have a reference to the previous node • How do you do the insertion?

  8. Generic linked lists

  9. Definition • Let’s try a simple definition for a linked list: public classLinkedList<T>{ private static class Node<T> { public T data; public Node<T> next; } private Node<T> head = null; … }

  10. Insert at head

  11. Delete from head

  12. Index Of

  13. Insert in order

  14. Definition with an iterator public classLinkedList<T> implementsIterable<T>{ private static class Node<T> { public T data; public Node<T> next; } private classListIterator implements Iterator<T> { … } private Node<T> head = null; … }

  15. Iterator<T> interface • The Iterator<T> interface has three methods: • booleanhasNext() • Returns true if the iteration has more elements • T next() • Returns the next element in the iteration • void remove() • Removes the last element returned by this iterator (optional)

  16. hasNext() Iterator Method

  17. next() Iterator Method

  18. JCF List

  19. JCF List usage import java.util.*; public class Testing { public static void main(String[] args) { List<Integer> numbers = new LinkedList<Integer>(); numbers.add( 6 ); numbers.add( 9 ); numbers.add( -4 ); for( int i : numbers ) System.out.println(i); } }

  20. List<E> interface

  21. ArrayList • Implementation of List interface backed by a dynamic array • Good for random and sequential access • Reasonably good for adds at to the end • Bad for lots of insertions and deletions • Adds these methods:

  22. LinkedList • Implementation of List interface backed by a doubly-linked list • Similar to your NewList implementation (except doubly-linked) • Good for sequential access, insertions, and deletions • Bad for random access • More memory overhead than ArrayList • Adds these methods:

  23. Upcoming

  24. Next time… • Stacks

  25. Reminders • Read Chapter 4 • Finish Assignment 2 • Due tonight by 11:59pm • Keep working on Project 1 • Due Friday, September 19 by 11:59pm

More Related