1 / 24

CS221

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 {

purity
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 4 – Monday CS221

  2. Last time • What did we talk about last time? • Linked list implementations • Generic linked lists • Iterators

  3. Questions?

  4. Project 1 Bitmap Manipulator

  5. JCF List

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

  7. List<E> interface

  8. 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:

  9. 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:

  10. Stacks Impromptu Student Lecture

  11. Stacks

  12. 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

  13. 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

  14. Implementations

  15. 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

  16. 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() {} }

  17. Array Constructor

  18. Array Push

  19. Array Pop

  20. Array Peek

  21. Array Size

  22. Upcoming

  23. Next time… • Finish stacks • Start queues

  24. Reminders • Keep reading Chapter 4 • Keep working on Project 1 • Due this Friday, September 19 by 11:59pm

More Related