240 likes | 406 Views
Week 4 – Monday. CS221. Last time. What did we talk about last time? Linked list implementations Generic linked lists Iterators. Questions?. Project 1. Bitmap Manipulator. JCF List. JCF L ist usage. import java.util .*; public class Testing {
E N D
Week 4 – Monday CS221
Last time • What did we talk about last time? • Linked list implementations • Generic linked lists • Iterators
Project 1 Bitmap Manipulator
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 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:
Stacks Impromptu Student Lecture
Stack • A stack is a simple (but useful) data structure that has three basic operations: • Push Put an item on the top of the stack • Pop Remove an item from the top of the stack • Top Return the item currently on the top of the stack (sometimes called peek) • This kind of data structure is sometimes referred to as an Abstract Data Type (ADT) • We don't actually care how the ADT works, as long as it supports certain basic operations
Keeping track of things • When are stacks used? • Implicitly, in recursion (or in any function calls) • Explicitly, when turning recursive solutions into iterative solutions • When parsing programming languages • When converting infix to postfix
Implementations • Array • Advantages: pop is O(1), top is O(1) • Disadvantages: push is O(n) in the very worst case, but not in the amortized case • Linked list • Advantages: push is O(1), pop is O(1), top is O(1) • Disadvantages: slightly slower than the array version, considerably more memory overhead
Array implementation public classArrayStack { private int[] data; private int size; public ArrayStack() {} public void push(int value) {} public intpop() {} public int peek() {} //instead of top public int size() {} }
Next time… • Finish stacks • Start queues
Reminders • Keep reading Chapter 4 • Keep working on Project 1 • Due this Friday, September 19 by 11:59pm