John Hurley Cal State LA

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Introduction to Computation and Problem
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Chapter 17 Linked Lists.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Linked Lists.
Linked Lists Chapter 4.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
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)
Data Structures Using C++
Concrete collections in Java library
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
Double-Linked Lists and Circular Lists
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Review Pseudo Code Basic elements of Pseudo code
Linked Lists.
Linked Lists.
The List ADT Textbook Sections
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Week 1.
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Building Java Programs Chapter 14
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
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++)
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 18 Java Collections Framework
LinkedList Many slides from Horstmann modified by Dr V.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
List Interface and Linked List Mrs. Furman March 25, 2010.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
UNIT-II Topics to be covered Singly linked list Circular linked list
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Chapter 19 Java Data Structures
John Hurley Cal State LA
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Chapter 20 Lists, Stacks, Queues, and Priority Queues
CS2013 Lecture 4 John Hurley Cal State LA.
Collections Framework
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Presentation transcript:

John Hurley Cal State LA CS203 Lecture 4 John Hurley Cal State LA

More Data Structures Memorize This! A data structure is a systematic way to organize information in order to improve the efficiency of algorithms that will use the data

More Data Structures So far we have studied arrays and lists, specifically ArrayLists, in detail, and also discussed stacks. As you know, this class is called "Programming With Data Structures," and obviously, data structures are the main topic of the course. Over the next few weeks, we will introduce a wide array (ha!) of new data structures. Most of these data structures can be used as collections of wide ranges of parameterized types We will first learn to use some of the data structure types included with the JDK, then implement one or two on our own, then return to using the built-in ones.

Java Collection Framework A collection is a container object that holds a group of objects, often referred to as elements. The Java Collections Framework supports three types of collections, named sets, lists, and maps.

Java Collection Framework hierarchy, cont. Set and List are subinterfaces of Collection.

The Collection Interface The Collection interface is the root interface for manipulating a collection of objects.

ArrayList and LinkedList Like an array, a list stores elements in a sequential order, and allows the user to specify where the element is stored. The user can access the elements by index. Recall that an array is fixed once it is created. An array is suitable if your application does not require insertion or deletion of elements. A list can grow or shrink dynamically. ArrayList and LinkedList are the most common concrete implementations of the List interface. If you need to support random access through an index without inserting or removing elements from any place other than the end, ArrayList offers the most efficient collection. ArrayList is implemented using an underlying array that begins with a default size. If the list outgrows the array, a new array must be created and the contents of the old one copied. Inserting values at arbitrary spots in the list involves moving all the subsequent elements. If your application requires the insertion or deletion of elements from arbitrary locations in the list, use a LinkedList.

The List Interface, cont.

java.util.ArrayList

Linked Lists A linked list is a data structure consisting of a group of nodes which represent a sequence. In the simplest form of linked list, each node is composed of a datum and a reference (in other words, a link) to the next node in the sequence. This is called a singly linked list. We will learn more about how linked lists are implemented in a few weeks; this week we will use the Java Collections Framework LinkedList<E> class.

Linked Lists In a doubly linked list, each node contains a reference to the next node and a reference to the previous node. Either a singly or double linked list may be circular; that is, last node points to the first, and, in a circular doubly-linked list, the first node has a reference to the last Diagrams from Wikipedia: http://en.wikipedia.org/wiki/Linked_list

Linked Lists Adding and removing elements from any spot in a linked list involves changing, at most, one reference in each of two existing elements and setting, at most, two references in a new element. Compare this to the expense of periodically creating a new array and copying all the elements from an old one. Add a node to a singly-linked list: Inserting a node into a doubly linked list just involves also changing the reference from the subsequent element and adding one from the new element back to the previous one

Linked Lists Delete a node from a singly-linked list:

java.util.LinkedList LinkedList implements a doubly-linked list. It is a subclass of a hierarchy of List classes, but more importantly it implements the List interface.

package demos; import java.util.*; public class TestArrayAndLinkedList { public static void main(String[] args) { List<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(1); // 1 is autoboxed to new Integer(1) arrayList.add(2); arrayList.add(3); arrayList.add(1); arrayList.add(4); arrayList.add(0, 10); arrayList.add(3, 30); System.out.println("A list of integers in the array list:"); System.out.println(arrayList); LinkedList<Object> linkedList = new LinkedList<Object>(arrayList); linkedList.add(1, "red"); linkedList.removeLast(); linkedList.addFirst("green"); System.out.println("Display the linked list backward:"); for (int i = linkedList.size() - 1; i >= 0; i--) { System.out.print(linkedList.get(i) + " "); }

Linked Lists Based on the info above, you might expect linked lists to be much more efficient than array lists. However, data in a linked list is accessed sequentially; we start at the head of the list and follow the references. This is called traversing the list. node = list.firstNode while node not null (do something with node.data) node = node.next Source for pseudocode: Wikipedia This may make a linked list less efficient than an array list for operations that require a great deal of traversal, because accessing an array element by index Is efficiently implemented. Also, memory is allocated on the fly when a node is added. This makes it more likely that different nodes will be far apart in memory, which makes it more expensive to traverse the list.

package listcompare; public class StopWatch { private long elapsedTime; private long startTime; private boolean isRunning; public StopWatch() { reset(); } public void start() { if (isRunning) { return; isRunning = true; startTime = System.currentTimeMillis(); public void stop() { if (!isRunning) { isRunning = false; long endTime = System.currentTimeMillis(); elapsedTime = elapsedTime + endTime - startTime;

public long getElapsedTime() { if (isRunning) { long endTime = System.currentTimeMillis(); return elapsedTime + endTime - startTime; } else { return elapsedTime; } public void reset() { elapsedTime = 0; isRunning = false;

package listcompare; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; public class ListComparer { Random r = new Random(); public void arrayListSequentialAdds() { List<Integer> al = new ArrayList<Integer>(); for (int counter = 0; counter < 100000; counter++) { al.add(0, counter); //System.out.println(counter); } public void linkedListSequentialAdds() { List<Integer> ll = new LinkedList<Integer>(); ll.add(0, counter); public void arrayListRandomAdds() { al.add(r.nextInt(al.size() + 1), counter); public void linkedListRandomAdds() { ll.add(r.nextInt(ll.size() + 1), counter);

package listcompare; public class ListDriver { public static void main(String[] args) { StopWatch s = new StopWatch(); ListComparer l = new ListComparer(); s.reset(); s.start(); l.arrayListSequentialAdds(); s.stop(); System.out.println("Array List sequential exercise took " + s.getElapsedTime() + " ms"); l.linkedListSequentialAdds(); System.out.println("Linked List sequential exercise took " + s.getElapsedTime() + " ms"); l.arrayListRandomAdds(); System.out.println("Array List random exercise took " + s.getElapsedTime() + " ms"); l.linkedListRandomAdds(); System.out.println("Linked List random exercise took " + s.getElapsedTime() + " ms"); }

I/O Expense This is a good time to point out that I/O is very expensive. Rerun the code above with the System.out.println statements uncommented.