心情不错,写个双向不循环单链表


public interface IDoubleLinked {
    //头插法
    void addFirst(int data);

    //尾插法
    void addLast(int data);

    //任意位置插入,第一个数据节点为0号下标
    boolean addindex(int index, int data);

    //查找是否包含关键字key是否在单链表当中
    boolean contains(int key);

    //删除第一次出现关键字为key的节点
    int remove(int key);

    //删除所有值为key的节点
    void removeAllKey(int key);

    //得到单链表的长度
    int getLength();

    void display();

    void clear();
}

//不带头双向不循环链表
public class MyDoubleLickList implements IDoubleLinked {
    class Node{
        private Node next;
        private int data;
        private Node prev;
        Node(int data){
            this.data = data;
            this.next = null;
            this.prev = null;
        }
    }
    private Node head = null;
    private Node last = null;
    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
        }
        node.next = this.head;
        this.head.prev = node;
        this.head = node ;
    }

    @Override
    public void addLast(int data) {
        Node node = new Node(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
        }
        this.head.next = node;
        node.prev = this.head;
        this.last = node;
    }
    //寻找指定下标的节点
    private Node SearchIndex(int index){
        Node cur = this.head;
        int MyIndex = 0;
        while(cur !=null){
            if(MyIndex == index){
                return cur;
            }
            MyIndex++;
            cur = cur.next;
        }
        return null;
}
    @Override
    public boolean addindex(int index, int data) {
        if(index < 0||index>getLength()){
            return false;
        }
        if(index == 0){
            addFirst(data);
            return true;
        }
        if(index == getLength()){
            addLast(data);
            return true;
        }
        Node cur = SearchIndex(index);
        if(cur == null){
            throw new UnsupportedOperationException("The Wrong Index!!!");
        }
        Node node = new Node(data);
        node.next = cur.next;
        cur.next.prev = node;
        node.prev = cur;
        cur.next = node;
        return true;
    }

    @Override
    public boolean contains(int key) {
        Node cur = this.head;
        while(cur != null){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public int remove(int key) {
        Node cur = this.head;
        int count = 0;
        while(cur != null){
            if(cur.data == key) {
            //储存key值,删除成功之后,返回该值
                count = key;
                if (cur == this.head) {
                //若删除的是头结点时,要将头结点后移
                    this.head = this.head.next;
                    this.head.prev = null;
                } else {
                    cur.prev.next = cur.next;
              //若无此条件时,直接执行if内的语句,当删除的是尾节点时,会报错空指针异常
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                    //若删除的是尾节点时
                        this.last = this.last.prev;
                    }
                    return count;
                }
            }
            cur = cur.next;
        }
        return -1;
    }

    @Override
    public void removeAllKey(int key) {
        Node cur = this.head;
        while(cur != null){
            if(cur.data == key) {
                if (cur == this.head) {
                    this.head = this.head.next;
                    this.head.prev = null;
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        this.last = this.last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int getLength() {
        Node cur = this.head;
        int length = 0;
        while(cur != null){
            length++;
            cur = cur.next;
        }
        return length;
    }

    @Override
    public void display() {
        Node cur = this.head;
        while (cur != null){
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        while(this.head != null){
            Node cur = this.head.next;
            this.head.next = null;
            this.head.prev = null;
            this.head =cur;
        }
    }
     public static void main(String[] args) {
        MyDoubleLickList myDoubleLickList = new MyDoubleLickList();
        myDoubleLickList.addFirst(2);
        myDoubleLickList.addFirst(3);
        myDoubleLickList.addFirst(4);
        myDoubleLickList.addFirst(5);
        myDoubleLickList.addFirst(5);
        myDoubleLickList.addLast(5);
        myDoubleLickList.display();
        System.out.println("****************************");
        myDoubleLickList.addindex(1,6);
        myDoubleLickList.display();
        System.out.println("****************************");
        myDoubleLickList.remove(6);
        myDoubleLickList.display();
        System.out.println("****************************");
        myDoubleLickList.removeAllKey(5);
        myDoubleLickList.display();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值