分享

带头结点的单链表类

 干掉熊猫,我就是国宝 2011-09-05
  1. //带头结点的单链表类  
  2. //建议,不声明成员变量rear和n,不安全,维护困难,子类需要同时修改3个成员变量,易出错。  
  3.   
  4. package dataStructure.linearList;  
  5. import dataStructure.linearList.Node;            //导入单链表结点类  
  6. import java.util.Iterator;                       //导入迭代器接口  
  7.   
  8. public class HSLinkedList<E> extends AbstractList<E> implements LList<E> //带头结点的单链表类,实现线性表接口  
  9. {  
  10.     protected Node<E> head;                      //头指针,指向单链表的头结点  
  11.     protected Node<E> rear;                    //尾指针,指向单链表最后一个结点  
  12.     protected int n;                             //单链表长度  
  13.   
  14.     public HSLinkedList()                        //构造空链表  
  15.     {  
  16.         this.head = new Node<E>();               //创建头结点,值为null  
  17.         this.rear = this.head;  
  18.         this.n=0;  
  19.     }  
  20.       
  21.       
  22.     public boolean isEmpty()                     //判断单链表是否为空,O(1)  
  23.     {  
  24.         return this.head.next==null;  
  25.     }  
  26.     public int length()                          //返回单链表长度,O(1)  
  27.     {  
  28.         return this.n;  
  29.     }  
  30.   
  31.   
  32.     public E get(int index)                      //返回序号为index的对象,index初值为0  
  33.     {                                            //若单链表空或序号错误返回null,O(n)  
  34.         if (index>=0)  
  35.         {  
  36.             int j=0;   
  37.             Node<E> p=this.head.next;  
  38.             while (p!=null && j<index)  
  39.             {  
  40.                 j++;  
  41.                 p = p.next;  
  42.             }  
  43.             if (p!=null)  
  44.                 return (E)p.data;  
  45.         }  
  46.         return null;  
  47.     }  
  48.      
  49.     public E set(int index, E element)           //设置序号为index的对象为element,O(n)  
  50.     {                                            //若操作成功返回原对象,否则返回null  
  51.         if (index>=0 && element!=null)  
  52.         {  
  53.             int j=0;   
  54.             Node<E> p=this.head.next;  
  55.             while (p!=null && j<index)  
  56.             {  
  57.                 j++;  
  58.                 p = p.next;  
  59.             }  
  60.             if (p!=null)  
  61.             {  
  62.                 E old = (E)p.data;  
  63.                 p.data = element;  
  64.                 return old;                      //若操作成功返回原对象  
  65.             }  
  66.         }  
  67.         return null;                             //操作不成功  
  68.     }  
  69.   
  70.     public boolean add(E element)                //在单链表最后添加对象,O(1)  
  71.     {  
  72.         if (element==null)  
  73.             return false;  
  74.                                            
  75.         this.rear.next = new Node<E>(element);   //尾插入  
  76.         this.rear = this.rear.next;              //移动尾指针  
  77.         this.n++;  
  78.         return true;  
  79.     }  
  80.   
  81.     public boolean add(int index, E element)     //在指定位置插入非空的指定对象  
  82.     {                                            //若操作成功返回true,O(n)  
  83.         if (element==null)  
  84.             return false;  
  85.   
  86.         if (index>=this.n)  
  87.             return this.add(element);            //插入在最后  
  88.         else  
  89.         {  
  90.             int j=0;  
  91.             Node<E> p = this.head;  
  92.             while (p.next!=null && j<index)      //若index<=0,插入在头结点之后  
  93.             {  
  94.                 j++;  
  95.                 p = p.next;  
  96.             }  
  97.             p.next=new Node<E>(element, p.next); //插入在p结点之后,包括头插入、中间插入  
  98.             this.n++;  
  99.             return true;  
  100.         }  
  101.     }  
  102.   
  103.     public E remove(int index)                   //移去index位置的对象,O(n)  
  104.     {                                            //若操作成功返回被移去对象,否则返回null  
  105.         E old = null;  
  106.         if (index>=0)                            //头删除、中间/尾删除  
  107.         {  
  108.             int j=0;   
  109.             Node<E> p=this.head;  
  110.             while (p.next!=null && j<index)      //定位到待删除结点的前驱结点  
  111.             {  
  112.                 j++;  
  113.                 p = p.next;  
  114.             }  
  115.             if (p.next!=null)  
  116.             {  
  117.                 old = (E)p.next.data;  
  118.                 if (p.next==this.rear)  
  119.                     this.rear=p;  
  120.                 p.next = p.next.next;            //删除p的后继结点  
  121.                 this.n--;  
  122.             }  
  123.         }  
  124.         return old;  
  125.     }  
  126.   
  127.     public void clear()                          //清空单链表,O(1)  
  128.     {  
  129.         this.head.next = null;  
  130.         this.rear = this.head;  
  131.         this.n=0;  
  132.     }  
  133.   
  134.     public String toString()                     //返回显示单链表所有元素值对应的字符串  
  135.     {                                            //单链表遍历算法,O(n)  
  136.         String str="(";  
  137.         Node<E> p = this.head.next;  
  138.         while (p!=null)   
  139.         {  
  140.            str += p.data.toString();  
  141.            p = p.next;  
  142.            if (p!=null)   
  143.               str += ", ";  
  144.         }  
  145.         return str+")";  
  146.     }  
  147.     //以上实现LList接口  
  148.   
  149.   
  150.   
  151.     public Iterator<E> iterator()                //返回一个迭代器对象  
  152.     {  
  153.         return new Itr();  
  154.     }  
  155.   
  156.     private class Itr implements Iterator<E>     //私有内部类,实现迭代器接口  
  157.     {  
  158.         private Node<E> cursor = head.next;  
  159.   
  160.         public boolean hasNext()                 //若有后继元素,返回true  
  161.         {  
  162.             return cursor!=null && cursor.next!=null;  
  163.         }  
  164.   
  165.         public E next()                          //返回后继元素  
  166.         {  
  167.             if (cursor != null && cursor.next!=null)  
  168.             {  
  169.                 E element = cursor.next.data;  
  170.                 cursor = cursor.next;  
  171.                 return element;  
  172.             }  
  173.             return null;  
  174.         }  
  175.   
  176.         public void remove()                     //不支持该操作  
  177.         {  
  178.             throw new UnsupportedOperationException();  
  179.         }  
  180.     }//内部类Itr结束  
  181.   
  182.     public static void main(String args[])  
  183.     {  
  184.         HSLinkedList<String> list = new HSLinkedList<String>();  
  185.         for (int i=5; i>=0; i--)  
  186.             list.add(0,new String((char)('A'+i)+""));  
  187.         System.out.println(list.toString());  
  188.     }  
  189. }  
  190.   
  191.   
  192.   
  193. /* 
  194. 程序运行结果如下:     
  195. (A, B, C, D, E, F) 
  196.  
  197.  
  198. */ 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多