Java--算法--双向链表

  1. 双向链表创建:
  2. 双向链表的实际应用:

  3. package com.model.linkedlist.doubly;
    
    /**
     * @Description:测试类
     * @Author: 张紫韩
     * @Crete 2021/7/9 9:57
     * 双向链表的介绍
     */
    public class DoublyLinkedListDemo01 {
        public static void main(String[] args) {
            DoublyLinkedList list=new DoublyLinkedList();
            list.add(new Node(1,"a"));
            list.add(new Node(2,"a"));
            list.add(new Node(3,"a"));
            list.add(new Node(4,"a"));
            list.add(new Node(5,"a"));
            list.show();
            list.delete(new Node(1,"a"));
            list.show();
            list.update(new Node(2,"张紫韩"));
            list.show();
            DoublyLinkedList list1 = new DoublyLinkedList();
            list1.addInOrder(new Node(3,"a"));
            list1.addInOrder(new Node(2,"a"));
            list1.addInOrder(new Node(1,"a"));
            list1.addInOrder(new Node(4,"a"));
            System.out.println("按顺序添加节点");
            list1.show();
        }
    }
    //双向链表类
    class DoublyLinkedList{
        private Node head=new Node();
        public Node getHead() {
            return head;
        }
        //添加元素,到链表的最后端
        public void add(Node node){
            Node temp=head;
            while(true){
                if (temp.next==null){
                    break;
                }
                temp=temp.next;
            }
            temp.next=node;
            node.pre=temp;
        }
    //    按这id号的大小进行添加
        public void addInOrder(Node node){
            Node temp=head;
            boolean flag=false;//是否可以进行添加
            while(true){
                if (temp.next==null){
                    break;
                }
                if (temp.next.id>node.id){
                    break;
                }
                if (temp.next.id==node.id){
                    flag=false;
                    break;
                }
                temp=temp.next;
            }
            if (flag){
                System.out.println("已经存在此节点,不能进行重复的添加");
    
            }else {
                //判断是加在链表的最后还是链表的中间部分
                if (temp.next==null) {
                    temp.next = node;
                    node.pre = temp;
                }else {
                    node.next = temp.next;
                    node.pre = temp;
                    temp.next = node;
                    node.next.pre = node;
                }
            }
        }
        //删除元素
        public void delete(Node node){
            if (isEmpty()){
                System.out.println("链表为空不能进行删除");
                return;
            }
            Node temp=head.next;
            boolean flag=false;
            while (true){
                if (temp==null){
                    break;
                }
                if (temp.id==node.id){
                    flag=true;
                    break;
                }
                temp=temp.next;
            }
            if (flag){
                temp.pre.next=temp.next;
                temp.next.pre=temp.pre;
            }else {
                System.out.println("没有找到此节点不能进行删除");
            }
    
        }
    //    修改节点
        public void update(Node node){
            if (isEmpty()){
                System.out.println("链表为空,不能进行修改");
                return;
            }
            Node temp=head.next;
            boolean flag=false;
            while (true){
                if (temp==null){
                    break;
                }
                if (temp.id==node.id){
                    flag=true;
                    break;
                }
                temp=temp.next;
            }
            if (flag){
                temp.name=node.name;
            }else {
                System.out.println("没有找到此节点,不能进行修改");
            }
        }
    
    //    遍历链表
        public void show(){
            Node temp=head.next;
            while(true){
                if (temp==null){
                    break;
                }
                System.out.println(temp.toString());
                temp=temp.next;
            }
        }
    
        public boolean isEmpty(){
            if (head.next==null){
                return true;
            }
            return false;
        }
    
    }
    //双向链表的节点类
    class Node{
        public int id;
        public String name;
        public Node pre;
        public Node next;
    
        public Node() {
        }
    
        @Override
        public String toString() {
            return "Node{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    
        public Node(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }

     

     

      
posted @ 2021-07-09 11:09  张紫韩  阅读(43)  评论(0编辑  收藏  举报