Home > CS2400: Data Structures and Advanced Programming > List ADTList ADT
List: Way to organize data.
List v.s. Array:
- List is 1-indexed (in this class).
- Array has limits.
- e.g., adding/removing from the middle
.add(1, "Amy");
list.add(2, "Elias");
list.add(2, "Bob");
list.add(3, "Drew"); list
Pseudocode | Description |
---|---|
add(newEntry) | Add an item to the end of the list |
add(newPosition, newEntry) | Add an item anywhere in the list |
remove(givenPosition) | Remove an item anywhere in the list |
clear() | Remove all entries from the list |
replace(givenPosition, newEntry) | Replace an element at an index |
getEntry(givenPosition) | Get the entry at a position |
toArray() | Return list as an array |
contains(anEntry) | Returns whether the list contains an entry |
getLength() | Returns number of items in the list |
isEmpty() | Returns whether list is empty |
public interface ListInterface<T> {
void add(T newEntry)
void add(int newPosition, T newEntry)
remove(int givenPosition)
T void clear()
replace(int givenPosition, T newEntry)
T getEntry(int givenPosition)
T [] toArray()
Tboolean contains(T anEntry)
int getLength()
boolean isEmpty()
}
List
void add(int index, T newEntry);
remove(int index);
T void clear();
set (int index, T anEntry);
T get (int index);
T boolean contains(Object anEntry);
int size();
boolean isEmpty();
Note: If you implement the iterator as a separate class rather than an inner-class, you need to finagle with the list’s methods.
Inner-Class Iterator
public interface ListInterface<T> extends Iterable<T>
public Iterator<T> iterator() {
// return the private iterator
}
private class IteratorForLinkedList implements Iterator<T> {
private Node nextNode;
private IteratorForLinkedList() {
= head;
nextNode }
//
public T next
}