现在的位置: 首页 > 综合 > 正文

java数据结构链表

2013年08月07日 ⁄ 综合 ⁄ 共 17510字 ⁄ 字号 评论关闭

 看了很多事还是weiss的书有效.他不知给出了方法还给出了完整代码并且给出了实例,就好比哲学家之于马克思.

大部分哲学家在解释世界而马克思提出了改造世界.

手头终于搞定了两种链表.单链表和双链表.

明天彻底改造.

第一个原始双链表上面有两个小瑕疵.代码是在他博客上下的.我改正了,目前至少编译出来了.呵呵

Code:
  1. import java.util.*;  
  2.   
  3. /** 
  4.  * LinkedList class implements a doubly-linked list. 
  5.  */  
  6. public abstract class LinkedList<AnyType> extends AbstractCollection<AnyType> implements List<AnyType>, Queue<AnyType>  
  7. {  
  8.     /** 
  9.      * Construct an empty LinkedList. 
  10.      */  
  11.     public LinkedList( )  
  12.     {  
  13.         clear( );  
  14.     }  
  15.       
  16.     /** 
  17.      * Construct a LinkedList with same items as another Collection. 
  18.      */  
  19.     public LinkedList( Collection<? extends AnyType> other )  
  20.     {  
  21.         clear( );  
  22.         for( AnyType val : other )  
  23.             add( val );  
  24.     }  
  25.       
  26.     /** 
  27.      * Change the size of this collection to zero. 
  28.      */  
  29.     public void clear( )  
  30.     {  
  31.         beginMarker = new Node<AnyType>( nullnullnull );  
  32.         endMarker = new Node<AnyType>( null, beginMarker, null );  
  33.         beginMarker.next = endMarker;  
  34.           
  35.         theSize = 0;  
  36.         modCount++;  
  37.     }  
  38.       
  39.     /** 
  40.      * Returns the number of items in this collection. 
  41.      * @return the number of items in this collection. 
  42.      */  
  43.     public int size( )  
  44.     {  
  45.         return theSize;  
  46.     }  
  47.       
  48.       
  49.     /** 
  50.      * Tests if some item is in this collection. 
  51.      * @param x any object. 
  52.      * @return true if this collection contains an item equal to x. 
  53.      */  
  54.     public boolean contains( Object x )  
  55.     {  
  56.         return findPos( x ) != NOT_FOUND;  
  57.     }   
  58.       
  59.     /** 
  60.      * Returns the position of first item matching x in this collection, 
  61.      * or NOT_FOUND if not found. 
  62.      * @param x any object. 
  63.      * @return the position of first item matching x in this collection, 
  64.      * or NOT_FOUND if not found. 
  65.      */  
  66.     private Node<AnyType> findPos( Object x )  
  67.     {  
  68.         for( Node<AnyType> p = beginMarker.next; p != endMarker; p = p.next )  
  69.             if( x == null )  
  70.             {  
  71.                 if( p.data == null )  
  72.                     return p;  
  73.             }  
  74.             else if( x.equals( p.data ) )  
  75.                 return p;  
  76.                   
  77.         return NOT_FOUND;  
  78.     }  
  79.       
  80.     /** 
  81.      * Adds an item to this collection, at the end. 
  82.      * @param x any object. 
  83.      * @return true. 
  84.      */  
  85.     public boolean add( AnyType x )  
  86.     {  
  87.         addLast( x );     
  88.         return true;           
  89.     }  
  90.       
  91.     /** 
  92.      * Adds an item to this collection, at specified position. 
  93.      * Items at or after that position are slid one position higher. 
  94.      * @param x any object. 
  95.      * @param idx position to add at. 
  96.      * @throws IndexOutOfBoundsException if idx is not between 0 and size(), inclusive. 
  97.      */  
  98.     public void add( int idx, AnyType x )  
  99.     {  
  100.         Node<AnyType> p = getNode( idx, 0, size( ) );  
  101.         Node<AnyType> newNode = new Node<AnyType>( x, p.prev, p );  
  102.         newNode.prev.next = newNode;  
  103.         p.prev = newNode;           
  104.         theSize++;  
  105.         modCount++;  
  106.     }  
  107.       
  108.     /** 
  109.      * Adds an item to this collection, at front. 
  110.      * Other items are slid one position higher. 
  111.      * @param x any object. 
  112.      */      
  113.     public void addFirst( AnyType x )  
  114.     {  
  115.         add( 0, x );  
  116.     }  
  117.   
  118.     /** 
  119.      * Adds an item to this collection, at end. 
  120.      * @param x any object. 
  121.      */      
  122.     public void addLast( AnyType x )  
  123.     {  
  124.         add( size( ), x );  
  125.     }      
  126.       
  127.     /** 
  128.      * Returns the front item in the list. 
  129.      * @throws NoSuchElementException if the list is empty. 
  130.      */  
  131.     public AnyType element( )  
  132.     {  
  133.         return getFirst( );      
  134.     }  
  135.       
  136.     /** 
  137.      * Returns the first item in the list. 
  138.      * @throws NoSuchElementException if the list is empty. 
  139.      */  
  140.     public AnyType getFirst( )  
  141.     {  
  142.         if( isEmpty( ) )  
  143.             throw new NoSuchElementException( );  
  144.         return getNode( 0 ).data;      
  145.     }  
  146.       
  147.     /** 
  148.      * Returns the last item in the list. 
  149.      * @throws NoSuchElementException if the list is empty. 
  150.      */  
  151.     public AnyType getLast( )  
  152.     {  
  153.         if( isEmpty( ) )  
  154.             throw new NoSuchElementException( );  
  155.         return getNode( size( ) - 1 ).data;      
  156.     }  
  157.       
  158.     /** 
  159.      * Returns the item at position idx. 
  160.      * @param idx the index to search in. 
  161.      * @throws IndexOutOfBoundsException if index is out of range. 
  162.      */  
  163.     public AnyType get( int idx )  
  164.     {  
  165.         return getNode( idx ).data;  
  166.     }  
  167.           
  168.     /** 
  169.      * Changes the item at position idx. 
  170.      * @param idx the index to change. 
  171.      * @param newVal the new value. 
  172.      * @return the old value. 
  173.      * @throws IndexOutOfBoundsException if index is out of range. 
  174.      */  
  175.     public AnyType set( int idx, AnyType newVal )  
  176.     {  
  177.         Node<AnyType> p = getNode( idx );  
  178.         AnyType oldVal = p.data;  
  179.           
  180.         p.data = newVal;     
  181.         return oldVal;  
  182.     }  
  183.       
  184.     /** 
  185.      * Removes the front item in the queue. 
  186.      * @return the front item. 
  187.      * @throws NoSuchElementException if the list is empty. 
  188.      */  
  189.     public AnyType remove( )  
  190.     {  
  191.         return removeFirst( );      
  192.     }  
  193.       
  194.     /** 
  195.      * Removes the first item in the list. 
  196.      * @return the item was removed from the collection. 
  197.      * @throws NoSuchElementException if the list is empty. 
  198.      */  
  199.     public AnyType removeFirst( )  
  200.     {  
  201.         if( isEmpty( ) )  
  202.             throw new NoSuchElementException( );  
  203.         return remove( getNode( 0 ) );      
  204.     }  
  205.       
  206.     /** 
  207.      * Removes the last item in the list. 
  208.      * @return the item was removed from the collection. 
  209.      * @throws NoSuchElementException if the list is empty. 
  210.      */  
  211.     public AnyType removeLast( )  
  212.     {  
  213.         if( isEmpty( ) )  
  214.             throw new NoSuchElementException( );  
  215.         return remove( getNode( size( ) - 1 ) );      
  216.     }  
  217.       
  218.     /** 
  219.      * Removes an item from this collection. 
  220.      * @param x any object. 
  221.      * @return true if this item was removed from the collection. 
  222.      */  
  223.     public boolean remove( Object x )  
  224.     {  
  225.         Node<AnyType> pos = findPos( x );  
  226.           
  227.         if( pos == NOT_FOUND )  
  228.             return false;  
  229.         else  
  230.         {  
  231.             remove( pos );  
  232.             return true;  
  233.         }          
  234.     }  
  235.       
  236.       
  237.     /** 
  238.      * Gets the Node at position idx, which must range from 0 to size( )-1. 
  239.      * @param idx index to search at. 
  240.      * @return internal node corrsponding to idx. 
  241.      * @throws IndexOutOfBoundsException if idx is not between 0 and size()-1, inclusive. 
  242.      */  
  243.     private Node<AnyType> getNode( int idx )  
  244.     {  
  245.         return getNode( idx, 0, size( ) - 1 );  
  246.     }  
  247.       
  248.     /** 
  249.      * Gets the Node at position idx, which must range from lower to upper. 
  250.      * @param idx index to search at. 
  251.      * @param lower lowest valid index. 
  252.      * @param upper highest valid index. 
  253.      * @return internal node corrsponding to idx. 
  254.      * @throws IndexOutOfBoundsException if idx is not between lower and upper, inclusive. 
  255.      */  
  256.     private Node<AnyType> getNode( int idx, int lower, int upper )  
  257.     {  
  258.         Node<AnyType> p;  
  259.           
  260.         if( idx < lower || idx > upper )  
  261.             throw new IndexOutOfBoundsException( "getNode index: " + idx + "; size: " + size( ) );  
  262.               
  263.         if( idx < size( ) / 2 )  
  264.         {  
  265.             p = beginMarker.next;  
  266.             forint i = 0; i < idx; i++ )  
  267.                 p = p.next;              
  268.         }  
  269.         else  
  270.         {  
  271.             p = endMarker;  
  272.             forint i = size( ); i > idx; i-- )  
  273.                 p = p.prev;  
  274.         }   
  275.           
  276.         return p;  
  277.     }  
  278.       
  279.     /** 
  280.      * Removes an item from this collection. 
  281.      * @param idx the index of the object. 
  282.      * @return the item was removed from the collection. 
  283.      */  
  284.     public AnyType remove( int idx )  
  285.     {  
  286.         return remove( getNode( idx ) );  
  287.     }  
  288.       
  289.     /** 
  290.      * Removes the object contained in Node p. 
  291.      * @param p the Node containing the object. 
  292.      * @return the item was removed from the collection. 
  293.      */  
  294.     private AnyType remove( Node<AnyType> p )  
  295.     {  
  296.         p.next.prev = p.prev;  
  297.         p.prev.next = p.next;  
  298.         theSize--;  
  299.         modCount++;  
  300.           
  301.         return p.data;  
  302.     }  
  303.       
  304.     /** 
  305.      * Obtains an Iterator object used to traverse the collection. 
  306.      * @return an iterator positioned prior to the first element. 
  307.      */  
  308.     public Iterator<AnyType> iterator( )  
  309.     {  
  310.         return new LinkedListIterator( 0 );  
  311.     }  
  312.       
  313.     /** 
  314.      * Obtains a ListIterator object used to traverse the collection bidirectionally. 
  315.      * @return an iterator positioned prior to the requested element. 
  316.      * @param idx the index to start the iterator. Use size() to do complete 
  317.      * reverse traversal. Use 0 to do complete forward traversal. 
  318.      * @throws IndexOutOfBoundsException if idx is not between 0 and size(), inclusive. 
  319.      */  
  320.     public ListIterator<AnyType> listIterator( int idx )  
  321.     {  
  322.         return new LinkedListIterator( idx );  
  323.     }  
  324.   
  325.     /** 
  326.      * This is the implementation of the LinkedListIterator. 
  327.      * It maintains a notion of a current position and of 
  328.      * course the implicit reference to the LinkedList. 
  329.      */  
  330.     private class LinkedListIterator implements ListIterator<AnyType>  
  331.     {  
  332.         private Node<AnyType> current;  
  333.         private Node<AnyType> lastVisited = null;  
  334.         private boolean lastMoveWasPrev = false;  
  335.         private int expectedModCount = modCount;  
  336.           
  337.         public LinkedListIterator( int idx )  
  338.         {  
  339.             current = getNode( idx, 0, size( ) );    
  340.         }  
  341.           
  342.         public boolean hasNext( )  
  343.         {  
  344.             if( expectedModCount != modCount )  
  345.                 throw new ConcurrentModificationException( );  
  346.             return current != endMarker;  
  347.         }  
  348.           
  349.         public AnyType next( )  
  350.         {  
  351.             if( !hasNext( ) )  
  352.                 throw new NoSuchElementException( );   
  353.                      
  354.             AnyType nextItem = current.data;  
  355.             lastVisited = current;  
  356.             current = current.next;  
  357.             lastMoveWasPrev = false;  
  358.             return nextItem;  
  359.         }  
  360.           
  361.         public void remove( )  
  362.         {  
  363.             if( expectedModCount != modCount )  
  364.                 throw new ConcurrentModificationException( );  
  365.             if( lastVisited == null )  
  366.                 throw new IllegalStateException( );  
  367.                   
  368.             LinkedList.this.remove( lastVisited );  
  369.             lastVisited = null;  
  370.             if( lastMoveWasPrev )  
  371.                 current = current.next;  
  372.             expectedModCount++;         
  373.         }  
  374.           
  375.         public boolean hasPrevious( )  
  376.         {  
  377.             if( expectedModCount != modCount )  
  378.                 throw new ConcurrentModificationException( );  
  379.             return current != beginMarker.next;  
  380.         }  
  381.           
  382.         public AnyType previous( )  
  383.         {  
  384.             if( expectedModCount != modCount )  
  385.                 throw new ConcurrentModificationException( );  
  386.             if( !hasPrevious( ) )  
  387.                 throw new NoSuchElementException( );   
  388.                      
  389.             current = current.prev;  
  390.             lastVisited = current;  
  391.             lastMoveWasPrev = true;  
  392.             return current.data;  
  393.         }  
  394.   
  395.         @Override  
  396.         public void add(AnyType e) {  
  397.             // TODO Auto-generated method stub  
  398.               
  399.         }  
  400.   
  401.         @Override  
  402.         public int nextIndex() {  
  403.             // TODO Auto-generated method stub  
  404.             return 0;  
  405.         }  
  406.   
  407.         @Override  
  408.         public int previousIndex() {  
  409.             // TODO Auto-generated method stub  
  410.             return 0;  
  411.         }  
  412.   
  413.         @Override  
  414.         public void set(AnyType e) {  
  415.             // TODO Auto-generated method stub  
  416.               
  417.         }           
  418.     }  
  419.       
  420.     /** 
  421.      * This is the doubly-linked list node. 
  422.      */  
  423.     private static class Node<AnyType>  
  424.     {  
  425.         public Node( AnyType d, Node<AnyType> p, Node<AnyType> n )  
  426.         {  
  427.             data = d; prev = p; next = n;  
  428.         }  
  429.           
  430.         public AnyType data;  
  431.         public Node<AnyType>   prev;  
  432.         public Node<AnyType>   next;  
  433.     }  
  434.       
  435.     private final Node<AnyType> NOT_FOUND = null;  
  436.       
  437.     private int theSize;  
  438.     private Node<AnyType> beginMarker;  
  439.     private Node<AnyType> endMarker;  
  440.     private int modCount = 0;  
  441. }  

下面是单链表的东东.看起来复制.用起来更复杂.还有一个问题编译器报错但是可以编译.不知道为啥.数据结构部分

Code:
  1. // Basic node stored in a linked list  
  2. // Note that this class is not accessible outside  
  3. // of package weiss.nonstandard  
  4. //copy by blacksapper  
  5.   
  6. class ListNode<AnyType>  
  7. {  
  8.       // Constructors  
  9.     public ListNode( AnyType theElement )  
  10.     {  
  11.         this( theElement, null );  
  12.     }  
  13.   
  14.     public ListNode( AnyType theElement, ListNode<AnyType> n )  
  15.     {  
  16.         element = theElement;  
  17.         next    = n;  
  18.     }  
  19.   
  20.     public AnyType   element;  
  21.     public ListNode<AnyType> next;  
  22. }  

具体方法,提供安全的访问

Code:
  1. package LinkNodetest;  
  2.   
  3. import LinkNodetest.LinkedList;  
  4. import LinkNodetest.ListNode;  
  5.   
  6.   
  7. //LinkedListIterator class; maintains "current position"  
  8. //  
  9. //CONSTRUCTION: Package visible only, with a ListNode  
  10. //  
  11. //******************PUBLIC OPERATIONS*********************  
  12. //void advance( )        --> Advance  
  13. //boolean isValid( )     --> True if at valid position in list  
  14. //AnyType retrieve       --> Return item in current position  
  15.   
  16. /**  
  17. * Linked list implementation of the list iterator  
  18. *    using a header node.  
  19. @author Mark Allen Weiss  
  20. *@copy by blacksapper  
  21. @see LinkedList  
  22. */  
  23. public class LinkedListIterator<AnyType>  
  24. {  
  25.  /** 
  26.   * Construct the list iterator 
  27.   * @param theNode any node in the linked list. 
  28.   */  
  29.  LinkedListIterator( ListNode<AnyType> theNode )  
  30.  {  
  31.      current = theNode;  
  32.  }  
  33.   
  34.  /** 
  35.   * Test if the current position is a valid position in the list. 
  36.   * @return true if the current position is valid. 
  37.   */  
  38.  public boolean isValid( )  
  39.  {  
  40.      return current != null;  
  41.  }  
  42.   
  43.  /** 
  44.   * Return the item stored in the current position. 
  45.   * @return the stored item or null if the current position 
  46.   * is not in the list. 
  47.   */  
  48.  public AnyType retrieve( )  
  49.  {  
  50.      return isValid( ) ? current.element : null;  
  51.  }  
  52.   
  53.  /** 
  54.   * Advance the current position to the next node in the list. 
  55.   * If the current position is null, then do nothing. 
  56.   */  
  57.  public void advance( )  
  58.  {  
  59.      if( isValid( ) )  
  60.          current = current.next;  
  61.  }  
  62.   
  63.  ListNode<AnyType> current;    // Current position  
  64. }  

抽象方法和例子

Code:
  1. package LinkNodetest;  
  2.   
  3. import LinkNodetest.LinkedListIterator;  
  4.   
  5. //LinkedList class  
  6. //  
  7. //CONSTRUCTION: with no initializer  
  8. //Access is via LinkedListIterator class  
  9. //  
  10. //******************PUBLIC OPERATIONS*********************  
  11. //boolean isEmpty( )     --> Return true if empty; else false  
  12. //void makeEmpty( )      --> Remove all items  
  13. //LinkedListIterator zeroth( )  
  14. //                     --> Return position to prior to first  
  15. //LinkedListIterator first( )  
  16. //                     --> Return first position  
  17. //void insert( x, p )    --> Insert x after current iterator position p  
  18. //void remove( x )       --> Remove x  
  19. //LinkedListIterator find( x )  
  20. //                     --> Return position that views x  
  21. //LinkedListIterator findPrevious( x )  
  22. //                     --> Return position prior to x  
  23. //******************ERRORS********************************  
  24. //No special errors  
  25.   
  26. /**  
  27. * Linked list implementation of the list  
  28. *    using a header node.  
  29. * Access to the list is via LinkedListIterator.  
  30. @author Mark Allen Weiss  
  31. @param blacksapper  
  32. @see LinkedListIterator  
  33. */  
  34. public class LinkedList<AnyType>  
  35. {  
  36.  /** 
  37.   * Construct the list 
  38.   */  
  39.  public LinkedList( )  
  40.  {  
  41.      header = new ListNode<AnyType>( null );  
  42.  }  
  43.   
  44.  /** 
  45.   * Test if the list is logically empty. 
  46.   * @return true if empty, false otherwise. 
  47.   */  
  48.  public boolean isEmpty( )  
  49.  {  
  50.      return header.next == null;  
  51.  }  
  52.   
  53.  /** 
  54.   * Make the list logically empty. 
  55.   */  
  56.  public void makeEmpty( )  
  57.  {  
  58.      header.next = null;  
  59.  }  
  60.   
  61.  /** 
  62.   * Return an iterator representing the header node. 
  63.   */  
  64.  public LinkedListIterator<AnyType> createhead( )  
  65.  {  
  66.      return new LinkedListIterator<AnyType>( header );  
  67.  }  
  68.   
  69.  /** 
  70.   * Return an iterator representing the first node in the list. 
  71.   * This operation is valid for empty lists. 
  72.   */  
  73.  public LinkedListIterator<AnyType> first( )  
  74.  {  
  75.      return new LinkedListIterator<AnyType>( header.next );  
  76.  }  
  77.   
  78.  /** 
  79.   * Insert after p. 
  80.   * @param x the item to insert. 
  81.   * @param p the position prior to the newly inserted item. 
  82.   */  
  83.  public void insert( AnyType x, LinkedListIterator<AnyType> p )  
  84.  {  
  85.      if( p != null && p.current != null )  
  86.          p.current.next = new ListNode<AnyType>( x, p.current.next );  
  87.  }  
  88.   
  89.  /** 
  90.   * Return iterator corresponding to the first node containing an item. 
  91.   * @param x the item to search for. 
  92.   * @return an iterator; iterator is not valid if item is not found. 
  93.   */  
  94.  public LinkedListIterator<AnyType> find( AnyType x )  
  95.  {  
  96.      ListNode<AnyType> itr = header.next;  
  97.   
  98.      while( itr != null && !itr.element.equals( x ) )  
  99.          itr = itr.next;  
  100.   
  101.      return new LinkedListIterator<AnyType>( itr );  
  102.  }  
  103.   
  104.  /** 
  105.   * Return iterator prior to the first node containing an item. 
  106.   * @param x the item to search for. 
  107.   * @return appropriate iterator if the item is found. Otherwise, t

抱歉!评论已关闭.