Java双向链表

双向链表

1构造方法

public DoublyLinkedList() {
    this.first = new Node();
    this.last = new Node(null, null, first);
    this.first.next = this.last;
}

2链表是否为空

public boolean isEmpty() {
    return first.next == last && last.prev == first;
}

3从前向后插入节点

Node node = new Node(item, this.first.next, this.first);
this.first.next = node;
node.next.prev = node;//this.first.next.prev=node;

4从后向前插入节点

public void afterList(String item) {
    Node node = new Node(item, this.last, this.last.prev);
    this.last.prev.next = node;
    this.last.prev = node;
}

5获取双向链表中有效节点的个数

public int elementsCount() {
    Node currentNode;
    int cnt = 0;
    for (currentNode = this.first.next; ; currentNode = currentNode.next) {
        cnt++;
        if (currentNode.next == last)
            break;
    }
    return cnt;
}

6获取指定节点

public Node getIndexOfList(int index) {
    Node currentNode = this.first.next;
    for (int i = 0; i < index; i++) {
        currentNode = currentNode.next;
    }
    return currentNode;
}

7获取指定节点的内容,并把节点从指定位置删除

public String getByIndex(int index) {
    if (this.isEmpty()) {
        throw new EmptyList("链表为空");
    }
    if (index < 0 || index >= this.elementsCount()) {
        throw new EmptyList("不存在"+index+"的元素");
    }

    Node targetNode = this.getIndexOfList(index);
    String result = targetNode.item;
    targetNode.next.prev = targetNode.prev;
    targetNode.prev.next = targetNode.next;
    targetNode.next = null;
    targetNode.prev = null;
    return result;
}

8打印链表中的内容

public void print() {
        if (isEmpty())
            System.out.println("[]");
        StringBuilder stringBuilder = new StringBuilder();
        Node node;
        stringBuilder.append("[--");
        for (node = this.first.next; ; node = node.next) {
            stringBuilder.append(node.item + "--");
            if (node.next == last) {
                stringBuilder.append("]");
                break;
            }
        }
        System.out.println(stringBuilder.toString());

    }

9节点的静态内部类

public static class Node {

        public Node() {
        }

        public Node(String item) {
            this.item = item;
        }

        public Node(String item, Node next, Node prev) {
            this.item = item;
            this.next = next;
            this.prev = prev;
        }

        /**
         * 节点内容
         */
        private String item;

        /**
         * 后置节点地址
         */
        private Node next;

        /**
         * 前驱节点地址
         */
        private Node prev;


    }
}

10异常处理

class EmptyList extends RuntimeException{
    public EmptyList() {
    }
    public EmptyList(String msg)
    {
        super(msg);
    }
}

总体呈现

package com.yu;

public class DoublyLinkedList {

    public DoublyLinkedList() {
        this.first = new Node();
        this.last = new Node(null, null, first);
        this.first.next = this.last;
    }

    /**
     * 起始节点
     */
    private Node first;
    /**
     * 最后一个节点
     */
    private Node last;

    /**
     * 判断链表是否为空
     */
    public boolean isEmpty() {
        return first.next == last && last.prev == first;
    }

    /**
     * 从前往后插节点
     */
    public void beforeList(String item) {
//        Node node = new Node(item);
//        Node currentFist=this.first;
//        Node currentLast=this.last;
//        while(currentLast.prev!=null)
//        {
//            currentLast=currentLast.prev;
//        }
//        currentFist.next=node;
//        currentLast.prev=node;
        Node node = new Node(item, this.first.next, this.first);
        this.first.next = node;
        node.next.prev = node;//this.first.next.prev=node;
    }

    /**
     * 从后插入节点
     */
    public void afterList(String item) {
        Node node = new Node(item, this.last, this.last.prev);
        this.last.prev.next = node;
        this.last.prev = node;
    }

    /**
     * 获取链表中有效节点个数
     */
    public int elementsCount() {
        Node currentNode;
        int cnt = 0;
        for (currentNode = this.first.next; ; currentNode = currentNode.next) {
            cnt++;
            if (currentNode.next == last)
                break;
        }
        return cnt;
    }

    /**
     * 获取指定节点
     * index从0开始
     */
    public Node getIndexOfList(int index) {
        Node currentNode = this.first.next;
        for (int i = 0; i < index; i++) {
            currentNode = currentNode.next;
        }
        return currentNode;
    }

    /**
     * 获取节点元素,并把节点从链表中删除
     */
    public String getByIndex(int index) {
        if (this.isEmpty()) {
            throw new EmptyList("链表为空");
        }
        if (index < 0 || index >= this.elementsCount()) {
            throw new EmptyList("不存在"+index+"的元素");
        }

        Node targetNode = this.getIndexOfList(index);
        String result = targetNode.item;
        targetNode.next.prev = targetNode.prev;
        targetNode.prev.next = targetNode.next;
        targetNode.next = null;
        targetNode.prev = null;
        return result;
    }


    /**
     * 从前往后打印元素
     */
    public void print() {
//        Node currentNode=this.first;
//        while(currentNode.next!=last)
//        {
//
//            currentNode=currentNode.next;
//            System.out.println(currentNode.item);
//        }
        if (isEmpty())
            System.out.println("[]");
        StringBuilder stringBuilder = new StringBuilder();
        Node node;
        stringBuilder.append("[--");
        for (node = this.first.next; ; node = node.next) {
            stringBuilder.append(node.item + "--");
            if (node.next == last) {
                stringBuilder.append("]");
                break;
            }
        }
        System.out.println(stringBuilder.toString());

    }

    /**
     * 静态内部类
     */
    public static class Node {

        public Node() {
        }

        public Node(String item) {
            this.item = item;
        }

        public Node(String item, Node next, Node prev) {
            this.item = item;
            this.next = next;
            this.prev = prev;
        }

        /**
         * 节点内容
         */
        private String item;

        /**
         * 后置节点地址
         */
        private Node next;

        /**
         * 前驱节点地址
         */
        private Nocde prev;


    }
}
class EmptyList extends RuntimeException{
    public EmptyList() {
    }
    public EmptyList(String msg)
    {
        super(msg);
    }
}

1测试类

package com.yu;

public class Demo1 {
    public static void main(String[] args) {
        DoublyLinkedList linkedList = new DoublyLinkedList();
        System.out.println(linkedList.isEmpty());
        linkedList.afterList("宫本0");
        linkedList.afterList("宫本1");
        linkedList.afterList("宫本2");
        linkedList.afterList("宫本3");
        linkedList.afterList("宫本4");
        System.out.println(linkedList.getByIndex(1));
        linkedList.print();
        System.out.println(linkedList.elementsCount());
        System.out.println(linkedList.isEmpty());
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ILFFQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值