实现简单的双向链表

落爺英雄遲暮 提交于 2019-12-05 03:14:29
package com.zhangwl.数据结构.双向链表;/** * @ClassName Node * @Description 双向链表节点 * @Author zhangwl * @Date 2019/11/18 22:34 * @Version 1.0 **/@SuppressWarnings("all")public class Node {    //节点id编号    public Integer id;    //节点数据    public String data;    //节点指针指向下一个节点    public Node next;    //节点指针指向上一个节点    public Node pre;    public Node() {    }    public Node(Integer id, String data) {        this.id = id;        this.data = data;    }    /**     * 打印具体节点信息     *     * @return     */    @Override    public String toString() {        //这块容易导致java.lang.StackOverflowError 异常,在打印next和pre的情况下会导致该异常(无线循环)        return "Node{" +                "id=" + id +                ", data='" + data +                "'}";    }}
package com.zhangwl.数据结构.双向链表;/** * @ClassName DoubleLinkedList * @Description 双向链表 * @Author zhangwl * @Date 2019/11/18 22:40 * @Version 1.0 **/@SuppressWarnings("all")public class DoubleLinkedList {    //创建头节点first    Node first = new Node(0, null);    /**     * 获取头结点     *     * @return 返回头结点     */    public Node getFirst() {        if (null != first.next) {            return first.next;        }        return null;    }    /**     * 向双向链表中添加一个节点     *     * @param node 添加到链表中的节点     * @return false :添加失败 ;true:添加成功     */    public boolean add(Node node) {        Node temp = first;        boolean flag = false;        while (true) {            //标识遍历到最后一个节点            if (null == temp.next) {                node.pre = temp;                temp.next = node;                flag = true;                break;            }            temp = temp.next;        }        //返回是否添加成功的标识        return flag;    }    /**     * 修改双向链表中的元素     *     * @return false :修改失败;true:修改成功     */    public boolean update(Node node) {        //是否修改成功的标志,默认修改失败        boolean flag = false;        //判断链表中是否有节点供修改        if (null == first.next) {            System.out.println("哦哦,链表,空空如也...");        }        Node temp = first.next;        //循环遍历找到链表中的元素进行修改        while (true) {            if (null != temp) {                if (temp.id == node.id) {                    temp.data = node.data;                    flag = true;                    break;                }                temp = temp.next;                continue;            }            break;        }        if (!flag) {            System.out.println("糟糕,尚无要修改的节点");        }        return flag;    }    public boolean delete(int id) {        //是否删除成功的标志,默认修改失败        boolean flag = false;        //判断链表中是否有节点供删除        if (null == first.next) {            System.out.println("哦哦,链表,空空如也...");        }        Node temp = first.next;        //循环遍历找到链表中的元素进行删除        while (true) {            if (null != temp) {                if (temp.id == id) {                    temp.pre.next = temp.next;                    temp.next.pre = temp.pre;                    flag = true;                    break;                }                temp = temp.next;                continue;            }            break;        }        if (!flag) {            System.out.println("糟糕,尚无要删除的节点");        }        return flag;    }    /**     * 遍历双向链表,并打印     */    public void foreach() {        //判断链表是否为空        if (first.next == null) {            System.out.println("哦哦,链表,空空如也...");        }        Node temp = first.next;        while (true) {            if (null == temp) {                break;            }            System.out.println(temp);            temp = temp.next;        }    }}
package com.zhangwl.数据结构.双向链表;import org.junit.Test;/** * @ClassName RunMain * @Description * @Author zhangwl * @Date 2019/11/18 23:00 * @Version 1.0 **/public class RunMain {    //添加节点    @Test    public void test_add_update() {        DoubleLinkedList linkedList = new DoubleLinkedList();        linkedList.add(new Node(1, "东至"));        linkedList.add(new Node(2, "青阳"));        linkedList.add(new Node(3, "石台"));        System.out.println("获取头元素:");        System.out.println(linkedList.getFirst());        System.out.println("遍历链表中元素:");        linkedList.foreach();    }    //修改节点    @Test    public void test_update() {        DoubleLinkedList linkedList = new DoubleLinkedList();        linkedList.add(new Node(1, "东至"));        linkedList.add(new Node(2, "青阳"));        linkedList.add(new Node(3, "石台"));        System.out.println("遍历链表中元素:");        linkedList.foreach();        System.out.println("修改链表中的节点:");        linkedList.update(new Node(2, "贵池"));        System.out.println("遍历链表中元素:");        linkedList.foreach();    }    //删除节点    @Test    public void test_delete() {        DoubleLinkedList linkedList = new DoubleLinkedList();        linkedList.add(new Node(1, "东至"));        linkedList.add(new Node(2, "青阳"));        linkedList.add(new Node(3, "石台"));        System.out.println("遍历链表中元素:");        linkedList.foreach();        System.out.println("删除链表中的节点:");        System.out.println(linkedList.delete(2));        System.out.println("遍历链表中元素:");        linkedList.foreach();        System.out.println("删除链表中的节点:");        System.out.println(linkedList.delete(1));        System.out.println("遍历链表中元素:");        linkedList.foreach();    }}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!