春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

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

          統(tǒng)計(jì)

          公告

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

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個(gè)人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          泛型單鏈表類

          package com.sitinspring;

          /**
           * 單鏈表節(jié)點(diǎn)類
           * 
          @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)不能是負(fù)數(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)不能是負(fù)數(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ù)組長(zhǎng)度為:"+ls.size());
                  System.out.println(
          "第三個(gè)元素為:"+ls.get(2));
                  
                  
          for(int i=0;i<ls.size();i++){
                      System.out.println(
          ""+(i+1)+"個(gè)元素為:"+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)+"個(gè)元素為:"+ls.get(i));
                  }

              }

          }

          posted on 2008-07-01 22:44 sitinspring 閱讀(563) 評(píng)論(0)  編輯  收藏 所屬分類: 算法數(shù)據(jù)結(jié)構(gòu)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處.
          主站蜘蛛池模板: 钟山县| 永顺县| 内黄县| 龙游县| 牡丹江市| 泸水县| 望城县| 武穴市| 阳曲县| 莆田市| 武夷山市| 汕头市| 永修县| 齐齐哈尔市| 丁青县| 阳山县| 贵阳市| 玉树县| 巴塘县| 石屏县| 沂源县| 河津市| 佛学| 枞阳县| 伊吾县| 东山县| 新源县| 仙游县| 习水县| 安化县| 霍邱县| 永昌县| 睢宁县| 高要市| 云南省| 连云港市| 深水埗区| 新泰市| 工布江达县| 汤原县| 大冶市|