void addFirst(Object obj) void addLast(Object obj) Object getFirst() Object getSecond() Object removeFirst() Object removeLast()
LinkedList list = . . . ListIterator iterator = list.listIterator();
iterator.next();
if (iterator.hasNext()) iterator.next();
while iterator.hasNext() { Object obj = iterator.next(); //do something with the object }
iterator.add("Juliet");
while (iterator.hasNext()) { Object obj = iterator.next(); if (obj fulfills condition) iterator.remove(); }
Sample program inserts elements into a list, then iterates through the list, adding and removing elements,then prints the list
class LinkedList { private class Link { public Object data; public Link next; } }
class LinkedList
{
public LinkedList()
{
first = null;
}
public Object getFirst()
{
if (first == null)
throw new NoSuchElementException();
return first.data;
} . . . private Link first; }
class LinkedList { . . . public void addFirst(Object obj)
{
Link newLink = new Link();
newLink.data = obj;
newLink.next = first;
first = newLink;
} ... }
class LinkedList { . . . public Object removeFirst() { if (first == null) throw new NoSuchElementException(); Object obj = first.data; first = first.next; return obj; } . . . }
class LinkedList { . . . public ListIterator listIterator() { return new LinkedListIterator(); } private class LinkedListIterator implements ListIterator { public LinkedListIterator() { position = null; previous = null; } . . . private Link position; private Link previous; } . . . }
private class LinkedListIterator implements ListIterator { . . . public Object next() { if (!hasNext()) throw new NoSuchElementException(); previous = position; // remember for remove if (position == null) position = first; else position = position.next; return position.data; } . . . }
private class LinkedListIterator implements ListIterator { . . . public boolean hasNext() { if (position == null) return first != null; else return position.next != null; } . . . }
private class LinkedListIterator implements ListIterator { . . . public void remove() { if (position == first) { removeFirst(); position = first; } else { if (previous == null) throw new IllegalStateException(); previous.next = position.next; position = previous; } previous = null; } . . . }
private class LinkedListIterator implements ListIterator { . . . public void set(Object obj) { if (position == null) throw new NoSuchElementException(); position.data = obj; } . . . }
private class LinkedListIterator implements ListIterator { . . . public void add(Object obj) { if (position == null) { addFirst(obj); position = first; } else { Link newLink = new Link(); newLink.data = obj; newLink.next = position.next; position.next = newLink; position = newLink; } previous = null; } . . . }
public class ArrayList { public Object get(int index) {. . . } public void set(int index, Object value) {. . . } }
public class LinkedList { public ListIteratior listIterator() {. . . } . . . } public interface ListIteratior { Object next(); boolean hasNext(); void add(Object value); void remove(); void set(Object value); . . . }
Stack s = new Stack(); s.push("A"); s.push("B"); s.push("C"); //the following loop prints C, B, A int i = s.size(); while (i > 0) { System.out.println(s.pop()); i--; }
A stack can be visualized as a stack of books. You place books on top and remove from the top.
public class Queue { /** Constructs an empty queue */ public Queue() { list = new LinkedList(); } /** Adds an item to the tail of the queue @param x the item to add */ public void add(Object x) { list.addLast(x); } /** Removes an item from the head of the queue @return the removed item */ public Object remove() { return list.removeFirst(); } /** Gets the number of items in the queue @return the size */ public int size() { return list.size() } private LinkedList list; }
A Queue can be visualized as a queue of people. People join the tail of the queue and wait until they reach the head.