Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations."— Presentation transcript:

1 CSS446 Spring 2014 Nan Wang

2  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

3 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

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

5  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

6  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

7 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

8  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

9  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

10  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

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

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

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

14 14

15 15

16 16

17 17

18 18

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

20  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.

21  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.

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

23  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 24

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

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

27  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

28  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

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

30 30

31  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 32

33  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

34  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

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

36 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

37  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

38  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

39  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

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

41 41

42 42

43 43

44 44

45 45

46 46

47 47

48 48

49 49

50 50

51 51


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

Similar presentations


Ads by Google