FileDocCategorySizeDatePackage
LinkedList.javaAPI DocExample4834Sat Jun 02 02:42:14 BST 2001None

LinkedList

public class LinkedList extends Object
This class implements a linked list that can contain any type of object that implements the nested Linkable interface. Note that the methods are all synchronized, so that it can safely be used by multiple threads at the same time.

Fields Summary
Linkable
head
This is the only field of the class. It holds the head of the list
Constructors Summary
Methods Summary
public synchronized LinkedList$LinkablegetHead()
Return the first node in the list

 return head; 
public synchronized voidinsertAtHead(LinkedList$Linkable node)
Insert a node at the beginning of the list

    node.setNext(head);
    head = node;
  
public synchronized voidinsertAtTail(LinkedList$Linkable node)
Insert a node at the end of the list

    if (head == null) head = node;
    else {
      Linkable p, q;
      for(p = head; (q = p.getNext()) != null; p = q);
      p.setNext(node);
    }
  
public synchronized voidremove(LinkedList$Linkable node)
Remove a node matching the specified node from the list. Use equals() instead of == to test for a matched node.

    if (head == null) return;
    if (node.equals(head)) { 
      head = head.getNext(); 
      return;
    }
    Linkable p = head, q = null;
    while((q = p.getNext()) != null) {
      if (node.equals(q)) {
        p.setNext(q.getNext());
        return;
      }
      p = q;
    }
  
public synchronized LinkedList$LinkableremoveFromHead()
Remove and return the node at the head of the list

    Linkable node = head;
    if (node != null) {
      head = node.getNext();
      node.setNext(null);
    }
    return node;
  
public synchronized LinkedList$LinkableremoveFromTail()
Remove and return the node at the end of the list

    if (head == null) return null;
    Linkable p = head, q = null, next = head.getNext();
    if (next == null) {
      head = null;
      return p;
    }
    while((next = p.getNext()) != null) { 
      q = p; 
      p = next;
    }
    q.setNext(null);
    return p;