春风博客

春天里,百花香...

导航

<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 阅读(548) 评论(0)  编辑  收藏 所属分类: 算法数据结构


只有注册用户登录后才能发表评论。


网站导航:
 
sitinspring(http://www.blogjava.net)原创,转载请注明出处.