CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Chapter 24 Lists, Stacks, and Queues
Advertisements

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Linked Lists Course Lecture Slides 23 July 2010 A list is only as strong.
Today’s Agenda  Stacks  Queues  Priority Queues CS2336: Computer Science II.
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.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Lists Based on content from: Java Foundations, 3rd Edition.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Information and Computer Sciences University of Hawaii, Manoa
CS 146: Data Structures and Algorithms June 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
111 © 2002, Cisco Systems, Inc. All rights reserved.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1/ 124 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 18 Programming Fundamentals using Java 1.
School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Linked Structures.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
A data structure is a type of data storage ….similar to an array. There are many data structures in Java (Stacks, Queues, LinkedList, Sets, Maps, HashTables,
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Arrays and Collections Tonga Institute of Higher Education.
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Data Structures for Midterm 2. C++ Data Structure Runtimes.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Understanding General Software Development Lesson 3.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CS 146: Data Structures and Algorithms June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
Linear Data Structures
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Understanding General Software Development Lesson 3.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Slides by Donald W. Smith
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
Lesson Objectives Aims
Collections Framework
Road Map CS Concepts Data Structures Java Language Java Collections
structures and their relationships." - Linus Torvalds
Presentation transcript:

CS 46B: Introduction to Data Structures July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak The Java Collections Framework  A built-in set of interfaces and classes that you can use in your programs to organize multiple data objects. Different ways to store the objects in memory. Different ways to access and manipulate the objects.  You put the objects into a collection. Example collection type: ArrayList 2

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Collections Interfaces and Classes 3

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak List  A list is an ordered collection of objects. Order does not necessarily mean sorted.  Once you’ve established an order, you can access the objects of a list one after another.  You can also access any object in a list directly via an index. Examples: 1 st object, 42 nd object, 5 th object, etc. 4

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak List, cont’d  A list of books:  ArrayList, Stack, and LinkedList implement the List interface.  List methods include: get() and set() add() and remove() size(), clear(), and isEmpty() 5

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Stack  Stack is a subclass of List Therefore, it is an ordered collection of objects.  Add objects to a stack or remove objects only at one end of the stack only. This end is commonly called the “top” of the stack. Call method push() to add an object to a stack. Call method pop() to remove an object. 6

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Stack, cont’d  Why does the term “last in, first out” (LIFO) describe a stack? 7

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Queue  Add objects only at one end of a queue, called its “tail”.  Remove objects only at the other end, called the “head” of the queue. 8 tail head

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Queue, cont’d  Why does the term “first in, first out” (FIFO) describe a queue? 9

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Set  A set is an unordered collection of unique objects. You don’t care about the order of the objects. You don’t want any duplicate objects.  A set of books:  Some methods: add(), remove() size(), isEmpty() contains() 10

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Map  A map is a collection in which you use a key to access a value in the map. It maps a key to its value. A map stores the keys, values, and the associations between them. 11 Mapping bar codes (keys) to books (values).

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Common Collector Methods 12

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak LinkedList  Like the ArrayList class, the LinkedList class implements the List interface. LinkedList also implements the Queue interface.  Therefore, it has the same methods: get() and set() add() and remove() size(), clear(), and isEmpty() 13

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak LinkedList, cont’d  A linked list is a sequence of nodes.  Each node contains: A data value. A reference to the next node in the sequence.  The reference is null if it’s the last node. A reference to the previous node in the sequence.  The reference is null if it’s the first node. 14

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Linked List, cont’d  Unlike an array list, you cannot directly access the i th node of a linked list. You start with the head of the list and then “chase” (follow) references (“links” or “pointers”) to the next node until you get the the i th node. Or, you can start with the tail of the list and then chase links to the previous node until you get to the i th node.  If the list is long, it can take a while to get to the node you want. 15

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak LinkedList, cont’d  Adding a node into a linked list can be very fast. You need to update only the references of the node’s new neighbors. Example: Add Romeo between Diana and Harry. 16 But getting to the node containing “Diana” may take a while.

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak LinkedList, cont’d  Similarly, removing a node from a linked list can be very fast. 17 But getting to the node containing “Diana” may take a while.

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak LinkedList, cont’d  By the way, adding and removing an object from an array list can be very slow. Why? 18

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak The LinkedList Class  The LinkedList class of the collections framework: 19

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak ListIterator  The get() method of a linked list can be very slow. Imagine a linked list L of a million nodes. Your program calls L.get(500000) How many links do you have to chase from the head of the list to access the node?  A list iterator keeps track of a position in a list. A list’s iterator allows you to efficiently access each node of the list in succession. 20

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak ListIterator, cont’d  Suppose you have a linked list named workers of Employee objects: Then iter enables you to access each object in turn, starting with the first one in the list: 21 LinkedList workers =... ; ListIterator iter = workers.listIterator(); while (iter.hasNext()) { Employee w = iter.next();... }

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak ListIterator, cont’d  You can think of an iterator as a “cursor” between list nodes:  You insert (add) before the cursor and so then the cursor is after the inserted node. 22

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak List ListIterator, cont’d 23

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak ListIterator, cont’d  To use an iterator to remove a list node: Call the iterator’s next() or previous() method to access a node. Then call the iterator’s remove() method to remove the node just accessed.  Do not: Call remove() more than once after calling next() or previous(). Call remove() right after calling add(). 24 Demo

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Clicker Question July 9 #1  What is the contents of letters after this sequence of instructions? 25 List letters = new LinkedList<>(); letters.add("F"); letters.add("R"); letters.add("E"); letters.add("D"); ListIterator iter = letters.iterator(); iter.next(); iter.remove(); iter.next(); iter.add("X"); a. [F, R, E, X] b. [F, E, X, D] c. [F, E, R, X] d. Something else

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Break 26

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Review for Midterm #2  Similar to Midterm #1  Open book, laptop, Internet  Use Codecheck and submit to Canvas  1 hour 55 minutes 27

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Review for Midterm #2  Recursion Solve the base case Look for a simpler but similar problem that’s closer to the base case whose solution will help solve the overall problem. Solve the simpler problem recursively.  Mutual recursion 28

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Review for Midterm #2, cont’d  Sorting selection sort merge sort quicksort  Partitioning strategy pivot element  Performance analysis Proof that merge sort is O(N log N) growth order 29

Computer Science Dept. Summer 2015: July 9 CS 46B: Introduction to Data Structures © R. Mak Review for Midterm #2, cont’d  Arrays.sort() and Collections.sort()  Searching binary search (recursive and non-recursive)  Parameterized Comparable and Comparator interfaces  Collections framework List interface linked list linked list iterator 30