数据结构双链表类

数据结构双链表类

public class DoublyList<T> {
    public DoubleNode<T> head;  //双链表头指针
    public DoubleNode<T> rear;  //双链表尾指针(因为双链表结点加入了双向指针,所以引入尾指针会更加方便)
    public int size;    //链表长度
    /*构造方法,构造空双链表*/
    public DoublyList(){
        this.head=new DoubleNode<>();
        this.rear=this.head;
        this.size=0;
    }
    /*构造双链表,尾插入values数组元素,忽略空元素*/
    public DoublyList(T[] values){
        this();
        for (int i=0; i<values.length; i++){
            if (values[i]!=null){
                rear.next=new DoubleNode<T>(values[i],rear,rear.next);
                rear=rear.next;
                size++;
            }
        }
    }
    /*链表判空*/
    public boolean isEmpty(){
        return this.head.next==null;
    }
    /*去元素*/
    public T get(int i){
        DoubleNode<T> p=this.head.next;
        for (int j=0;p!=null && j<i;j++){
            p=p.next;
        }
        return (i>=0 && p!=null) ? p.data : null;
    }
    /*返回链表长度*/
    public int size(){
        return size;
    }
    /*设置元素*/
    public void set(int i, T x){
        DoubleNode<T> p=this.head.next;
        if (x==null)
            throw new NullPointerException("x==null");
        if (i>=0 && i<=this.size){
            for (int j=0; p!=null && j<i; j++){
                p=p.next;
            }
            p.data=x;
        }
        else
            throw new IndexOutOfBoundsException();
    }
    /*返回所有元素的描述字符串*/
    public String toString(){
        String str=this.getClass().getName()+"(";   //返回类名
        for (DoubleNode<T> p=this.head.next ; p!=null ; p=p.next){
            str += p.data.toString()+(p.next!=null ? "," : "");
        }
        return str+")";
    }
    /*双链表插入,在指定结点前插入*/
    public DoubleNode<T> insertFront(int i, T x){
        DoubleNode<T> front=this.head;
        if (x==null)
            return null;
        if (i<0)
            i=0;
        if (i>this.size)
            i=size;
        for (int j=0; front.next!=null && j<i; j++)
            front=front.next;
        front.next=new DoubleNode<>(x,front,front.next);
        this.size++;
        return front.next;
    }
    /*双链表插入,在指定结点后插入*/
    public DoubleNode<T> insertBehind(int i, T x){
        DoubleNode<T> front=this.head;
        if (x==null)
            return null;
        if (i<0)
            i=0;
        if (i>this.size)
            i=size;
        for (int j=0; j<=i; j++)
            front=front.next;
        front.next=new DoubleNode<>(x,front,front.next);
        this.size++;
        if (i==size){
            rear=rear.next;
        }
        return front.next;
    }
    /*双链表插入,直接插入最后*/
    public DoubleNode<T> insert(T x){
       return this.insertFront(Integer.MAX_VALUE,x);
    }
    /*双链表删除*/
    public T remove(int i) {
        DoubleNode<T> front = this.head;
        for (int j = 0; j <= i; j++) {
            front = front.next;
        }
        if (i >= 0) {
            T x = front.data;
            front.prev.next = front.next;
            if (front != null) {
                front.next.prev = front.prev;
                return x;
            }
            this.size--;
        }
        return null;
    }

    public static void main(String[] args) {
        DoublyList<String> list=new DoublyList<>();
        for (int i=4; i>=0; i--){
            list.insertFront(0,(char)('A'+i)+"");
        }
        System.out.println(list.toString());
        list.remove(0);
        System.out.println(list.toString());
        list.insertFront(0,"A");
        System.out.println(list.toString());
        list.insertFront(0,"A");
        System.out.println(list.toString());
        list.insertBehind(0,"X");
        System.out.println(list.toString());
        list.insertBehind(6,"F");
        System.out.println(list.toString());
        list.insertFront(6,"F");
        System.out.println(list.toString());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值