12/18/2015itk 2751 collectionlist a bstractsequentiallist vector stack linkedlist {interface}...

16
06/17/22 ITK 275 1 Collection List AbstractSequentia lList Vector Stack LinkedList {interfa ce} AbstractList {abstract} ArrayList Java Provides a List interface for several implementations {interfa ce} {abstract}

Upload: roger-hart

Post on 18-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

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

04/21/23 ITK 275 1

Collection List

AbstractSequentialListVector

StackLinkedList

{interface}

AbstractList{abstract}

ArrayList

Java Provides a List interface for several implementations

{interface}

{abstract}

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

04/21/23 ITK 275 2

Interface for the user ArrayListLinkedLis

tVectorStack

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()

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

04/21/23 ITK 275 3

API ArrayList<T>

ArrayList1

2

3

4

5

6

7

8

Application Programming Interface

It’s a generic type,<E> 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()

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

04/21/23 ITK 275 4

API LinkedList<T>

1

2

3

4

5

6

7

8

LinkedList

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()

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

04/21/23 ITK 275 5

My SingleLinkedList<T>

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 informationand structures are protected.

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

04/21/23 ITK 275 6

What we need for single linked lists

1 2 3 4

8

head tail

insert

remove

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

04/21/23 ITK 275 7

Internal classes

public class A { private ....

Public ....

}

private static class C { .....}

public class B { private ....

Public ....

}

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

04/21/23 ITK 275 8

public class SingleLinkedList<T> implements Iterable {

/***** This is an inner class for internal nodes ********/ private static 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;

public SingleLinkedList() {head = null;

}..............}

Using an inner class for the internal nodes

E E

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

04/21/23 ITK 275 9

public class SingleLinkedList<T> implements Iterable {.......... public void add(T item) { // add item to the head

head = new Node<T>(item,head); }

public int size() { int count = 0; Node<T> next=head;

while (next != null) { count++; next = next.next;

} return count; }.....

Two easy methods

E E

head

2 3 4

head

next

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

04/21/23 ITK 275 10

public class SingleLinkedList<T> implements Iterable {..... public void append(T item) { // append item to the tail

Node<T> newNode = new Node<T>(item, null);if (head == null) { head = newNode;return;}

Node<T> tail = head;while (tail.next != null) tail = tail.next;tail.next = newNode;

}.....

append We have to find the tail.

X X X

head

tail

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

04/21/23 ITK 275 11

public class SingleLinkedList<T> implements Iterable {..... public T get(int at) {

if (head == null || at < 0) return null;// nothing to return

Node<T> 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.

X X X

head

theNode

X

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

04/21/23 ITK 275 12

public class SingleLinkedList<T> implements Iterable {.......... 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; }..........

indexof There is no index, we have to count.

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

04/21/23 ITK 275 13

public class SingleLinkedList<T> implements Iterable {.......... public T set(int at, T data) { // return the previous value;

if (head == null || at < 0) return null; // nothing to set

Node<T> 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.

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

04/21/23 ITK 275 14

public class SingleLinkedList<T> 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 doNode<T> prevNode = head;int count = 1; // while (count < at) { prevNode = prevNode.next; if (prevNode == null) return null; count++;}/***** newNode will be inserted after prevNode; ***Node<T> newNode = new Node<T>(item,null);newNode.next = prevNode.next;prevNode.next = newNode;return item;

}.....

insert We need to know the previous node

at

new

0 1 2

head

3

X

prev

prevNode

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

04/21/23 ITK 275 15

public class SingleLinkedList<T> implements Iterable {..... public T remove(int at) {

if (head == null || at < 0) return null;

Node<T> 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

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

04/21/23 ITK 275 16

public class SingleLinkedList<T> implements Iterable {..... public Object[] toArray(Object[] a) { //a must have the right size

int arraySize = size(); Node<T> 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<T> currentNode = head;for (int i = 0; i<arraySize; i++) { a[i] = currentNode.data; currentNode = currentNode.next;}return a;

}