单向链

啥话都不说了,上代码吧,代码中我加了注释。

package cn.lu.linklist;

public class Cell<T> {

    private T data;
    private Cell<T> next;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Cell<T> getNext() {
        return next;
    }

    public void setNext(Cell<T> next) {
        this.next = next;
    }
}
package cn.lu.linklist;
//单向链表的类
public class LinkList<T> {
    private Cell<T> first;//链头定义

    //新的空链的无参构造
    public LinkList() {
        first=new Cell<T>();
    }
    //带参构造的方法(尾部添加法)
    public LinkList(T[] data){
          
          first = new Cell<T>(); // 头结点
          Cell<T> end = first;
          Cell<T> temp = null;
          //将形参传入链中
          for (T t : data) {
            temp = new Cell<T>();//必须new,否则temp.next的最终不为空
            temp.setData(t);
            end.setNext(temp);
            end = temp;
        }
     }
    //在链中指定位置插入数据
    public boolean insert(T t, int index) {
        int count = 0;
        Cell<T> pointer = first;//定义指针变量
        Cell<T> temp = null;//临时变量
        //插入链的位置值不能小于1(默认链已经有了头节点)
        if (index < 1)
            throw new ArrayIndexOutOfBoundsException();
        //指针移动到指定的位置(index位置的前一个,默认值是从0开始的)
        while (pointer != null && count < index - 1) {
            pointer = pointer.getNext();
            count++;
        }
        //指针位置为空时就报错(插入的位置超出链长)
        if (pointer == null)
            throw new ArrayIndexOutOfBoundsException();
        //在指定位置插入数据
        else {
            temp = new Cell<T>();
            temp.setData(t);//需要插入的数据赋值给临时变量
            temp.setNext(pointer.getNext());//把要插入数据的next指向index位置的数据
            pointer.setNext(temp);//把指针位置的next指向要插入的数据
        }
        return true;
    }

    //在链中的指定位置删除数据
    public Boolean remove(int index) {
         
        int count = 0;
        Cell<T> pointer = first;  //定义指针变量
        Cell<T> temp = null;    //定义临时变量
        //如果要删除链中的位置不能小于1
        if (index < 1)
            throw new ArrayIndexOutOfBoundsException();
        //指针移动到指定的位置(index位置的前一个,默认值是从0开始的)
        while (pointer != null && count < index - 1) {
            pointer = pointer.getNext();
            count++;
        }
        //要删除的数据位置超过了链长度
        if (pointer== null)
            throw new ArrayIndexOutOfBoundsException();
        //删除数据的操作开始
        else {
            temp = pointer.getNext();//临时变量赋值给要删除的数据
            pointer.setNext(temp.getNext());//把当前指针数据的next指向要删除数据的下一个数据
        }
        return true;
    }
    //替换指定位置的数据
    public boolean replace(int index, T t) {
         
        int count = 0;
        Cell<T> pointer = first;//定义指针变量
        
        //要替换链中的位置不能小于1
        if (index < 1)
            throw new ArrayIndexOutOfBoundsException();
        //指针移动到要替换的位置
        while (pointer!= null && count < index) {
            pointer = pointer.getNext();
            count++;
        }
        //判断当前指针位置不能超出链长度
        if (pointer == null)
            throw new ArrayIndexOutOfBoundsException();
        //替换数据开始操作
        else{
            //注意此时没有改变pointer的next值,只是将pointer的Data替换成t。
            pointer.setData(t);
            
        }
        return true;
    }
    //获得指定位置的数据链值
    public T get(int index) {
         
        int count = 0;
        Cell<T> pointer = first;//定义指针变量
        //要替换链中的位置不能小于1
        if (index < 1)
            throw new ArrayIndexOutOfBoundsException();
        //指针移动到指定位置
        while (pointer != null && count < index) {
            pointer = pointer.getNext();
            count++;
        }
        //判断当前指针位置不能超出链长度
        if (pointer == null)
            throw new ArrayIndexOutOfBoundsException();
        else
            return pointer.getData();
 
    }
    //判断数据链是否为空
    public boolean isEmpty(){
        
        if(first.getNext() == null)
            return true;
        
        return false;
    }
    //计算数据链长度
    public int size() {
         
        int count = 0;
        Cell<T> pointer = first; //定义指针变量
        
        while (pointer.getNext() != null) {
            pointer = pointer.getNext();
            count++;
        }
        return count;
    }
    //链的toString方法
    public String toString() {
         
        String str = "[";//起始符输出
        Cell<T> pointer = first;
        //移动指针判断循环的条件
        while (pointer!= null) {
            pointer = pointer.getNext();//移动指针
            //指针位置不为能为空
            if (pointer != null) {
                //当指针位置的下一个不为空时的输出。
                if (pointer.getNext() != null){
                    str = str + pointer.getData() + ",";
                    }
                //当指针位置的下一个为空时
                else{
                    str = str + pointer.getData();
                    }
            }
        }
        str = str + "]";//结束符输出
        return str;
    }

    
    
}
原文地址:https://www.cnblogs.com/shqnl/p/10988140.html