欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

双向链表实现思路,

来源: javaer 分享于  点击 23846 次 点评:192

双向链表实现思路,


双向链表

实现思路

遍历

  • 和单向链表的遍历相同,需要一个辅助节点来保存当前正在遍历的节点

添加

  • 双向链表多出了一个front,所以在添加时,要让新增节点的front指向链表尾节点

修改

  • 和单向链表的修改相同

删除

  • 使用temp来保存要删除的节点
  • temp.front.next指向temp.next
  • temp.next指向temp.front

代码

package com.wyh.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        StudentLinked studentLinked = new StudentLinked();
        studentLinked.addNode(new Node(1,"张三"));
        studentLinked.addNode(new Node(2,"李四"));
        studentLinked.addNode(new Node(3,"王五"));
        studentLinked.addNode(new Node(4,"二蛋"));

        studentLinked.updateNode(new Node(4,"狗蛋儿"));
        studentLinked.delete(4);
        studentLinked.list();
    }
}

class StudentLinked{
    //定义头节点
    private Node head = new Node(0,"");

    //遍历双向链表
    public void list(){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        Node temp = head.next;
        while (true){
            if (temp == null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    //添加节点
    public void addNode(Node newNode){
        Node temp = head;
        while(temp.next!=null){
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.front = temp;
    }

    //删除节点
    public void delete(int no){
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        Node temp = head.next;
        boolean flag = true;
        while (temp!=null){
            if (temp.no == no){
                if (temp.next!=null){
                    temp.next.front = temp.front;
                }
                temp.front.next = temp.next;
                flag =false;
            }
            temp = temp.next;
        }
        if (flag){
            System.out.println("未找到节点");
        }
    }

    //更改节点的信息
    public void updateNode(Node newNode) {
        if (head.next == null){
            System.out.println("队列为空,无法更改");
            return;
        }
        Node temp = head.next;
        boolean flag = false;
        while (true){
            if (temp == null){
                break;
            }
            if (temp.no == newNode.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.name = newNode.name;
        }else {
            System.out.printf("没有找到编号为%d的节点,无法修改\n",newNode.no);
        }
    }


}

//定义节点的类
class Node{
    public int no;
    public String name;
    public Node next;//指向下一个节点
    public Node front;//指向上一个节点

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

  

本文来自博客园,作者:腹白,转载请注明原文链接:https://www.cnblogs.com/wyh518/

相关栏目:

用户点评