JAVA实现——单链表

结点Node类

public class Node {
    Object element;              //数据元素
    Node next;                   //表示下一个结点的对象引用
    Node(Node nextval){          //用于头结点的构造函数,头指针指向头结点,头结点不含数据元素
        next=nextval;
    }
    Node(Object obj,Node nextval){   //用于其他结点的构造函数,包括数据元素以及指向下一个结点的指针
        element=obj;
        next=nextval;
    }
    public Node getNext(){           //获得下一个结点
        return next;
    }
    public void setNext(Node nextval){ //设置当前结点的下一个结点
        next=nextval;
    }
    public Object getElement(){        //获得当前结点的数据元素
        return element;
    }
    public void setElement(Object obj){    //设置当前结点的数据元素
        element=obj;
    }
    public String toString(){              //转换element为String类型
        return element.toString();
    }

}

单链表LinList类


public class LinList implements List{  //实现List接口
    Node head;                         //头结点
    Node current;                      //当前结点位置
    int size;                          //数据元素个数
    LinList(){                         //构造函数
        head=current=new Node(null);   //设置头指针和当前指针均指向空
        size=0;                        //数据元素个数为0
    }

    public void index(int i) throws Exception{    //新增一个函数用于定位到指定的链表位置
        if(i<-1||i>size-1){
            throw new Exception("参数错误");
        }
        if(i==-1)return;                          //此句什么意思,不懂?
        current=head.next;
        int j=0;
        while((current!=null)&&j<i){              //循环定位到第i个结点
            current=current.next;
            j++;
        }
    }

    public void insert(int i, Object obj) throws Exception {   //单链表实现插入函数,在链表头和尾均可插入
        if(i<0||i>size){
            throw new Exception("参数错误");
        }
        index(i-1);                                            //定位到插入位置
        current.setNext(new Node(obj,current.next));           //设置形参obj为插入的数据元素,current.next是设置其指针指向当前结点的下一个结点
        size++;                                                //数据元素个数加1
    }

    public Object delete(int i) throws Exception {             //单链表实现删除,在链表头和尾均可删除,并且返回被删除结点的数据元素
        if(size==0){
            throw new Exception("链表已空,无元素可删");
        }
        if(i<0||i>size-1){
            throw new Exception("参数错误");
        }

        index(i-1);                                            //定位到第i-1个结点
        Object obj=current.next.getElement();                  //获得第i个结点的数据元素
        current.setNext(current.next.next);                    //设置第i-1个结点的下一个结点指向是第i+1个结点
        size--;                                                //结点个数减1
        return obj;                                            //返回被删除结点的数据元素
    }

    public Object getData(int i) throws Exception {            //取数据元素
        if(i<-1||i>size-1){
            throw new Exception("参数错误");
        }
        index(i);                                              //定位到第i个结点
        Object obj=current.getElement();                       //获得第i个结点的数据元素
        return obj;                                            //返回
    }

    public int size() {                                        //获得数据元素个数
        return size;
    }

    public boolean isEmpty() {                                 //判断是否为空
        return size==0;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值