MyList<T> It’s a generic type, <T> is a type parameter ArrayList int size(); T get(int i); T set(int i, T item); int indexOf(T item); void add(T item); void add(int i, T item); T remove(int i); Object[] toArray(); <K> K[] toArray(K[] a); A i = 2 [2] [0] [1] B [2] C [3] D [4] E [5] F [6] G [7] H [8] [9] 11/11/2018 IT 179
MyList<T> The internal information and structures are protected. Single Linked List MyList<T> [6] int size(); T get(int i); T set(int i, T item); int indexOf(T item); void add(T item); void add(int i, T item); T remove(int i); Object[] toArray(); <K> K[] toArray(K[] a); G i = 2 [2] [1] B [5] F [3] A D [0] C [4] E [2] H [7] The internal information and structures are protected. 11/11/2018 IT 179
L1 L2 L3 L4 11/11/2018 IT 179 MyArrayList<Integer> data size = 5 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … “Sean” 20 “Leon” 45 “Tom” 5 “Mary” 14 MyArrayList<String> data size = 4 void remove(int i) {….} void add(int I, String item) {…} Stringr max() {…}; … L1 L2 L3 L4 24 MySLinkedList<String> head size = 2 void remove(int i) {….} void add(int I, String item) {…} String max() {…}; … data next “ISU” “IT179” data next 11 270 228 MySLinkedList<Integer> head size = 3 void remove(int i) {….} void add(int I, Integer item) {…} Integer max() {…}; … data next data next data next 11/11/2018 IT 179
What do we need for single linked lists? Data structures head tail (optional) A B C D get(0) get(2) get(3) remove E insert Operations 11/11/2018 IT 179
Internal classes (define a class inside a class) public class A { private .... Public .... } public class B { private .... Public .... } private class C { ..... } public class ClassA<T> { T head; } public class ClassB<T,E> { T t; E e; } public class B<T> { private .... Public .... C<T> } private class C<E> { E ... T ... } IT 179 11/11/2018
Using a generic inner class for nodes public class MySLinkedList <T> implements MyList<T>{ /***** This is an inner class for internal nodes ********/ private class Node<E> { private E data; private Node<E> next; private Node(E data, Node<E> next) { // Construct a node pointing to next this.data = data; this.next = next; } /**** This is the end of the inner class Node<E> ******/ private Node<T> head; private int size; public MySLinkedList() { head = null; size = 0; ..... .... x y 11/11/2018 IT 179
Using a non-generic inner class for nodes public class MySLinkedList <T> implements MyList<T>{ /***** This is an inner class for internal nodes ********/ private class Node { private T data; private Node next; private Node(T data, Node next) { // Construct a node pointing to next this.data = data; this.next = next; } /**** This is the end of the inner class Node<E> ******/ private Node head; private int size; public MySLinkedList() { head = null; size = 0; ..... .... x y 11/11/2018 IT 179
There is no index, we have to count. /** * @see myUtil.MyList#get(int) */ public T get(int at) { if (at < 0 || at >= size) throw new IndexOutOfBoundsException("IndexOutOfBoundsException: "); Node<T> theNode = head; int count = 0; while (count < at) { theNode = theNode.next; count++; } return theNode.data; theNode X X X X head 11/11/2018 IT 179
There is no index, we have to count. ..... public int indexOf(T data) { if (head == null) return -1; // nothing there Node<T> theNode = head; int count = 0; while (!theNode.data.equals(data)) { theNode = theNode.next; if (theNode == null) return -1; count++; } return count; That’s why equals is important 11/11/2018 IT 179
* @see myUtil.MyList#set(int, java.lang.Object) */ ..... /** * @see myUtil.MyList#set(int, java.lang.Object) */ public T set(int at, T data) { // return the previous value; if (at < 0 || at >= size) throw new IndexOutOfBoundsException(); Node<T> theNode = head; int count = 0; while (count < at) { theNode = theNode.next; count++; } T oldValue = theNode.data; theNode.data = data; return oldValue; 11/11/2018 IT 179
Append (alternative, not required by MyList<T>) ..... public void append(T item) { // append item to the tail Node<T> newNode = new Node<T>(item, null); size ++; if (head == null) { head = newNode; return; } Node<T> tail = head; while (tail.next != null) tail = tail.next; tail.next = newNode; We have to find the tail. tail X X X null head 11/11/2018 IT 179
Using private method /** * @see myUtil.MyList#add(java.lang.Object) */ public void add(T item) { // append item to the tail add(size, item); } /** * @see myUtil.MyList#add(int, java.lang.Object) */ public void add(int at, T item) { // insert item at at if (at < 0 || at > size) // no can do throw new IndexOutOfBoundsException("Can't add at "+at); if (at == 0) { head = new Node<T>(item, head); size++; } else { addAfter(getNode(at-1),item); } private void addAfter(Node<T> node, T item) { node.next = new Node<T>(item, node.next); size++; } 11/11/2018 IT 179
Private getNode. /** * @see myUtil.MyList#get(int) */ private Node<T> getNode(int at) { if (at < 0 || at >= size) throw new IndexOutOfBoundsException("IndeOutOfBoundsException: "); Node<T> theNode = head; int count = 0; while (count < at) { theNode = theNode.next; count++; } return theNode; theNode X X X X return theNode; head 11/11/2018 IT 179
remove We need to know the previous node /** * @see myUtil.MyList#remove(int) */ public T remove(int at) { if (at < 0 || at >= size) throw new IndexOutOfBoundsException("IndeOutOfBoundsException: "); if (at == 0) { T temp = head.data; head = head.next; size--; return temp; } return removeNext(getNode(at-1)); private T removeNext(Node<T> node) { T temp = node.next.data; node.next = node.next.next; 11/11/2018 IT 179
toArray /** * @see myUtil.MyList#toArray() */ public Object[] toArray() { Object[] a = new Object[size]; Node<T> currentNode = head; for (int i = 0; i<a.length; i++) { a[i] = currentNode.data; currentNode = currentNode.next; } return a; 11/11/2018 IT 179
toArray public <K> K[] toArray(K[] a) { if (a == null) throw new NullPointerException("Argument can't be null"); if (a.length < size) a = (K[]) new Object[size]; Node<T> node = head; for (int i = 0; i<a.length ; i++) { if (i < size) { a[i] = (K) node.data; node = node.next; } else{ a[i] = null; return a; 11/11/2018 IT 179
* @see java.lang.Object#toString() */ public String toString() { /** * @see java.lang.Object#toString() */ public String toString() { Node<T> c=head; String outString="["; while (c != null) { outString += c.data.toString()+","; c = c.next; } return outString.substring(0,outString.length()-1)+"]"; 11/11/2018 IT 179