CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

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.
Chapter 24 Lists, Stacks, and Queues
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
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)
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.
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++)
CS252: Systems Programming Ninghui Li Program Interview Questions.
M180: Data Structures & Algorithms in Java
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.
Data Structures: A Pseudocode Approach with C
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
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.
1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.
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.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
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, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CS2110 Recitation Week 8. Hashing Hashing: An implementation of a set. It provides O(1) expected time for set operations Set operations Make the set empty.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Information and Computer Sciences University of Hawaii, Manoa
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
LinkedList Many slides from Horstmann modified by Dr V.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
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).
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Hashing as a Dictionary Implementation Chapter 19.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
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,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Click to edit Master text styles Stacks Data Structure.
Building Java Programs Generics, hashing reading: 18.1.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Sets and Maps Chapter 9.
Chapter 16 – Basic Data Structures
Chapter 17 Object-Oriented Data Structures
Introduction to Data Structures
Chapter 15 – An Introduction to Data Structures
CS2013 Lecture 4 John Hurley Cal State LA.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Framework
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Sets and Maps Chapter 9.
Presentation transcript:

CSS446 Spring 2014 Nan Wang

 To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations of lists and arrays  To implement the stack and queue data types  To implement a hash table and understand the efficiency of its operations 2

Node Class  A linked list stores elements in a sequence of nodes.  Node object stores an element and a reference to the next node  make Node a private inner class of the LinkedList class 3

 LinkedList class holds a reference first to the first node (or null, if the list is completely empty) 4

 When a new node is added, it becomes the head of the list, and the node that was the old list head becomes its next node 5

 The successor of the first node becomes the first node of the shorter list. Then there are no further references to the old node, and the garbage collector will eventually recycle it. 6

Method Summary void addadd(E e) Inserts the specified element into the list (optional operation).E boolean hasNexthasNext() Returns true if this list iterator has more elements when traversing the list in the forward direction. E nextnext() Returns the next element in the list. void removeremove() Removes from the list the last element that was returned by next or previous (optional operation). void setset(E e) Replaces the last element returned by next or previous with the specified element (optional operation).E 7

 LinkedList class declares a private inner class LinkedListIterator, which implements our simplified ListIterator interface.  Each iterator includes two references and one flag: 8 Node Position, to the current node Node Previous, to the last node. boolean isAfterNext, flag to track when the next method has been called

 To advance an iterator, update the position to position.next and remember the old.  If the iterator points before the first element of the list, then the old position is null, and position must be set to first. 9

 In order to remove an element, first call next and then call remove on the iterator. first element, call removeFirst Element in the middle of the list, previous.next = position.next  The position reference skips over the element after the removed one. 10

 It is illegal to call remove twice in a row. 11

 The add method of the iterator inserts the new node after the last visited node 12

 set method changes the data stored in the previously visited element 13

14

15

16

17

18

 Array lists allowing you to add and remove elements at any position. 19

 An array list maintains a reference to an array of elements. The array is large enough and when the array gets full, it is replaced by a larger one. 20 For simplicity, our ArrayList manages elements of type Object. To access array list elements, we provide get and set methods. There are O(1) operations for get and set method.

 When removing an element at position k, the elements with higher index values need to move. 21 Removing the ith element  There are O(n) operations.

 Adding an element to an array list cost only O(1). 22

 If there is no more room in the internal array, then we need to grow it.  The new array is typically twice the size of the current array and the elements are then copied to the new array.----O(n) 23

24

 Add and remove notes from the same end of the node sequence. 25

 The push and pop are both O(1) operations. 26

 Store the value in an array and therefore saving the storage of references.  The array can grow when it gets full.  The push and pop are both O(1)+ operations. 27

 Add nodes at one end of the queue and remove them at the other end.  The add and remove operations of a queue are O(1) operations 28

 In a circular array implementation of a queue, element locations wrap from the end of the array to the beginning. 29

30

 Hash Set and Hash Map  Hash function is to compute hash code from an object in such a way that different objects are likely to yield different hash code. ◦ Int h=x.hashCode(); 31

32

 The basic idea behind hashing is to place object into an array, at a location that can be determined from the object itself.  It is possible for two or more distinct objects to have the same hash code - Collision 33

 A hash table uses the hash code to determine where to store each element.  Idea 1: A hash code is used as an array index into a hash table.  No collision  Large enough Array Needed  Solution: ◦ Compress 34

 Pick an array of reasonable size and then compress the hash code to become a valid array index.  Problem: Collsion  Solution: bucket 35

Hash Table  using a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.  Elements in a bucket or slot are stored as a linked list 36

 Rule: evenly distributed in all buckets  Load factor F = n/L ◦ n: the number of element ◦ L: the table length ◦ 0.75 is for the standard Java library 37

 Algorithm for finding an object obj in a hash table: 1. Compute the hash code and compress it. This gives an index h into the hash table. 2. Iterate through the elements of the bucket at position h. For each element of the bucket, check whether it is equal to obj. 3. If a match is found among the elements of that bucket, then obj is in the set.  Taking O(1) constant time. 38

 First compute the hash code to locate the bucket and then insert: 1. Compute the compressed hash code h. 2. Iterate through the elements of the bucket at position h. For each element of the bucket, check whether it is equal to obj. 3. If a match is found among the elements of that bucket, then exit. 4. Otherwise, add a node containing obj to the beginning of the node sequence. 5. If the load factor exceeds a fixed threshold, reallocate the table.  Adding an element to a hash table is O(1)+ 39

 Iterating over a Hash Table takes O(n) time. 40

41

42

43

44

45

46

47

48

49

50

51