采用Java实现单链表:

1.新建一个Node类,存放数据

public class Node<E> {
  //存放数据
public E data=null;
  //元素指针,指向下一节点
public Node<E> next=null; }

2.链表LinkList类

public class LinkList<E> {
    private Node<E> head = null;
    private int length = 0;

    public LinkList() {
        this.head = new Node<E>();
        this.head.next=null;
    }

    /**
     * 获取指定索引处的元素
     * 
     * @Title: getElem
     * @Description:
     * @param @param index
     * @param @return
     * @return Node<E>
     * @throws
     */
    public Node<E> getElem(int index) {
        Node<E> node = this.head;
        int j=0;
        while(node.next!=null&&j<index+1){
            node = node.next;
            j++;
        }
        if(node==null||j>index+1){
            throw new RuntimeException("第"+index+"个元素不存在!!");
        }
        return node;
    }


    /**
     * 向指定索引处插入元素
     * 
     * @Title: insert
     * @Description:
     * @param @param node
     * @param @param index
     * @return void
     * @throws
     */
    public void insert(Node<E> node, int index) {
        Node<E> tmp=new Node<E>();
        if(index==0){
            tmp=this.head.next;
            this.head.next=node;
            node.next=tmp;
            this.length++;
        }else{
            if(index<this.length()-1){
                Node<E> preNode = this.getElem(index-1);
                tmp = preNode.next;
                preNode.next=node;
                node.next=tmp;
                this.length++;                
            }
        }
    }
    /**
     * 添加一个元素
     * @Title: add 
     * @Description: 
     * @param @param node
     * @return void
     * @throws
     */
    public void add(Node<E> node){
        if(this.length()==0){
            this.head.next=node;
        }else{
            this.getElem(this.length()-1).next=node;
        }
        node.next=null;
        this.length++;
        //Msg.info("长度为:"+this.length());
    }

    /**
     * 判断链表是否为空
     * @Title: isEmpty 
     * @Description: 
     * @param @return
     * @return boolean
     * @throws
     */
    public boolean isEmpty(){
        return this.length()==0?true:false;
    }
    /**
     * 获取链表长度
     * 
     * @Title: length
     * @Description:
     * @param @return
     * @return int
     * @throws
     */
    public int length() {
        return this.length;
    }
    /**
     * 删除元素
     * @Title: delete 
     * @Description: 
     * @param @param index
     * @return void
     * @throws
     */
    public void delete(int index){
        if(this.isEmpty()){
            throw new RuntimeException("链表为空,无法删除");
        }else if(index<0||index>this.length-1){
            throw new RuntimeException("索引越界:"+index);
        }else{
            Node<E> preNode;
            if(index==0){
                preNode=this.head;
            }else{
                preNode = this.getElem(index-1);                
            }
            Node<E> afterNode = preNode.next.next;
            preNode.next=afterNode;
            this.length--;
        }
    }
    /**
     * 链表重置
     * @Title: reset 
     * @Description: 
     * @param 
     * @return void
     * @throws
     */
    public void reset(){
        this.head.next=null;
        this.length=0;
    }
}

 

链表的优点:

插入、删除很快,不需要移动大量元素,只需要找到对应的索引,移动指针就可以。

链表的缺点:

查找数据较慢,需一个个进行定位。

posted on 2013-06-06 23:55  Marcos.W  阅读(62)  评论(0)    收藏  举报