Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.

Slides:



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

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
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
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)
Double-Linked Lists and Circular Lists
CSE 1302 Lecture 23 Hashing and Hash Tables Richard Gesick.
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++)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
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.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Information and Computer Sciences University of Hawaii, Manoa
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
LinkedList Many slides from Horstmann modified by Dr V.
Data structures Abstract data types Java classes for Data structures and ADTs.
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).
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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.
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
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.
Chapter 16 Data Structures 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 16 An Introduction to Data Structures.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS 367 Introduction to Data Structures Lecture 5.
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.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Chapter 22 Generic Programming. Chapter Goals To understand the objective of generic programming To be able to implement generic classes and methods To.
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
Recursive Objects Singly Linked List (Part 2) 1. Operations at the head of the list  operations at the head of the list require special handling because.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Slides by Donald W. Smith
Chapter 20 An Introduction to Data Structures
Chapter 16 – Advanced Data Structures
Chapter 16 – Basic Data Structures
Chapter 17 Object-Oriented Data Structures
Introduction to Data Structures
Linked Lists, Stacks and Queues Textbook Sections
Chapter 15 – An Introduction to Data Structures
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Framework
Presentation transcript:

Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures

Copyright © 2014 by John Wiley & Sons. All rights reserved.2 Chapter Goals  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

Copyright © 2014 by John Wiley & Sons. All rights reserved.3 Implementing Linked Lists - The Node Class  We will implement a simplified, singly-linked list.  A linked list stores elements in a sequence of nodes.  A Node object stores an element and a reference to the next node. private inner class public instance variables public class LinkedList {... class Node { public Object data; public Node next; }

Copyright © 2014 by John Wiley & Sons. All rights reserved.4 Implementing Linked Lists - The Node Class  A linked list object holds a reference to the first node: each node holds a reference to the next node. public class LinkedList { private Node first; public LinkedList() { first = null; } public Object getFirst() { if (first == null) { throw new NoSuchElementException(); } return first.data; }

Copyright © 2014 by John Wiley & Sons. All rights reserved.5 Implementing Linked Lists - Adding and Removing the First Element  When adding or removing the first element, the reference to the first node must be updated. public class LinkedList {... public void addFirst(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = first; first = newNode; }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.6 Implementing Linked Lists – Adding the First Element Figure 1 Adding a Node to the Head of a LinkedList

Copyright © 2014 by John Wiley & Sons. All rights reserved.7 Implementing Linked Lists - Removing the First Element  The data of the first node are saved and later returned as the method result.  The successor of the first node becomes the first node of the shorter list.  The old node is eventually recycled by the garbage collector. public class LinkedList {... public Object removeFirst() { if (first == null) { throw new NoSuchElementException(); } Object element = first.data; first = first.next; return element; }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.8 Implementing Linked Lists – Removing the First Element Figure 2 Removing the First Node from a LinkedList

Copyright © 2014 by John Wiley & Sons. All rights reserved.9 The Iterator Class  Our simplified ListIterator interface has methods: next, hasNext, remove, add, and set.  Our LinkedList class declares a private inner class LinkedListIterator. LinkedListIterator implements our simplified ListIterator interface. As an inner class LinkedListIterator has access to o The instance variable first o The private Node class.  A list iterator object has: A reference to the the currently visited node, position A reference to the last node before that, previous A isAfterNext flag to track when the next method has been called.

Copyright © 2014 by John Wiley & Sons. All rights reserved.10 The Iterator Class  The LinkedListIterator class: public class LinkedList {... public ListIterator listIterator() { return new LinkedListIterator(); } class LinkedListIterator implements ListIterator { private Node position; private Node previous; private boolean isAfterNext; public LinkedListIterator() { position = null; previous = null; isAfterNext = false; }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.11 Advancing an Iterator  To advance an iterator: Update the position Remember the old position for the remove method.

Copyright © 2014 by John Wiley & Sons. All rights reserved.12 Advancing an Iterator  The next method: class LinkedListIterator implements ListIterator {... public Object next() { if (!hasNext()) { throw new NoSuchElementException(); } previous = position; // Remember for remove isAfterNext = true; if (position == null) { position = first; } else { position = position.next; } return position.data; }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.13 Advancing an Iterator  The iterator is at the end if the list is empty (first == null) or if there is no element after the current position (position.next == null).  The hasNext method: class LinkedListIterator implements ListIterator {... public boolean hasNext() { if (position == null) { return first != null; } else { return position.next != null; } }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.14 Removing an Element  If this is the first element: Call removeFirst Otherwise, update the next reference of the previous node  Update isAfterNext to disallow another call to remove.

Copyright © 2014 by John Wiley & Sons. All rights reserved.15 Removing an Element  The remove method: class LinkedListIterator implements ListIterator {... public void remove() { if (!isAfterNext) { throw new IllegalStateException(); } if (position == first) { removeFirst(); } else { previous.next = position.next; } position = previous; isAfterNext = false; }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.16 Removing an Element Figure 3 Removing a Node from the Middle of a Linked List

Copyright © 2014 by John Wiley & Sons. All rights reserved.17 Adding an Element  After adding the new element set the isAfterNext flag to false to disallow a subsequent call to the remove or set

Copyright © 2014 by John Wiley & Sons. All rights reserved.18 Adding an Element  The add method: class LinkedListIterator implements ListIterator {... public void add(Object element) { if (position == null) { addFirst(element); position = first; } else { Node newNode = new Node(); newNode.data = element; newNode.next = position.next; position.next = newNode; position = newNode; } isAfterNext = false; }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.19 Adding an Element Figure 4 Add a Node to the Middle of a Linked List

Copyright © 2014 by John Wiley & Sons. All rights reserved.20 Setting an Element to a Different Value  set method changes the data in the previously visited element.  Must follow a call to next.  The set method: public void set(Object element) { if (!isAfterNext) { throw new IllegalStateException(); } position.data = element; }

Copyright © 2014 by John Wiley & Sons. All rights reserved.21 Efficiency of Linked List Operations  To get the k th element of a linked list, you start at the beginning of the list and advance the iterator k times  To get to the k th node of a linked list, one must skip over the preceding nodes.

Copyright © 2014 by John Wiley & Sons. All rights reserved.22 Efficiency of Linked List Operations  When adding or removing an element, we update a couple of references in a constant number of steps.  Adding and removing an element at the iterator position in a linked list takes O(1) time.

Copyright © 2014 by John Wiley & Sons. All rights reserved.23 Efficiency of Linked List Operations  To add an element at the end of the list Must get to the end - an O(n) operation Add the element O(1) operation  Adding to the end of a linked list in our implementation takes O(n) time  If the linked list keeps a reference to last as well as first The time is reduced to constant time: O(1)  We will conclude that adding to the end of a linked list is O(1).

Copyright © 2014 by John Wiley & Sons. All rights reserved.24 Efficiency of Linked List Operations  To remove an element from the end of the list: Need a reference to the next-to-last element so that we can set its next reference to null Takes n-1 iterations  Removing an element from the end of the list is O(n).

Copyright © 2014 by John Wiley & Sons. All rights reserved.25 Efficiency of Linked List Operations Figure 5 Removing the Last Element of a Singly-Linked List

Copyright © 2014 by John Wiley & Sons. All rights reserved.26 Efficiency of Linked List Operations  In a doubly-linked list, each node has a reference to the previous node in addition to the next one. public class LinkedList {... class Node { public Object data; public Node next; public Node previous; }

Copyright © 2014 by John Wiley & Sons. All rights reserved.27 Efficiency of Linked List Operations  In a doubly-linked list, removal of the last element takes a constant number of steps. last = last.previous; last.next = null;

Copyright © 2014 by John Wiley & Sons. All rights reserved.28 Efficiency of Linked List Operations Figure 6 Removing the Last Element of a Doubly-Linked List

Copyright © 2014 by John Wiley & Sons. All rights reserved.29 Efficiency of Linked List Operations

Copyright © 2014 by John Wiley & Sons. All rights reserved.30 section_1/LinkedList.javaLinkedList.java 1 import java.util.NoSuchElementException; 2 3 /** 4 A linked list is a sequence of nodes with efficient 5 element insertion and removal. This class 6 contains a subset of the methods of the standard 7 java.util.LinkedList class. 8 */ 9 public class LinkedList 10 { 11 private Node first; /** 14 Constructs an empty linked list. 15 */ 16 public LinkedList() 17 { 18 first = null; 19 } /** 22 Returns the first element in the linked list. the first element in the linked list 24 */ 25 public Object getFirst() 26 { 27 if (first == null) { throw new NoSuchElementException(); } 28 return first.data; 29 } 30 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.31 section_1/LinkedList.javaLinkedList.java 31 /** 32 Removes the first element in the linked list. the removed element 34 */ 35 public Object removeFirst() 36 { 37 if (first == null) { throw new NoSuchElementException(); } 38 Object element = first.data; 39 first = first.next; 40 return element; 41 } /** 44 Adds an element to the front of the linked list. element the element to add 46 */ 47 public void addFirst(Object element) 48 { 49 Node newNode = new Node(); 50 newNode.data = element; 51 newNode.next = first; 52 first = newNode; 53 } 54 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.32 section_1/LinkedList.javaLinkedList.java 55 /** 56 Returns an iterator for iterating through this list. an iterator for iterating through this list 58 */ 59 public ListIterator listIterator() 60 { 61 return new LinkedListIterator(); 62 } class Node 65 { 66 public Object data; 67 public Node next; 68 } class LinkedListIterator implements ListIterator 71 { 72 private Node position; 73 private Node previous; 74 private boolean isAfterNext; /** 77 Constructs an iterator that points to the front 78 of the linked list. 79 */ 80 public LinkedListIterator() 81 { 82 position = null; 83 previous = null; 84 isAfterNext = false; 85 } 86 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.33 section_1/LinkedList.javaLinkedList.java 87 /** 88 Moves the iterator past the next element. the traversed element 90 */ 91 public Object next() 92 { 93 if (!hasNext()) { throw new NoSuchElementException(); } 94 previous = position; // Remember for remove 95 isAfterNext = true; if (position == null) 98 { 99 position = first; 100 } 101 else 102 { 103 position = position.next; 104 } return position.data; 107 } 108 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.34 section_1/LinkedList.javaLinkedList.java 109 /** 110 Tests if there is an element after the iterator position. true if there is an element after the iterator position 112 */ 113 public boolean hasNext() 114 { 115 if (position == null) 116 { 117 return first != null; 118 } 119 else 120 { 121 return position.next != null; 122 } 123 } 124 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.35 section_1/LinkedList.javaLinkedList.java 125 /** 126 Adds an element before the iterator position 127 and moves the iterator past the inserted element. element the element to add 129 */ 130 public void add(Object element) 131 { 132 if (position == null) 133 { 134 addFirst(element); 135 position = first; 136 } 137 else 138 { 139 Node newNode = new Node(); 140 newNode.data = element; 141 newNode.next = position.next; 142 position.next = newNode; 143 position = newNode; 144 } isAfterNext = false; 147 } 148 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.36 section_1/LinkedList.javaLinkedList.java 149 /** 150 Removes the last traversed element. This method may 151 only be called after a call to the next() method. 152 */ 153 public void remove() 154 { 155 if (!isAfterNext) { throw new IllegalStateException(); } if (position == first) 158 { 159 removeFirst(); 160 } 161 else 162 { 163 previous.next = position.next; 164 } 165 position = previous; 166 isAfterNext = false; 167 } /** 170 Sets the last traversed element to a different value. element the element to set 172 */ 173 public void set(Object element) 174 { 175 if (!isAfterNext) { throw new IllegalStateException(); } 176 position.data = element; 177 } 178 } 179 }

Copyright © 2014 by John Wiley & Sons. All rights reserved.37 section_1/ListIterator.javaListIterator.java 1 /** 2 A list iterator allows access of a position in a linked list. 3 This interface contains a subset of the methods of the 4 standard java.util.ListIterator interface. The methods for 5 backward traversal are not included. 6 */ 7 public interface ListIterator 8 { 9 /** 10 Moves the iterator past the next element. the traversed element 12 */ 13 Object next(); /** 16 Tests if there is an element after the iterator position. true if there is an element after the iterator position 18 */ 19 boolean hasNext(); /** 22 Adds an element before the iterator position 23 and moves the iterator past the inserted element. element the element to add 25 */ 26 void add(Object element); 27 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.38 section_1/ListIterator.javaListIterator.java 28 /** 29 Removes the last traversed element. This method may 30 only be called after a call to the next() method. 31 */ 32 void remove(); /** 35 Sets the last traversed element to a different value. element the element to set 37 */ 38 void set(Object element); 39 }

Copyright © 2014 by John Wiley & Sons. All rights reserved.39 Static Classes  Every object of an inner class has a reference to the outer class. It can access the instance variables and methods of the outer class  If an inner class does not need to access the data of the outer class, It does not need a reference Declare it static to save the cost of the reference

Copyright © 2014 by John Wiley & Sons. All rights reserved.40 Static Classes  Example: Declare the Node class of the LinkedList class as static: public class LinkedList {... static class Node {... } }

Copyright © 2014 by John Wiley & Sons. All rights reserved.41 Implementing Array Lists  An array list maintains a reference to an array of elements.  The array is large enough to hold all elements in the collection.  When the array gets full, it is replaced by a larger one.  An array list has an instance field that stores the current number of elements. Figure 7 An Array List Stores its Elements in an Array

Copyright © 2014 by John Wiley & Sons. All rights reserved.42 Implementing Array Lists  Our ArrayList implementation will manage elements of type Object: public class ArrayList { private Object[] elements; private int currentSize; public ArrayList() { final int INITIAL_SIZE = 10; elements = new Object[INITIAL_SIZE]; currentSize = 0; } public int size() { return currentSize; }... }

Copyright © 2014 by John Wiley & Sons. All rights reserved.43 Implementing Array Lists - Getting and Setting Elements  Providing get and set methods: Check for valid positions  Access the internal array at the given position  Helper method to check bounds: private void checkBounds(int n) { if (n = currentSize) { throw new IndexOutOfBoundsException(); } }

Copyright © 2014 by John Wiley & Sons. All rights reserved.44 Implementing Array Lists - Getting and Setting Elements  The get method: public Object get(int pos) { checkBounds(pos); return element[pos]; }  The set method: public void set(int pos, Object element) { checkBounds(pos); elements[pos] = element; }  Getting and setting an element can be carried out with a bounded set of instructions, independent of the size of the array list.  These are O(1) operations.

Copyright © 2014 by John Wiley & Sons. All rights reserved.45 Removing or Adding Elements  To remove an element at position k, move the elements with higher index values.  The remove method: public Object remove(int pos) { checkBounds(pos); Object removed = elements[pos]; for (int i = pos + 1; i < currentSize; i++) { elements[i - 1] = elements[i]; } currentSize--; return removed; }  On average, n / 2 elements need to move.

Copyright © 2014 by John Wiley & Sons. All rights reserved.46 Removing or Adding Elements  Inserting a element also requires moving, on average, n /2 elements.  Inserting or removing an array list element is an O(n) operation.

Copyright © 2014 by John Wiley & Sons. All rights reserved.47 Removing or Adding Elements  Exception: adding an element after the last element Store the element in the array Increment size  An O(1) operation  The addLast method: public boolean addLast(Object newElement) { growIfNecessary(); currentSize++; elements[currentSize - 1] = newElement; return true; }

Copyright © 2014 by John Wiley & Sons. All rights reserved.48 Removing or Adding Elements Figure 8 Removing and Adding Elements

Copyright © 2014 by John Wiley & Sons. All rights reserved.49 Growing the Internal Array When an array list is completely full, we must move the contents to a larger array.

Copyright © 2014 by John Wiley & Sons. All rights reserved.50 Growing the Internal Array  When the array is full: Create a bigger array  Copy the elements to the new array  New array replaces old  Reallocation is O(n).

Copyright © 2014 by John Wiley & Sons. All rights reserved.51 Growing the Internal Array  The growIfNecessary method: private void growIfNecessary() { if (currentSize == elements.length) { Object[] newElements = new Object[2 * elements.length]; for (int i = 0; i < elements.length; i++) { newElements[i] = elements[i]; } elements = newElements; }

Copyright © 2014 by John Wiley & Sons. All rights reserved.52 Growing the Internal Array

Copyright © 2014 by John Wiley & Sons. All rights reserved.53 Growing the Internal Array  Reallocation seldom happens.  We amortize the cost of the reallocation over all the insertion or removals.  Adding or removing the last element in an array list takes amortized O(1) time. Written O(1)+

Copyright © 2014 by John Wiley & Sons. All rights reserved.54 Efficiency of Array List and Linked List Operations

Copyright © 2014 by John Wiley & Sons. All rights reserved.55 Implementing Stacks and Queues  Stacks and queues are abstract data types.  We specify how operations must behave.  We do not specify the implementation.  Many different implementations are possible.

Copyright © 2014 by John Wiley & Sons. All rights reserved.56 Stacks as Linked Lists  A stack can be implemented as a sequence of nodes.  New elements are “pushed” to one end of the sequence, and they are “popped” from the same end.  Push and pop from the least expensive end - the front.  The push and pop operations are identical to the addFirst and removeFirst operations of the linked list.

Copyright © 2014 by John Wiley & Sons. All rights reserved.57 Stacks as Linked Lists Figure 10 Push and Pop for a Stack Implemented as a Linked List

Copyright © 2014 by John Wiley & Sons. All rights reserved.58 section_3_1/LinkedListStack.javaLinkedListStack.java 1 import java.util.NoSuchElementException; 2 3 /** 4 An implementation of a stack as a sequence of nodes. 5 */ 6 public class LinkedListStack 7 { 8 private Node first; 9 10 /** 11 Constructs an empty stack. 12 */ 13 public LinkedListStack() 14 { 15 first = null; 16 } /** 19 Adds an element to the top of the stack. element the element to add 21 */ 22 public void push(Object element) 23 { 24 Node newNode = new Node(); 25 newNode.data = element; 26 newNode.next = first; 27 first = newNode; 28 } 29 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.59 section_3_1/LinkedListStack.javaLinkedListStack.java 30 /** 31 Removes the element from the top of the stack. the removed element 33 */ 34 public Object pop() 35 { 36 if (first == null) { throw new NoSuchElementException(); } 37 Object element = first.data; 38 first = first.next; 39 return element; 40 } /** 43 Checks whether this stack is empty. true if the stack is empty 45 */ 46 public boolean empty() 47 { 48 return first == null; 49 } class Node 52 { 53 public Object data; 54 public Node next; 55 } 56 }

Copyright © 2014 by John Wiley & Sons. All rights reserved.60 Stacks as Arrays  A stack can be implemented as an array.  Push and pop from the least expensive end - the back.  The array must grow when it gets full.  The push and pop operations are identical to the addLast and removeLast operations of an array list.  push and pop are O(1)+ operations. Figure 11 A Stack Implemented as an Array

Copyright © 2014 by John Wiley & Sons. All rights reserved.61 Queues as Linked Lists  A queue can be implemented as a linked list: Add elements at the back Remove elements at the front. Keep a reference to last element  The add and remove operations are O(1) operations.

Copyright © 2014 by John Wiley & Sons. All rights reserved.62 Queues as Linked Lists Figure 12 A Queue Implemented as a Linked List

Copyright © 2014 by John Wiley & Sons. All rights reserved.63 Queues as Circular Arrays  In a circular array, we wrap around to the beginning after the last element.  When removing elements of a circular array, increment the index at which the head of the queue is locate  When the last element of the array is filled, Wrap around and start storing at index 0 If elements have been removed there is room else reallocate

Copyright © 2014 by John Wiley & Sons. All rights reserved.64 Queues as Circular Arrays

Copyright © 2014 by John Wiley & Sons. All rights reserved.65 Implementing a Hash Table  In the Java library sets are implemented as hash sets and tree sets.  Hashing: place items into an array at an index determined from the element.  Hash code: an integer value that is computed from an object, in such a way that different objects are likely to yield different hash codes.  Collision: when two or more distinct objects have the same hash code.  A good hash function minimizes collisions.  A hash table uses the hash code to determine where to store each element.

Copyright © 2014 by John Wiley & Sons. All rights reserved.66 Implementing a Hash Table

Copyright © 2014 by John Wiley & Sons. All rights reserved.67 Hash Tables  Hash table: An array that stores the set elements.  Hash code: used as an array index into a hash table.  Simplistic implementation Very large array Each object at its hashcode location Simple to locate an element But not practical Figure 14 A Simplistic Implementation of a Hash Table

Copyright © 2014 by John Wiley & Sons. All rights reserved.68 Hash Tables  Realistic implementation: A reasonable size array. Use the remainder operator to calculate the position. int h = x.hashCode(); if (h < 0) { h = -h; } position = h % arrayLength; Use separate chaining to handle collisions: o All colliding elements are collected in a linked list of elements with the same position value o The lists are called buckets Each entry of the hash table points to a sequence of nodes containing elements with the same hash code. A hash table can be implemented as an array of buckets— sequences of nodes that hold elements with the same hash code.

Copyright © 2014 by John Wiley & Sons. All rights reserved.69 Hash Tables Figure 15 A Hash Table with Buckets to Store Elements with the Same Hash Code

Copyright © 2014 by John Wiley & Sons. All rights reserved.70 Hash Tables Elements with the same hash code are placed in the same bucket.

Copyright © 2014 by John Wiley & Sons. All rights reserved.71 Implementing a Hash Table - Finding an Element  Algorithm to find an element, obj Compute the hash code and compress it. o gives an index h into the hash table. Iterate through the elements of the bucket at position h. o Check element is equal to obj. If a match is found among the elements of that bucket, o obj is in the set. o Otherwise, it is not  If there are no or only a few collisions: adding, locating, and removing hash table elements takes O(1) time.

Copyright © 2014 by John Wiley & Sons. All rights reserved.72 Adding and Removing Elements  Algorithm to add an element: Compute the compressed hash code h. Iterate through the elements of the bucket at position h. o For each element of the bucket, check whether it is equal to obj If a match is found among the elements of that bucket, then exit. Otherwise, add a node containing obj to the beginning of the node sequence. If the load factor exceeds a fixed threshold, reallocate the table.  Load factor: a measure of how full the table is. The number of elements in the table divided by the table length  Adding an element to a hash table is O(1)+

Copyright © 2014 by John Wiley & Sons. All rights reserved.73 Adding and Removing Elements  Algorithm to remove an element: Compute the hash code to find the bucket that should contain the object Try to find the element If it is present: remove it otherwise, do nothing Shrink the table if it becomes to sparse  Removing an element from a hash table is O(1)+

Copyright © 2014 by John Wiley & Sons. All rights reserved.74 Iterating over a Hash Table  When iterator points to the middle of a node chain, easy to get the next element  When the iterator is at the end of a node chain, Skip over empty buckets  Advance the iterator to the first node of the first non- empty bucket

Copyright © 2014 by John Wiley & Sons. All rights reserved.75 Iterating over a Hash Table  Iterator needs to store the bucket number and a reference to the current node in the node chain. if (current != null && current.next != null) { current = current.next; // Move to next element in bucket } else // Move to next bucket { do { bucketIndex++; if (bucketIndex == buckets.length) { throw new NoSuchElementException(); } current = buckets[bucketIndex]; } while (current == null); }

Copyright © 2014 by John Wiley & Sons. All rights reserved.76 Iterating over a Hash Table Figure 16 An Iterator to a Hash Table

Copyright © 2014 by John Wiley & Sons. All rights reserved.77 Hash Table Efficiency  The cost of iterating over all elements of a hash table: Is proportional to the table length  Not the number of elements in the table  Shrink the table when the load factor gets too small. One iteration is O(1). Iterating over the entire table is O(n).

Copyright © 2014 by John Wiley & Sons. All rights reserved.78 section_4/HashSet.javaHashSet.java 1 import java.util.Iterator; 2 import java.util.NoSuchElementException; 3 4 /** 5 This class implements a hash set using separate chaining. 6 */ 7 public class HashSet 8 { 9 private Node[] buckets; 10 private int currentSize; /** 13 Constructs a hash table. bucketsLength the length of the buckets array 15 */ 16 public HashSet(int bucketsLength) 17 { 18 buckets = new Node[bucketsLength]; 19 currentSize = 0; 20 } 21 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.79 section_4/HashSet.javaHashSet.java 22 /** 23 Tests for set membership. x an object true if x is an element of this set 26 */ 27 public boolean contains(Object x) 28 { 29 int h = x.hashCode(); 30 if (h < 0) { h = -h; } 31 h = h % buckets.length; Node current = buckets[h]; 34 while (current != null) 35 { 36 if (current.data.equals(x)) { return true; } 37 current = current.next; 38 } 39 return false; 40 } 41 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.80 section_4/HashSet.javaHashSet.java 42 /** 43 Adds an element to this set. x an object true if x is a new object, false if x was 46 already in the set 47 */ 48 public boolean add(Object x) 49 { 50 int h = x.hashCode(); 51 if (h < 0) { h = -h; } 52 h = h % buckets.length; Node current = buckets[h]; 55 while (current != null) 56 { 57 if (current.data.equals(x)) { return false; } 58 // Already in the set 59 current = current.next; 60 } 61 Node newNode = new Node(); 62 newNode.data = x; 63 newNode.next = buckets[h]; 64 buckets[h] = newNode; 65 currentSize++; 66 return true; 67 } 68 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.81 section_4/HashSet.javaHashSet.java 69 /** 70 Removes an object from this set. x an object true if x was removed from this set, false 73 if x was not an element of this set 74 */ 75 public boolean remove(Object x) 76 { 77 int h = x.hashCode(); 78 if (h < 0) { h = -h; } 79 h = h % buckets.length; Node current = buckets[h]; 82 Node previous = null; 83 while (current != null) 84 { 85 if (current.data.equals(x)) 86 { 87 if (previous == null) { buckets[h] = current.next; } 88 else { previous.next = current.next; } 89 currentSize--; 90 return true; 91 } 92 previous = current; 93 current = current.next; 94 } 95 return false; 96 } 97 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.82 section_4/HashSet.javaHashSet.java 98 /** 99 Returns an iterator that traverses the elements of this set. a hash set iterator 101 */ 102 public Iterator iterator() 103 { 104 return new HashSetIterator(); 105 } /** 108 Gets the number of elements in this set. the number of elements 110 */ 111 public int size() 112 { 113 return currentSize; 114 } class Node 117 { 118 public Object data; 119 public Node next; 120 } 121 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.83 section_4/HashSet.javaHashSet.java 122 class HashSetIterator implements Iterator 123 { 124 private int bucketIndex; 125 private Node current; /** 128 Constructs a hash set iterator that points to the 129 first element of the hash set. 130 */ 131 public HashSetIterator() 132 { 133 current = null; 134 bucketIndex = -1; 135 } public boolean hasNext() 138 { 139 if (current != null && current.next != null) { return true; } 140 for (int b = bucketIndex + 1; b < buckets.length; b++) 141 { 142 if (buckets[b] != null) { return true; } 143 } 144 return false; 145 } 146 Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.84 section_4/HashSet.javaHashSet.java 147 public Object next() 148 { 149 if (current != null && current.next != null) 150 { 151 current = current.next; // Move to next element in bucket 152 } 153 else // Move to next bucket 154 { 155 do 156 { 157 bucketIndex++; 158 if (bucketIndex == buckets.length) 159 { 160 throw new NoSuchElementException(); 161 } 162 current = buckets[bucketIndex]; 163 } 164 while (current == null); 165 } 166 return current.data; 167 } public void remove() 170 { 171 throw new UnsupportedOperationException(); 172 } 173 } 174 }

Copyright © 2014 by John Wiley & Sons. All rights reserved.85 section_4/HashSetDemo.javaHashSetDemo.java 1 import java.util.Iterator; 2 3 /** 4 This program demonstrates the hash set class. 5 */ 6 public class HashSetDemo 7 { 8 public static void main(String[] args) 9 { 10 HashSet names = new HashSet(101); names.add("Harry"); 13 names.add("Sue"); 14 names.add("Nina"); 15 names.add("Susannah"); 16 names.add("Larry"); 17 names.add("Eve"); 18 names.add("Sarah"); 19 names.add("Adam"); 20 names.add("Tony"); 21 names.add("Katherine"); 22 names.add("Juliet"); 23 names.add("Romeo"); 24 names.remove("Romeo"); 25 names.remove("George"); Iterator iter = names.iterator(); 28 while (iter.hasNext()) 29 { 30 System.out.println(iter.next()); 31 } 32 } 33 } Continued

Copyright © 2014 by John Wiley & Sons. All rights reserved.86 section_4/HashSetDemo.javaHashSetDemo.java Program Run: Harry Sue Nina Susannah Larry Eve Sarah Adam Juliet Katherine Tony