FileDocCategorySizeDatePackage
DoublyLinkedList.javaAPI DocphoneME MR2 API (J2ME)7579Wed May 02 18:00:34 BST 2007com.sun.perseus.util

DoublyLinkedList

public class DoublyLinkedList extends Object
A simple Doubly Linked list class, designed to avoid O(n) behaviour on insert and delete.
author
Thomas DeWeese
version
$Id: DoublyLinkedList.java,v 1.2 2006/04/21 06:35:47 st125089 Exp $

Fields Summary
private Node
head
The list's head. The previous node is the tail of the list
private int
size
The list's size
Constructors Summary
public DoublyLinkedList()
Default constructor

    
           
      
    
Methods Summary
public voidadd(com.sun.perseus.util.DoublyLinkedList$Node nde)
Adds nde to the head of the list. In perl this is called an 'unpop'. nde should not currently be part of any list.

param
nde the node to add to the list.

        nde.insertBefore(head);
        head = nde;
        size++;
    
public voidempty()
Removes all elements from the list.

        while (size > 0) {
            pop();
        }
    
public com.sun.perseus.util.DoublyLinkedList$NodegetHead()
Get the current head element

return
The current 'first' element in list.

 
        return head; 
    
public intgetSize()

return
the number of elements currently in the list.

 
        return size; 
    
public com.sun.perseus.util.DoublyLinkedList$NodegetTail()
Get the current tail element

return
The current 'last' element in list.

 
        if (head != null) {
            return head.getPrev(); 
        } else {
            return null;
        }
    
public com.sun.perseus.util.DoublyLinkedList$Nodepop()
Removes 'head' from list and returns it. Returns null if list is empty.

return
current head element, next element becomes head.

        if (head == null) {
            return null;
        }
            
        Node nde = head;
        remove(nde);
        return nde;
    
public voidpush(com.sun.perseus.util.DoublyLinkedList$Node nde)

param
nde the Node to add to tail of list

        nde.insertBefore(head);
        if (head == null) {
            head = nde;
        }
        size++;
    
public voidremove(com.sun.perseus.util.DoublyLinkedList$Node nde)
Removes nde from the list it is part of (should be this one, otherwise results are undefined). If nde is the current head element, then the next element becomes head, if there are no more elements the list becomes empty.

param
nde Node to remove.

        if (nde == head) {
            if (head.getNext() == head) {
                head = null;  // Last node...
            } else {
                head = head.getNext();
            }
        }
        nde.unlink();
        size--;
    
public voidunpop(com.sun.perseus.util.DoublyLinkedList$Node nde)

param
nde the Node to put to the head of list

        nde.insertBefore(head);
        head = nde;
        size++;
    
public com.sun.perseus.util.DoublyLinkedList$Nodeunpush()
Removes 'tail' from list and returns it. Returns null if list is empty.

return
current tail element.

        if (head == null) {
            return null;
        }
            
        Node nde = getTail();
        remove(nde);
        return nde;