• 双端链表


    双端链表的概念:链表中保存着对最后一个节点引用的链表。

    1. 链表头部插入

    如果链表为空,要对链表尾部处理

    Element el = new Element(data);
    if(first == null){
       last = el;
    }
    el.next = first;
    first=el;
    

     

    2. 链表尾部插入

    如果链表为空,要对头节点进行处理

    Element el = new Element(data);
    if(first == null){
        first = el;
    }else{
        last.next = el;
    }
    last = el;
    

      

    3. 链表头部删除节点

    Element el = first;
    if(el.next != null){
          last = null;
    }
    first = el.next;
    

      

    code

    public class Element {
        public Object object;
        public Element next;
    
        public Element(Object object){
            this.object = object;
        }
    
        public void display(){
            System.out.println(this.object);
        }
    }
    public class DoubleLinkList {
        Element first;
        Element last;
        public DoubleLinkList(){
            first = null;
            last = null;
        }
    
        //从头结点开始插入
        public void addFirst(int data){
            Element el = new Element(data);
            if(first == null){
                last = el;
            }
            el.next = first;
            first=el;
        }
    
        //从尾结点开始插入
        public void addLast(int data){
            Element el = new Element(data);
            if(first == null){
                first = el;
            }else{
                last.next = el;
            }
            last = el;
        }
    
        //从头节点开始删除
        public void deleteFirst(){
            Element el = first;
            if(el.next != null){
                last = null;
            }
    
            first = el.next;
        }
    
        public void display(){
            Element element = first;
            while (element != null){
                element.display();
                element=element.next;
            }
        }
    }
    public class Demo {
        public static void main(String[] args){
            DoubleLinkList dll = new DoubleLinkList();
            dll.addFirst(1);
            dll.addFirst(2);
            dll.addFirst(3);
            dll.addFirst(4);
            dll.addLast(6);
            dll.addFirst(5);
            dll.addLast(8);
            dll.display();
            System.out.println("删除元素之后");
            dll.deleteFirst();
            dll.display();
        }
    }
    View Code
  • 相关阅读:
    Node.Js安装教程
    使用Idea 配置maven
    Sublime Text3 使用记录
    配置Java 环境变量
    什么是应届生?要不要签三方?看看就知道了
    Python学习(二)——深度学习入门介绍
    python学习(一)——python与人工智能
    php学习(二)——html + css
    19、SOAP安装,运用与比对结果解释
    24、Linux 多线程压缩工具pigz 的学习
  • 原文地址:https://www.cnblogs.com/mutong1228/p/10770683.html
Copyright © 2020-2023  润新知