春風博客

          春天里,百花香...

          導航

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統計

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          泛型單鏈表類

          package com.sitinspring;

          /**
           * 單鏈表節點類
           * 
          @author: sitinspring(junglesong@gmail.com)
           * @date: 2008-7-1-下午10:42:49
           * 
          @param <T>
           
          */

          class Node<extends Object>{
              
          protected T value;
              
          protected Node next;
          }


          /**
           * 單鏈表類
           * 
          @author: sitinspring(junglesong@gmail.com)
           * @date: 2008-7-1-下午10:42:37
           * 
          @param <T>
           
          */

          public class ChainList<extends Object>{
              
          private Node<T> first;
              
              
          public void addTail(T t){
                  Node
          <T> newNode=new Node<T>();
                  newNode.value
          =t;
                  
                  
          if(first==null){                        
                      first
          =newNode;
                  }

                  
          else{
                      Node
          <T> curr=first;
                      
                      
          while(curr.next!=null){
                          curr
          =curr.next;
                      }

                      
                      curr.next
          =newNode;
                  }

              }

              
              
          public void addHead(T t){
                  Node
          <T> newNode=new Node<T>();
                  newNode.value
          =t;
                  newNode.next
          =first;
                  first
          =newNode;
              }

              
              
          public int size(){
                  
          int size=0;
                  Node
          <T> curr=first;
                  
                  
          while(curr!=null){
                      size
          ++;
                      curr
          =curr.next;
                  }

                  
                  
          return size;
              }

              
              
          public T get(int index){
                  
          if(index<0){
                      
          throw new IllegalArgumentException("下標不能是負數");
                  }

                  
          else{
                      Node
          <T> curr=first;
                      
                      
          while(curr!=null){                
                          
          if(index==0){
                              
          return curr.value;
                          }

                          
                          index
          --;
                          curr
          =curr.next;
                      }

                      
                      
          throw new NullPointerException();
                  }

              }

              
              
          public void remove(int index){
                  
          if(index<0){
                      
          throw new IllegalArgumentException("下標不能是負數");
                  }

                  
          else{
                      
                      
          if(index==0){
                          Node
          <T> next=first.next;                
                          first
          =next;
                      }

                      
          else{            
                          Node
          <T> curr=first;
                          Node
          <T> prev=curr;
                          
                          
          while(curr!=null){                
                              
          if(index==0){
                                  prev.next
          =curr.next;
                                  
          return;
                              }

                              
                              index
          --;
                              prev
          =curr;
                              curr
          =curr.next;
                          }

                          
                          
          throw new NullPointerException();
                      }

                  }

              }

              
              
          public void display(){
                  System.out.print(
          "鏈表元素有");
                  Node
          <T> curr=first;
                  
                  
          while(curr!=null){
                      System.out.print(curr.value
          +",");
                      curr
          =curr.next;
                  }

                  System.out.println();
              }

              
              
          public static void main(String[] args){
                  ChainList
          <String> ls=new ChainList<String>();
                  
                  ls.addTail(
          "A");
                  ls.addTail(
          "B");
                  ls.addTail(
          "C");
                  ls.display();
                  
                  ls.addHead(
          "1");
                  ls.display();
                  System.out.println(
          "數組長度為:"+ls.size());
                  System.out.println(
          "第三個元素為:"+ls.get(2));
                  
                  
          for(int i=0;i<ls.size();i++){
                      System.out.println(
          ""+(i+1)+"個元素為:"+ls.get(i));
                  }

                  
                  ls.addTail(
          "E");
                  ls.addTail(
          "F");
                  ls.remove(
          0);        
                  ls.display();
                  
                  ls.remove(
          1);        
                  ls.display();
                  
                  ls.remove(ls.size()
          -1);        
                  ls.display();
                  
                  
          for(int i=0;i<ls.size();i++){
                      System.out.println(
          ""+(i+1)+"個元素為:"+ls.get(i));
                  }

              }

          }

          posted on 2008-07-01 22:44 sitinspring 閱讀(554) 評論(0)  編輯  收藏 所屬分類: 算法數據結構

          sitinspring(http://www.aygfsteel.com)原創,轉載請注明出處.
          主站蜘蛛池模板: 尤溪县| 正镶白旗| 藁城市| 亚东县| 资兴市| 阳城县| 荆州市| 班玛县| 盖州市| 长丰县| 宁远县| 弥渡县| 阜康市| 汾西县| 昌黎县| 铁力市| 大洼县| 富锦市| 和平区| 林周县| 荔波县| 昌邑市| 临猗县| 洛川县| 邹平县| 庆安县| 民和| 贞丰县| 海伦市| 兴文县| 同江市| 洛南县| 海盐县| 崇信县| 忻州市| 南丰县| 绥宁县| 确山县| 遂宁市| 吴忠市| 农安县|