Presentation is loading. Please wait.

Presentation is loading. Please wait.

12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.

Similar presentations


Presentation on theme: "12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface."— Presentation transcript:

1 12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface for several implementations {interface} {abstract}

2 12/18/2015ITK 2752 Interface for the user ArrayList LinkedLis t Vector Stack XXXX List.... 1 2 3 4 5 6 7 8 void add(T item) T insert(int at, T item) void append(T item) T get(int at) T set(int at, T item) int indexOf(T item) T remove(int at) int size() Object[] toArray(Object[] a) String toString()

3 12/18/2015ITK 2753 API ArrayList ArrayList 1 2 3 4 5 6 7 8 Application Programming Interface It’s a generic type, is a type parameter void add(T item) T insert(int at, T item) void append(T item) T get(int at) T set(int at, T item) int indexOf(T item) T remove(int at) int size() Object[] toArray(Object[] a) String toString()

4 12/18/2015ITK 2754 API LinkedList 1 2 3 4 5 6 7 8 LinkedLis t void add(T item) T insert(int at, T item) void append(T item) T get(int at) T set(int at, T item) int indexOf(T item) T remove(int at) int size() Object[] toArray(Object[] a) String toString()

5 12/18/2015ITK 2755 My SingleLinkedList 1 2 3 4 5 6 7 8 SngleLinkedList void add(T item) T insert(int at,T item) void append(T item) T get(int at) T set(int at, T item) int indexOf(T item) T remove(int at) int size() Object[] toArray(Object[] a) String toString() The internal information and structures are protected.

6 12/18/2015ITK 2756 What we need for single linked lists 1234 8 headtail insert remove

7 12/18/2015ITK 2757 Internal classes public class A { private.... Public.... } private static class C {..... } public class B { private.... Public.... }

8 12/18/2015ITK 2758 public class SingleLinkedList implements Iterable { /***** This is an inner class for internal nodes ********/ private static class Node { private E data; private Node next; private Node(E data, Node next) { // Construct a node pointing to next this.data = data; this.next = next; } } /**** This is the end of the inner class Node ******/ private Node head; public SingleLinkedList() { head = null; }......... } Using an inner class for the internal nodes EE

9 12/18/2015ITK 2759 public class SingleLinkedList implements Iterable {..... public void add(T item) { // add item to the head head = new Node (item,head); } public int size() { int count = 0; Node next=head; while (next != null) { count++; next = next.next; } return count; }..... Two easy methods EE head 234 next

10 12/18/2015ITK 27510 public class SingleLinkedList implements Iterable {..... public void append(T item) { // append item to the tail Node newNode = new Node (item, null); if (head == null) { head = newNode; return; } Node tail = head; while (tail.next != null) tail = tail.next; tail.next = newNode; }..... append We have to find the tail. XXX head tail

11 12/18/2015ITK 27511 public class SingleLinkedList implements Iterable {..... public T get(int at) { if (head == null || at < 0) return null;// nothing to return Node theNode = head; int count = 0; while (count < at) { theNode = theNode.next; if (theNode == null) return null; count++; } return theNode.data; }..... get There is no index, we have to count. XXX head theNode X

12 12/18/2015ITK 27512 public class SingleLinkedList implements Iterable {..... public int indexOf(T data) { if (head == null) return -1; // nothing there Node theNode = head; int count = 0; while (!theNode.data.equals(data)) { theNode = theNode.next; if (theNode == null) return -1; count++; } return count; }..... indexof There is no index, we have to count.

13 12/18/2015ITK 27513 public class SingleLinkedList implements Iterable {..... public T set(int at, T data) { // return the previous value; if (head == null || at < 0) return null; // nothing to set Node theNode = head; int count = 0; while (count < at) { theNode = theNode.next; if (theNode == null) return null; count++; } T oldValue = theNode.data; theNode.data = data; return oldValue; }..... set There is no index, we have to count.

14 12/18/2015ITK 27514 public class SingleLinkedList implements Iterable {..... public T insert(int at, T item) { // return item if success if (at == 0) { add(item); return item; // always success; } if (head == null || at < 0) return null; // nothing to do Node prevNode = head; int count = 1; // while (count < at) { prevNode = prevNode.next; if (prevNode == null) return null; count++; } /***** newNode will be inserted after prevNode; *** Node newNode = new Node (item,null); newNode.next = prevNode.next; prevNode.next = newNode; return item; }..... insert We need to know the previous node at new 012 head 3 X prev prevNode

15 12/18/2015ITK 27515 public class SingleLinkedList implements Iterable {..... public T remove(int at) { if (head == null || at < 0) return null; Node theNode=head, prevNode=null; if (at == 0) { head = head.next; return theNode.data; } int count = 0; while (count < at) { prevNode = theNode; theNode = theNode.next; if (theNode == null) return null; count++; } prevNode.next = theNode.next; return theNode.data; }..... remove We need to know the previous node at ? theNode prevNode

16 12/18/2015ITK 27516 public class SingleLinkedList implements Iterable {..... public Object[] toArray(Object[] a) { //a must have the right size int arraySize = size(); Node currentNode = head; for (int i = 0; i<arraySize; i++) { a[i] = currentNode.data; currentNode = currentNode.next; } return a; }..... toArray public Object[] toArray(Object[] a) { int arraySize = size(); a = new Object[arraySize]; Node currentNode = head; for (int i = 0; i<arraySize; i++) { a[i] = currentNode.data; currentNode = currentNode.next; } return a; }


Download ppt "12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface."

Similar presentations


Ads by Google