250 likes | 379 Views
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
E N D
Week 3 - Friday CS221
Last time • What did we talk about last time? • Linked list implementations • Skip, circular, and self-organizing lists
Project 1 Bitmap Manipulator
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
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?
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; … }
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; … }
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)
hasNext() Iterator Method
next() Iterator Method
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); } }
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:
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:
Next time… • Stacks
Reminders • Read Chapter 4 • Finish Assignment 2 • Due tonight by 11:59pm • Keep working on Project 1 • Due Friday, September 19 by 11:59pm