A simple Doubly Linked list class, designed to avoid O(n) behaviour on insert and delete. : Link List « Collections Data Structure « Java






A simple Doubly Linked list class, designed to avoid O(n) behaviour on insert and delete.

A simple Doubly Linked list class, designed to avoid O(n) behaviour on insert and delete.
      

/*

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

/**
 * A simple Doubly Linked list class, designed to avoid
 * O(n) behaviour on insert and delete.
 *
 * @author <a href="mailto:thomas.deweese@kodak.com">Thomas DeWeese</a>
 * @version $Id: DoublyLinkedList.java 475685 2006-11-16 11:16:05Z cam $
 */
public class DoublyLinkedList {

    /**
     * Basic doubly linked list node interface.
     */
        public static class Node {
                private Node next = null;
                private Node prev = null;
                        
                public final Node getNext() { return next; }
                public final Node getPrev() { return prev; }
                                                
                protected final void setNext(Node newNext) { next = newNext; }
                protected final void setPrev(Node newPrev) { prev = newPrev; }

        /**
         * Unlink this node from it's current list...
         */
                protected final void unlink() {
                        if (getNext() != null)
                                getNext().setPrev(getPrev());
                        if (getPrev() != null)
                                getPrev().setNext(getNext());
                        
                        setNext(null);
                        setPrev(null);
                }
                                                
        /**
         * Link this node in, infront of nde (unlinks it's self
         * before hand if needed).
         * @param nde the node to link in before.
         */
                protected final void insertBefore(Node nde) {
                        // Already here...
                        if (this == nde) return;

                        if (getPrev() != null)
                unlink();
                        
                        // Actually insert this node...
                        if (nde == null) {
                                // empty lst...
                                setNext(this);
                                setPrev(this);
                        } else {
                                setNext(nde);
                                setPrev(nde.getPrev());
                                nde.setPrev(this);
                if (getPrev() != null)
                    getPrev().setNext(this);
                        }
                }
        }


    private Node head = null;
    private int  size = 0;
                        
    public DoublyLinkedList() {}
                        
    /**
     * Returns the number of elements currently in the list.
     */
    public synchronized int getSize() { return size; }

    /**
     * Removes all elements from the list.
     */
    public synchronized void empty() {
        while(size > 0) pop();
    }
                        
    /**
     * Get the current head element
     * @return The current 'first' element in list.
     */
    public Node getHead() { return head; }
    /**
     * Get the current tail element
     * @return The current 'last' element in list.
     */
    public Node getTail() { return head.getPrev(); }

    /**
     * Moves <tt>nde</tt> to the head of the list (equivilent to
     * remove(nde); add(nde); but faster.
     */
    public void touch(Node nde) {
        if (nde == null) return;
        nde.insertBefore(head);
        head = nde;
    }

    public void add(int index, Node nde) {
        if (nde == null) return;
        if (index == 0) {
              // This makes it the first element in the list.
            nde.insertBefore(head);
            head = nde;
        } else if (index == size) {
              // Because the list is circular this
              // makes it the last element in the list.
            nde.insertBefore(head);
        } else {
            Node after = head;
            while (index != 0) {
                after = after.getNext();
                index--;
            }
            nde.insertBefore(after);
        }
        size++;
    }

    /**
     * Adds <tt>nde</tt> to the head of the list.
     * In perl this is called an 'unpop'.  <tt>nde</tt> should
     * not currently be part of any list.
     * @param nde the node to add to the list.
     */
    public void add(Node nde) {
        if (nde == null) return;
        nde.insertBefore(head);
        head = nde;
        size++;
    }
                
        /**
     * 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.
     */
    public void remove(Node nde) {
        if (nde == null) return;
        if (nde == head) {
            if (head.getNext() == head) 
                head = null;  // Last node...
            else
                head = head.getNext();
        }
        nde.unlink();
        size--;
    }

    /**
     * Removes 'head' from list and returns it. Returns null if list is empty.
     * @return current head element, next element becomes head.
     */
    public Node pop() {
        if (head == null) return null;
                        
        Node nde = head;
        remove(nde);
        return nde;
    }

    /**
     * Removes 'tail' from list and returns it. Returns null if list is empty.
     * @return current tail element.
     */
    public Node unpush() {
        if (head == null) return null;
                        
        Node nde = getTail();
        remove(nde);
        return nde;
    }



    /**
     * Adds <tt>nde</tt> to tail of list
     */
    public void push(Node nde) {
        nde.insertBefore(head);
        if (head == null) head = nde;
        size++;
    }

    /**
     * Adds <tt>nde</tt> to head of list
     */
    public void unpop(Node nde) {
        nde.insertBefore(head);
        head = nde;
        size++;
    }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Use for each loop to go through elements in a linkedlist
2.Use addFirst method to add value to the first position in a linked list
3.To insert an object into a specific position into the list, specify the index in the add method
4.Updating LinkedList Items
5.Convert LinkedList to Array with zero length array
6.Convert LinkedList to Array with full length array
7.Checking what item is first in line without removing it: element
8.Removing the first item from the queue: poll
9.Convert a LinkedList to ArrayList
10.Add elements at beginning and end of LinkedList Java example
11.Check if a particular element exists in LinkedList Java example
12.Create an object array from elements of LinkedList Java example
13.Get elements from LinkedList Java example
14.Get first and last elements from LinkedList Java example
15.Get SubList from LinkedList Java example
16.Iterate through elements of Java LinkedList using Iterator example
17.Remove all elements or clear LinkedList Java example
18.Iterate through elements of Java LinkedList using ListIterator example
19.Remove first and last elements of LinkedList Java example
20.Remove range of elements from LinkedList Java example
21.Remove specified element from LinkedList Java example
22.Replace an Element of LinkedList Java example
23.Search elements of LinkedList Java example
24.Add or insert an element to ArrayList using Java ListIterator Example
25.Finding an Element in a Sorted List
26.Create a list with an ordered list of strings
27.Search for a non-existent element
28.Use an Iterator to cycle through a collection in the forward direction.
29.Implementing a Queue with LinkedList
30.Implementing a Stack
31.Using a LinkedList in multi-thread
32.Convert Collection to ArrayList
33.Wrap queue to synchronize the methods
34.Making a stack from a LinkedListMaking a stack from a LinkedList
35.Single linked list
36.Double LinkedList
37.Doubly Linked listDoubly Linked list
38.A class for you to extend when you want object to maintain a doubly linked list
39.A List helper class that attempts to avoid unneccessary List creation.
40.This program demonstrates operations on linked lists
41.Simple linked list class which uses a Comparator to sort the nodes.
42.This implementation of LinkedList that is optimized for element removal.