ArrayList<T> and LinkedList<T>
(T is a type
variable):
| Operation signature | Description |
|---|---|
boolean add(T item) | Place a new item at the end of the list |
void add(int index, T item) | Place a new item at
position index of the list; don't lose any data |
T set(int index, T item) | Set item
index of the list.
Overwrites existing item in the list
|
boolean remove(T item) | Remove first instance of
item from the list. |
E remove(int index) | Remove item at position
index from the
list. Old item at index+1 is now item at
index, etc.
|
boolean contains(T item) | Returns true if the item is in the list |
E get(int index) |
Retrieve item at position index from the list |
Iterator<T> iterator() |
Returns an iterator pointing to the first item in the list |
Iterator: | |
boolean hasNext() | Are there more items |
E next() | Retrieve next item |
void remove() | Remove item (just returned by next) |
ArrayList<T>
add is called,
by doing the following:
LinkedList<T>
+-------+ +-------+ +--------+ +--->| 3 | o---->| 1 | o---->| 5 |NULL| | +-------+ +-------+ +--------+ | head


public class Node
{
// constructors
public Node()
{
item = 0; next = null;
}
public Node(int val)
{
item = val; next = null;
}
public Node(int val, Node ptr)
{
item = val; next = ptr;
}
// data members
public int item; // data type of 'item' is int
public Node next;
}
public class Node<E>
{
public Node()
{
item = null; next = null;
}
public Node(E theElement)
{
item = theElement; next = null;
}
public Node(E theElement, Node ptr)
{
item = theElement; next = ptr;
}
// data members
public E item; // data type is E (generic place holder symbol)
public Node next;
}
public static void main(String[] args)
{
Node<Integer> p1 = new Node<Integer>(3);
Node<Integer> p2 = new Node<Integer>(1);
Node<Integer> p3 = new Node<Integer>(5);
p1.next = p2; // p1's next points to where p2 points to
p2.next = p3; // p2's next points to where p3 points to
Node<Integer> head = p1; // the beginning of the list
for (Node<Integer> ptr = head; ptr != null; ptr = ptr.next)
System.out.println(ptr.item); // prints the data portion
Integer key = new Integer(5); // key to look for in the list, print all occurrences
for (Node<Integer> ptr = head; ptr != null; ptr = ptr.next) {
if (ptr.item.equals(key)) { // equals() to compare
System.out.println(ptr.item);
}
}
+-------+ +-------+ +--------+
+-->| 3 | o---->| 1 | o | +->| 5 |null|
| +-------+ +-----|-+ | +--------+
+-|-+ | +-------+ |
head | o | +-->| 2 | o---+
+---+ +-------+
// get the reference to the node with 1 first Node<Integer> ptr = head.next; // create a new node with new element Node<Integer> temp = new Node<Integer>(2); // insert after ptr temp.next = ptr.next; // temp's next points to where ptr's next (5) ptr.next = temp; // ptr's next points to temp(2), the new element
+---------------+
| |
+-------+ +-----|-+ +-------+ | +--------+
+->| 3 | o---->| 1 | o | | 2 | o | +->| 5 |null|
| +-------+ +-------+ +-------+ +--------+
+-|-+
head | o |
+---+
Node<Integer> ptr = head.next; // points to 1 Node<Integer> temp = ptr.next; // points to 2 ptr.next = temp.next; // ptr's next skips 2, to 5
EXERCISE: Write a code which
public class LinkedList<E extends Comparable<E>> implements Iterable<E>
{
private Node head; // reference to the first node
/*******************
* nested class Node
*******************/
private class Node
{
public E item;
public Node next;
//...
}
/***************************
* nested class ListIterator
***************************/
private class ListIterator implements Iterator<E>
{
private Node current;
//...
}
+---+---+ +---+---+ +---+---+ +---+----+
+-->| 3 | o--->| 1 | o--->| 2 | o--->| 5 |null|
| +---+---+ +---+---+ +---+---+ +---+----+
+-|-+ ^
head | o | |
+---+ +-----------|---+
| +-|-+ |
| current | o | |
| +---+ |
+---------------+
a ListIterator object
import java.util.Iterator;
public class LinkedList<E extends Comparable<E>> implements Iterable<E>
{
// instance data member of list
private Node head;
private int N; // number of elements stored in the list
/**
* nested class Node
*/
private class Node
{
// instance data members of Node
public E item;
public Node next;
// constructors for Node
public Node()
{
item = null; next = null;
}
public Node(E e, Node ptr)
{
item = e; next = ptr;
}
}// end class Node
/***************************
* nested class ListIterator
***************************/
private class ListIterator implements Iterator<E>
{
// instance data member of ListIterator
private Node current;
// constructors for ListIterator
public ListIterator()
{
current = head; // head in the enclosing list
}
public boolean hasNext()
{
return current != null;
}
public E next()
{
E ret = current.item;
current = current.next;
return ret;
}
public void remove() { /* omitted because optional */ }
}// end class ListIterator
/***** back to class LinkedList *******/
public LinkedList()
{
head = null; N = 0;
}
public Iterator<E> iterator( )
{
return new ListIterator();
}
public void add(E e)
{
// This code will be edited during the class.
// For now, it pushes a new node in the front
// But first check the parameter is null.. if so, throw an exception.
if (e == null)
throw new NullPointerException();
// For now, the element is pushed in the front, and N is incremented.
head = new Node(e, head);
++N;
}
public void remove(E e)
{
// This code will be edited during the class.
// Removes the node with the parameter item if it exists, and decrements N.
// Does nothing if the item doesn't exist, or the list is empty.
// Throws a NullPointerException if the parameter item is null.
}
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
/**
* main()
*/
public static void main(String[] args)
{
String names[ ] = {"Uno", "Dos", "Tres", "Cuatro", "Cinco"};
LinkedList<String> lst = new LinkedList<String>();
for (String s : names)
lst.add(s); // add in the Bag
// (1) Use an Iterator to iterate/traverse the bag
System.out.print("Print using iterator -- ");
Iterator<String> it = lst.iterator();
while (it.hasNext())
{
String n = it.next();
System.out.printf("%s ", n);
}
System.out.printf("%n");
// (2) Another way by the for-each syntax
System.out.print("Print using foreach -- ");
for (String n : lst)
System.out.printf("%s ", n);
System.out.printf("%n%n");
}
}
/* Output
Print using iterator -- Cinco Cuatro Tres Dos Uno
Print using foreach -- Cinco Cuatro Tres Dos Uno
*/
EXERCISE: Some exercise questions in the textboo (p. 164-165):

public class Node<E>
{
// constructors
public Node() { data = null; prev = next = null; }
public Node(E n) { data = n; prev = next = null; }
// data members
public Node prev, next;
public E data;
}

head = new Node(); tail = new Node(); head.next = tail; tail.prev = head;

Node ptr;
for (ptr = head.next; // start from the node after dummy head
ptr != tail; // when ptr points to dummy tail, terminate.
ptr = ptr.next) // advance ptr to the next node
System.out.print(ptr.data + " ");

// BE CAREFUL WITH THE ORDER!! Node temp = new Node(7); temp.prev = ptr.prev; // (1) ptr.prev.next = temp; // (2) temp.next = ptr; // (3) ptr.prev = temp; // (4)

ptr.prev.next = ptr.next; // (1) ptr.next.prev = ptr.prev; // (2)