Top Ten Words that Almost Rhyme with “Peas”

Slides:



Advertisements
Similar presentations
Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
Advertisements

Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Topic 11 Linked Lists -Joel Spolsky
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Stacks, Queues, and Deques
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Lists Based on content from: Java Foundations, 3rd Edition.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Information and Computer Sciences University of Hawaii, Manoa
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Linked Data Structures
Elementary Data Structures
Review Array Array Elements Accessing array elements
The List ADT.
Week 4 - Monday CS221.
Data Structure By Amee Trivedi.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Marcus Biel, Software Craftsman
Week 4 - Friday CS221.
Big-O notation Linked lists
Chapter 5 Ordered List.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Queues Queues Queues.
Programming Abstractions
Week 15 – Monday CS221.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
structures and their relationships." - Linus Torvalds
Topic 11 Linked Lists -Joel Spolsky
Chapter 5 Ordered List.
Arrays and Linked Lists
Java Collections Framework
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Linked Lists [AJ 15].
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Introduction to Data Structure
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
structures and their relationships." - Linus Torvalds
Topic 11 Linked Lists -Joel Spolsky
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Top Ten Words that Almost Rhyme with “Peas” Lists Top Ten Words that Almost Rhyme with “Peas” 10. heats 5. lens 9. rice 4. ice 8. moss 3. nurse 7. ties 2. leaks 6. needs 1. meats

CS 221 - Computer Science II Lists A list is a linear collection, like stacks and queues, but is more flexible. Adding and removing elements in lists can occur at either end or anywhere in the middle. We will examine three types of list collections: Ordered lists. Unordered lists. Indexed lists. CS 221 - Computer Science II

CS 221 - Computer Science II Sorted Lists The elements in a sorted list are ordered by some inherent characteristic of the elements: Names in alphabetical order. Scores in ascending order. The elements themselves determine where they are stored in the list. CS 221 - Computer Science II

CS 221 - Computer Science II Sorted Lists An sorted list: CS 221 - Computer Science II

CS 221 - Computer Science II Unsorted Lists There is an order to the elements in an unsorted list, but that order is not based on element characteristics. The user of the list determines the order of the elements. A new element can be put on the front or the rear of the list, or it can be inserted after a particular element already in the list. CS 221 - Computer Science II

CS 221 - Computer Science II Unsorted Lists An unsorted list: CS 221 - Computer Science II

CS 221 - Computer Science II Indexed Lists In an indexed list, elements are referenced by their numeric position in the list. Like an unsorted list, there is no inherent relationship among the elements. The user can determine the order. Every time the list changes, the indexes are updated. CS 221 - Computer Science II

CS 221 - Computer Science II Indexed Lists An indexed list: CS 221 - Computer Science II

CS 221 - Computer Science II A List ADT public interface IList<T> extends Iterable<T> { void add(T element); void insert(int pos, T element); T set(int pos, T element); T get(int pos); T remove(int pos); void remove(T element); int size(); int indexOf(T element); void makeEmpty(); public String toString(); Iterator<T> iterator(); } CS 221 - Computer Science II

Implementing a List with an Array An array is an obvious choice for implementing a list collection. Easy to create, direct indexing. Will need to keep track of capacity of array, as well as number of elements in list. How construct new array, if want to use generics? What type of array create? CS 221 - Computer Science II

An Array Implementation of List ADT public class MyList<T> implements IList<T> { private int count; private int capacity; private T[] list; private static final int DEFAULT_CAPACITY = 10; public MyList() setCount(0); setCapacity(DEFAULT_CAPACITY); list = ???; } … CS 221 - Computer Science II

Creating MyList Object // Using implementation in code MyList<String> list = new MyList<String>(); MyList capacity count DEFAULT_CAPACITY list 10 - - - - - - - - - - CS 221 - Computer Science II

CS 221 - Computer Science II add method public void add(T element) Add to the end of list. What if array is full? Any shifting? CS 221 - Computer Science II

CS 221 - Computer Science II insert method public void insert(int pos, T element) Add at position in list. Exceptional situations? What if full? Where start shifting? CS 221 - Computer Science II

CS 221 - Computer Science II set method public void set(int index, T element) Replace element at index with given element. Exceptional situations? Shifting? CS 221 - Computer Science II

CS 221 - Computer Science II get method public T get(int index) Return element at given index. Exceptional situations? CS 221 - Computer Science II

CS 221 - Computer Science II remove method public T remove(int index) Remove element at given index. Exceptional situations? Where start shifting? CS 221 - Computer Science II

Implementing a List with Links A single-linked list is also a good choice for implementing a list collection. Will need to implement Node class. Maintain both head and tail references, as well as number of elements in list. CS 221 - Computer Science II

CS 221 - Computer Science II Question 1 What is output by the following code? Integer i1 = new Integer(12); Integer i2 = new Integer(12); System.out.println( i1 == i2 ); No output due to syntax error No output due to runtime error false true CS 221 - Computer Science II

CS 221 - Computer Science II Question 1 What is output by the following code? Integer i1 = new Integer(12); Integer i2 = new Integer(12); System.out.println( i1 == i2 ); No output due to syntax error No output due to runtime error false true CS 221 - Computer Science II

CS 221 - Computer Science II Object References Recall, a variable is an object reference, the address of an object. A reference can also be called a pointer. They are often depicted graphically: student John Smith 40725 3.57 CS 221 - Computer Science II

CS 221 - Computer Science II Creating References The keyword new creates a new object, but also returns a reference to that object. For example, Integer i1 = new Integer(12); new Integer(12)creates and Integer object and returns a reference to it. Can assign this reference to a variable like i1, or use it in other ways. CS 221 - Computer Science II

A Single-Linked List Implementation of List ADT public class MyList<T> implements IList<T> { private Node<T> head; private Node<T> tail; private int count; public MyList() head = null; tail = null; setCount(0); } … CS 221 - Computer Science II

A Single-Linked List Node Class public class Node<T> { private T element; private Node<T> next; public Node() { setElement(null); setNext(null); } public Node(T element) { setElement(element); setNext(null); public T getElement() { return element; } public Node<T> getNext() { return next; } public void setElement(T element) { this.element = element; } public void setNext(Node<T> next) { this.next = next; } } CS 221 - Computer Science II

Creating MyList Object // Using implementation in code MyList<String> list = new MyList<String>(); MyList head count tail null null CS 221 - Computer Science II

CS 221 - Computer Science II Writing Methods When trying to code methods for linked lists draw pictures! If you don't draw pictures of what you are trying to do, it is very easy to make mistakes! CS 221 - Computer Science II

CS 221 - Computer Science II add method public void add(T element) Add to the end of list Special case if empty Steps on following slides CS 221 - Computer Science II

add Element - List Empty (Before) head tail count null null String element CS 221 - Computer Science II

add Element - List Empty (After) head tail count 1 Node element next null String element CS 221 - Computer Science II

add Element - List Not Empty (Before) head tail count 1 Node element next null String String element CS 221 - Computer Science II

add Element - List Not Empty (After) head tail count 2 Node element next Node element next null String String element CS 221 - Computer Science II

CS 221 - Computer Science II Question 2 What is the worst case run-time to add at the end of an array-based list with n items? A linked-list implementation? Array Linked List O(1) O(1) O(n) O(n) O(log n) O(1) O(1) O(n) O(n) O(1) CS 221 - Computer Science II

CS 221 - Computer Science II Question 2 What is the worst case run-time to add at the end of an array-based list with n items? A linked-list implementation? Array Linked List O(1) - if space O(1) - with tail O(n) - if no space O(n) - with no tail O(log n) O(1) O(1) - if space O(n) - with no tail O(n) - if no space O(1) - with tail CS 221 - Computer Science II

CS 221 - Computer Science II insert method public void insert(int pos, T element) Add at position in list Special case if empty CS 221 - Computer Science II

CS 221 - Computer Science II Question 3 What is the run-time to insert for an array-based list with n items? For a linked-list implementation? Array Linked List O(1) O(1) O(n) O(1) O(log n) O(1) O(1) O(n) O(n) O(n) CS 221 - Computer Science II

CS 221 - Computer Science II Question 3 What is the run-time to insert for an array-based list with n items? For a linked-list implementation? Array Linked List O(1) O(1) O(n) O(1) O(log n) O(1) O(1) O(n) O(n) O(n) CS 221 - Computer Science II

CS 221 - Computer Science II set method public void set(int index, T element) Must be careful not to break the chain! Where do we need to stop? Special cases? CS 221 - Computer Science II

CS 221 - Computer Science II Question 4 What is the run-time to set an element for an array-based list? To a linked-list implementation? Array Linked List O(n) O(n) O(n) O(1) O(log n) O(1) O(log n) O(log n) O(1) O(n) CS 221 - Computer Science II

CS 221 - Computer Science II Question 4 What is the run-time to set an element for an array-based list? To a linked-list implementation? Array Linked List O(n) O(n) O(n) O(1) O(log n) O(1) O(log n) O(log n) O(1) O(n) CS 221 - Computer Science II

CS 221 - Computer Science II Code for get public T get(int index) The downside of linked lists CS 221 - Computer Science II

CS 221 - Computer Science II Question 5 What is the run-time to find an element based on position in an array-based list? A linked-list implementation? Array Linked List O(1) O(n) O(n) O(1) O(log n) O(1) O(log n) O(n) O(n) O(n) CS 221 - Computer Science II

CS 221 - Computer Science II Question 5 What is the run-time to find an element based on position in an array-based list? A linked-list implementation? Array Linked List O(1) O(n) O(n) O(1) O(log n) O(1) O(log n) O(n) O(n) O(n) CS 221 - Computer Science II

CS 221 - Computer Science II Why Need Iterators? What is the Big-O of the following code? MyList<Integer> list; list = new MyList<Integer>(); // code to fill list with n elements //Big-O of following code? for(int i = 0; i < list.size(); i++) System.out.println( list.get(i) ); O(n) B. O(2n) C. O(n log n) D. O(n2) E. O(n3) CS 221 - Computer Science II

CS 221 - Computer Science II Why Need Iterators? What is the Big-O of the following code? MyList<Integer> list; list = new MyList<Integer>(); // code to fill list with n elements //Big-O of following code? for(int i = 0; i < list.size(); i++) System.out.println( list.get(i) ); O(n) B. O(2n) C. O(n log n) D. O(n2) E. O(n3) Depends… On what? CS 221 - Computer Science II

CS 221 - Computer Science II remove method public T remove(int index) Special case? CS 221 - Computer Science II

CS 221 - Computer Science II Arrays vs Linked Lists Which operations are more efficient using an array? Which are more efficient using a linked-list implementation? Which operations have the same efficiency? CS 221 - Computer Science II

Array / Linked List Efficiency Method Array Linked List add O(1) insert(index, value) O(n) indexOf get remove set size

Implementing a List with More Links A double-linked list can also be used to implement a list collection. Will need a Node class with next and previous references. Have to track both next and previous links but logic simpler. Probably want to use dummy to indicate end of list. CS 221 - Computer Science II

A Double-Linked List Implementation of List ADT public class MyList<T> implements IList<T> { private DLLNode<T> head; private DLLNode<T> tail; private DLLNode<T> end; private int count; public MyList() head = null; tail = null; end = new DLLNode<T>(null); setCount(0); } … CS 221 - Computer Science II

CS 221 - Computer Science II Updated Node Class public class DLLNode<T> { private T element; private DLLNode<T> next; private DLLNode<T> previous; … } CS 221 - Computer Science II

CS 221 - Computer Science II Dummy Nodes Use of Dummy Node for a double-linked list removes most special cases. Also could make the double-linked list circular. CS 221 - Computer Science II

CS 221 - Computer Science II

CS 221 - Computer Science II Lists in the Java API The list classes in the Java API primarily support the concept of an indexed list (and somewhat an unsorted list). The API does not have any classes that directly implement a sorted list. The ArrayList and LinkedList classes both implement the List<E> interface. CS 221 - Computer Science II

CS 221 - Computer Science II Lists in the Java API Some of the operations from the List<E> interface: CS 221 - Computer Science II

CS 221 - Computer Science II Implementing Lists The following operations are common to most types of lists: CS 221 - Computer Science II

CS 221 - Computer Science II Implementing Lists Operation particular to a sorted list: Operations particular to an unsorted lists: CS 221 - Computer Science II

Class Diagram of List Classes CS 221 - Computer Science II

CS 221 - Computer Science II Reference Based on content from: Java Foundations, 3rd Edition CS 221 - Computer Science II