CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Linked Lists Dr. Tim Margush University of Akron © 2009.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Information and Computer Sciences University of Hawaii, Manoa
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
LinkedList Many slides from Horstmann modified by Dr V.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
(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.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Java Programming: From the Ground Up Chapter 17 The Java Collections Framework.
List Interface and Linked List Mrs. Furman March 25, 2010.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
CSCI 62 Data Structures Dr. Joshua Stough September 25, 2008.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
1 CS162: Introduction to Computer Science II Abstract Data Types.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
An Array-Based Implementation of the ADT List
Linked Data Structures
Chapter 4 Unordered List.
The List ADT.
Double-Ended Queues Chapter 5.
Single-Linked Lists.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Marcus Biel, Software Craftsman
4. Linked Lists.
Announcements & Review
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Java collections library
Priority Queue.
Chapter 17 Object-Oriented Data Structures
Object Oriented Programming COP3330 / CGS5409
The List ADT Reading: Textbook Sections 3.1 – 3.5
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
ArrayLists 22-Feb-19.
Collections Framework
Queues CSC212.
JCF Collection classes and interfaces
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Programming II (CS300) Chapter 07: Linked Lists
TCSS 143, Autumn 2004 Lecture Notes
Presentation transcript:

CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008

Today Concordance Program Program 3 Lists

For handling varying amounts of data Grows and shrinks when necessary and does it fast Methods involving random access are slow. (book posits this without detail, because the detail is not the abstract part). –Insert –Find –Get

Lists: interface public interface List extends Structure { public int size(); // post: returns number of elements in list public boolean isEmpty(); // post: returns true iff list has no elements public void clear(); // post: empties list public void addFirst(E value); // post: value is added to beginning of list public void addLast(E value); // post: value is added to end of list public E getFirst(); // pre: list is not empty // post: returns first value in list

Lists:interface public E getLast(); // pre: list is not empty // post: returns last value in list public E removeFirst(); // pre: list is not empty // post: removes first value from list public E removeLast(); // pre: list is not empty // post: removes last value from list public E remove(E value); // post: removes and returns element equal to value // otherwise returns null public void add(E value); // post: value is added to tail of list public E remove(); // pre: list has at least one element // post: removes last value found in list

Lists: interface public E get(); // pre: list has at least one element // post: returns last value found in list public boolean contains(E value); // pre: value is not null // post: returns true iff list contains an object equal to value public int indexOf(E value); // pre: value is not null // post: returns (0-origin) index of value, // or -1 if value is not found public int lastIndexOf(E value); // pre: value is not null // post: returns (0-origin) index of value, // or -1 if value is not found public E get(int i); // pre: 0 <= i < size() // post: returns object found at that location

Lists: interface public E set(int i, E o); // pre: 0 <= i < size() // post: sets ith entry of list to value o; // returns old value public void add(int i, E o); // pre: 0 <= i <= size() // post: adds ith entry of list to value o public E remove(int i); // pre: 0 <= i < size() // post: removes and returns object found at that location public Iterator iterator(); // post: returns an iterator allowing // ordered traversal of elements in list

List extends Structure Anywhere is a Structure is desired, a list can serve. The interface makes implementation- independent decisions. –Add method adds to end of list. Could be set the last element of array Could be instantiate a new object and have current list reference it.

public static void main(String[] args) { // input is read from System.in Scanner s = new Scanner(System.in); String current; // current line // list of unique lines List lines = new SinglyLinkedList (); //A SinglyLinkedList implements the List interface. // read a list of possibly duplicated lines while (s.hasNextLine()) { current = s.nextLine(); // check to see if we need to add it if (!lines.contains(current)) { System.out.println(current); lines.add(current); }

Free Lists: managing a pool of resources class Space { // structure describing parking space public final static int COMPACT = 0; // small space public final static int MINIVAN = 1; // medium space public final static int TRUCK = 2; // large space protected int number; // address in parking lot protected int size; // size of space public Space(int n, int s) // post: construct parking space #n, size s { number = n; size = s; } public boolean equals(Object other) // pre: other is not null // post: true iff spaces are equivalent size { Space that = (Space)other; return this.size == that.size; }}

Free Lists: managing a pool of resources

In order to return a spot, similar to add: –Create the proposed Assoc (the one the user says they want to give back) –If it’s among the rented spaces: Remove from rented Add to free –Otherwise say couldn’t remove it.

Abstract Lists (extend AbstractStructure, implement List Abstract class may or may not contain abstract methods (declared without implementation). A subclass of an abstract class must implement must implement all abstract methods or else still be abstract.

Linked Lists Three classes (typically) working together –an “item” class one atomic unit of the aggregate data e.g., a “Name” class (item) might have two instance variables String first, last; –a “node” class one “item” and a reference to the next “node” the next reference is the “link” in “linked list” –a “list” class reference to the first “node”—head of the list

Linked Lists data Item nextNode Node List head Node

Linked Lists New List head Node List head Node

Linked Lists Find List head Node

Linked Lists Insert Node List head Node

Linked Lists Delete List head Node

Linked Lists: random stuff Generally: advantage to implementing some methods by calling others: –removeLast() -> remove(size()-1) –isEmpty() -> size() == 0 –addFirst(E e) -> add(0, e) Speed: –Remove/add to front is constant time –Remove/add to back or middle is O(n) How to make constant for addLast?