春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

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

          統(tǒng)計

          公告

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

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          泛型單鏈表類

          package com.sitinspring;

          /**
           * 單鏈表節(jié)點類
           * 
          @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("下標(biāo)不能是負數(shù)");
                  }

                  
          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("下標(biāo)不能是負數(shù)");
                  }

                  
          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(
          "數(shù)組長度為:"+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)  編輯  收藏 所屬分類: 算法數(shù)據(jù)結(jié)構(gòu)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請注明出處.
          主站蜘蛛池模板: 玛曲县| 吉安市| 宿松县| 鹿泉市| 工布江达县| 文昌市| 绥中县| 杂多县| 云霄县| 佛山市| 马关县| 平安县| 伊金霍洛旗| 蓝山县| 麟游县| 西畴县| 中江县| 遂平县| 马鞍山市| 青浦区| 河东区| 察隅县| 从化市| 瓦房店市| 仙居县| 新和县| 格尔木市| 滨州市| 同心县| 抚顺市| 长垣县| 南溪县| 灵丘县| 方城县| 峨眉山市| 吴忠市| 孟连| 沧州市| 西乌珠穆沁旗| 隆德县| 普定县|