LinkedLists CS445 Data Structures. What is a list? A list is an object that is a sequence of objects. The positions in the list are numbered 0, 1, 2,

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Chapter 24 Lists, Stacks, and Queues
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
CE203 - Application Programming Autumn 2013CE203 Part 41 Part 4.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Map Collections and Custom Collection Classes Chapter 14.
Data Structures and Collections
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.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Tree Traversals & Maps Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
The Java Collections Package C. DeJong Fall 2001.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
Lecture Objectives To understand the Java Map and Set interfaces and how to use them To be introduced to the implementation of Map s and Set s To see how.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
(c) University of Washington14-1 CSC 143 Java Collections.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
Big Java Chapter 16.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Sets and Maps Sets Maps The Comparator Interface Sets and Maps in Java Collections API – TreeSet – TreeMap Review for Exam Reading:
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Maps Nick Mouriski.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
AD Lecture #3 Java Collection Framework 1.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
CSE 143 Lecture 11: Sets and Maps reading:
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Ch 16: Data Structures - Set and Map
Road Map CS Concepts Data Structures Java Language Java Collections
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
CSE 1020: The Collection Framework
Web Design & Development Lecture 6
CS 240 – Advanced Programming Concepts
Presentation transcript:

LinkedLists CS445 Data Structures

What is a list? A list is an object that is a sequence of objects. The positions in the list are numbered 0, 1, 2, … or may be numbered 1, 2, 3, … Examples: 45, 75, 31, 52, 27 Bob, Nick, Anne, Mary, Larry, Celia

LinkedList of Chairs

LinkedList Head

What is a LinkedList? A LinkedList is one of the many ways to implement a list Each object is stored in a node of the form object next node

Instantiating a LinkedList LinkedList Examples: LinkedList bigLst = new LinkedList (); LinkedList stringLst = new LinkedList ();

LinkedList Functionality add(aObject) – add aObject to end of list add(new BigInteger( )) add(aObject, position) – add aObject at the position size() – size of list contains(aObject) – does aObject belong to list get(position) – get object at position in list remove() – remove object at beginning of list remove(position) – remove object from position indexOf(aObject) - remove object at position

LinkedList list = new LinkedList (); list.add("Bob"); list.add("Al"); list.add("Carl"); list.add(1,"Sela"); list.add(0,"Morgan"); list.add(3,"Mona"); System.out.println("list = " + list); System.out.println("size = " + list.size() ); if( list.contains("Mona " ) ){ list.remove(list.indexOf("Mona")); System.out.println("deleting " + "\"Mona\""); } System.out.println("list = " + list); System.out.println("first item in list is " + "\"" + list.getFirst() + "\""); System.out.println("last item in list is " + "\"" + list.getLast() + "\"");

Maps

What is a Map? An object that maps keys to values A map cannot contain duplicate keys A map may contain duplicate values Each key can map to at most one value. keys values Jones, William Keller, Mary Madden, Matt Jones, William

HashMap Table consisting of two columns Column 1 contains the keys Column 2 contains the values Jones, William Keller, Mary Madden, Matt Jones, William KeysValues Like a one-dimensional array, where a key acts like an index to access the corresponding value

Instantiating a HashMap HashMap Examples: HashMap hmap = new HashMap (); HashMap > amap = new HashMap >();

HashMap Functionality get(aKey) – returns corresponding value get( ) = Keller, Mary put(aKey, aValue) – inserts a pair put( , Keller, Mary) containsKey(aKey) – does key belong containsValue(aValue) – does value belong remove(aKey) – remove a pair whose key is aKey

HashMap Class values() – returns the set of all values keySet() - returns the set of all keys A HashMap is an implementation of a data structure called a hash table

import java.util.*; public class testHashMap{ public static void main(String[] args){ HashMap table = new HashMap (); table.put("Bob",28.0); table.put("Al",28.0); table.put("Carl",38.0); System.out.println(table.get("Al")); System.out.println(table.get("Carl")); if(!table.containsKey("Al")) table.put("Al",28.0); if(!table.containsKey("Donna")) table.put("Donna",24.0); System.out.println(table.keySet()); System.out.println(table.values()); }

TreeMap Sorted table with respect to keys Column 1 contains the keys Column 2 contains the values Jones, William Keller, Mary Jones, William Madden, Matt KeysValues sorted

Instantiating a TreeMap TreeMap Examples: TreeMap hmap = new TreeMap (); TreeMap amap = new TreeMap ();

TreeMap Functionality get(aKey) – returns corresponding value get( ) = Keller, Mary put(aKey, aValue) – inserts a pair put( , Keller, Mary) containsKey(aKey) – does key belong containsValue(aValue) – does value belong remove(aKey) – remove a pair whose key is aKey

TreeMap Class values() – returns the set of all values keySet() - returns the set of all keys

TreeMap as a Tree A TreeMap is an implementation of a data structure called a binary search tree Jones, William Keller, Mary Madden, Matt Jones, William