java实现简单的链式结构二(双向链表)

双向链表必要性 :单链表,不管是头插还是尾插都只是在增加头尾节点的时候能达到O(1),在知道某个具体中间节点的时候既不能直接增加节点,也不能 直接删除节点。原因就在于我们不知道该节点的前驱,导致不能修改前驱的next属性,如果在每一个节点加入前驱属性,就能在单独拿到一个节点的时候直接进行新增或删除操作,将新增删除已知任意节点的时间复杂度控制到O(1)。ps:表前驱元(previous)就是一个元素的前一个元素。后继元(next)就是一个元素的后一个元素

双向链表的实现:

分为 Node 节点类和DoublyLinkedList 双向链表类

/**
 * Created by 我来杀ai on 2019/4/11.
 */
public class Node {

    Node next = null;
    Node previous = null;
    // 节点的引用,指向下一个节点
    int data;

    // 节点的对象,即内容
    public Node(int data) {
        this.data = data;
    }

}
/**
 * Created by 我来杀ai on 2019/4/25.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 Copyright (c) <2019> <我来杀ai>

 "Anti 996" License Version 1.0 (Draft)

 Permission is hereby granted to any individual or legal entity
 obtaining a copy of this licensed work (including the source code,
 documentation and/or related items, hereinafter collectively referred
 to as the "licensed work"), free of charge, to deal with the licensed
 work for any purpose, including without limitation, the rights to use,
 reproduce, modify, prepare derivative works of, distribute, publish
 and sublicense the licensed work, subject to the following conditions:

 1. The individual or the legal entity must conspicuously display,
 without modification, this License and the notice on each redistributed
 or derivative copy of the Licensed Work.

 2. The individual or the legal entity must strictly comply with all
 applicable laws, regulations, rules and standards of the jurisdiction
 relating to labor and employment where the individual is physically
 located or where the individual was born or naturalized; or where the
 legal entity is registered or is operating (whichever is stricter). In
 case that the jurisdiction has no such laws, regulations, rules and
 standards or its laws, regulations, rules and standards are
 unenforceable, the individual or the legal entity are required to
 comply with Core International Labor Standards.

 3. The individual or the legal entity shall not induce, suggest or force
 its employee(s), whether full-time or part-time, or its independent
 contractor(s), in any methods, to agree in oral or written form, to
 directly or indirectly restrict, weaken or relinquish his or her
 rights or remedies under such laws, regulations, rules and standards
 relating to labor and employment as mentioned above, no matter whether
 such written or oral agreements are enforceable under the laws of the
 said jurisdiction, nor shall such individual or the legal entity
 limit, in any methods, the rights of its employee(s) or independent
 contractor(s) from reporting or complaining to the copyright holder or
 relevant authorities monitoring the compliance of the license about
 its violation(s) of the said license.

 THE LICENSED WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM,
 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 OTHERWISE, ARISING FROM, OUT OF OR IN ANY WAY CONNECTION WITH THE
 LICENSED WORK OR THE USE OR OTHER DEALINGS IN THE LICENSED WORK.

 版权所有(c)<2019><我来杀ai>

 反996许可证版本1.0

 在符合下列条件的情况下,特此免费向任何得到本授权作品的副本(包括源代码、文件和/或相关内容,以
 下统称为“授权作品”)的个人和法人实体授权:被授权个人或法人实体有权以任何目的处置授权作品,包括
 但不限于使用、复制,修改,衍生利用、散布,发布和再许可:

 1. 个人或法人实体必须在许可作品的每个再散布或衍生副本上包含以上版权声明和本许可证,不得自行修
 改。
 2. 个人或法人实体必须严格遵守与个人实际所在地或个人出生地或归化地、或法人实体注册地或经营地(
 以较严格者为准)的司法管辖区所有适用的与劳动和就业相关法律、法规、规则和标准。如果该司法管辖区
 没有此类法律、法规、规章和标准或其法律、法规、规章和标准不可执行,则个人或法人实体必须遵守国际
 劳工标准的核心公约。
 3. 个人或法人不得以任何方式诱导、暗示或强迫其全职或兼职员工或其独立承包人以口头或书面形式同意
 直接或间接限制、削弱或放弃其所拥有的,受相关与劳动和就业有关的法律、法规、规则和标准保护的权利
 或补救措施,无论该等书面或口头协议是否被该司法管辖区的法律所承认,该等个人或法人实体也不得以任
 何方法限制其雇员或独立承包人向版权持有人或监督许可证合规情况的有关当局报告或投诉上述违反许可证
 的行为的权利。

 该授权作品是"按原样"提供,不做任何明示或暗示的保证,包括但不限于对适销性、特定用途适用性和非侵
 权性的保证。在任何情况下,无论是在合同诉讼、侵权诉讼或其他诉讼中,版权持有人均不承担因本软件或
 本软件的使用或其他交易而产生、引起或与之相关的任何索赔、损害或其他责任。
 */
public class DoublyLinkedList {
    Node head = null;
    Node last = null;



    /**
     *   向链表中插入数据和单链表没区别
     */
    public Node addNode(int d) {
        Node newNode = new Node(d);
// 实例化一个节点
        if (head == null) {
            head = newNode;
            last = newNode;
            return newNode;
        }
        last.next = newNode;
        newNode.previous = last;
        last = newNode;
        return newNode;
    }

    /**
     *
     *  @param index:删除第index个节点
     *   删除尾部节点效率将变高从O(n)降为O(1)
     *    
     */
    public Boolean deleteNode(int index) {
        if (index < 1 || index > length()) {
            System.out.println("删除失败,没有有效节点链");
            return false;
        }
        //头结点删除
        if (index == 1) {
            head = head.next;
            System.out.println("删除成功,删除了头结点");
            return true;
        }
        //尾结点删除
        if(index == length()){
            last = last.previous;
            last.next = null;
            System.out.println("删除成功,删除了尾结点");
            return true;
        }

        int flag = 2;//标识,因为设置了前驱元素,所以当前元素开始就是第2个,删头元素的情况在上面写过了
        Node preNode = head;  //precursor前驱 preNode前一个节点
        Node curNode = preNode.next;  //current当前  curNode当前节点

        while (curNode != null) {
            if (index == flag) {

                preNode.next = curNode.next;
                System.out.println("删除成功,删除了第"+flag+"个节点");
                return true;
            }
            preNode = curNode;
            curNode = curNode.next;
            flag++;
        }
        System.out.println("删除错误");
        return false;
    }


    /**
     *  已知具体Node时删除节点
     */
    public Boolean deleteNode(Node  node) {
        if (node.previous==null){       //删除头结点时操作第二个结点的前驱为空
            head = head.next;
            node.next.previous = null;
            node = null;
        }else if (node.next==null){
            last = last.previous;           //删除尾结点时操作倒数第二个结点的后继为空
            node.previous.next = null;
            node = null;
        }else {
            node.previous.next = node.next;
            node.next.previous = node.previous;
        }





        return true;
    }


    /**
     *    *
     *    * @return 返回节点长度
     *    
     */
    public int length() {
        int length = 0;
        Node tmp = head;
        while (tmp != null) {
            length++;
            tmp = tmp.next;
        }
        return length;
    }

    /**
     * 打印链表
     */

    public void printList() {
        Node tmp = head;
        while (tmp != null) {
            System.out.print("->|-"+tmp.data);
            tmp = tmp.next;
        }
        System.out.println("-||");
    }

    public static void main(String[] args) {
        DoublyLinkedList list = new DoublyLinkedList();
        List<Node> listArr = new ArrayList<Node>();


        for (int i=0; i<100;i++){
            Random random = new Random();
            int i1 = random.nextInt(1000);
            if(i1>500){
                Node node = list.addNode(i1);
                listArr.add(node);
            }else {
                list.addNode(i1);
            }
        }
        list.printList();
        System.out.println("1:打印保存的节点数量"+listArr.size());
        System.out.println("2:链表长度:" + list.length());
        System.out.println("3:头结点:" + list.head.data);
        System.out.println("4:尾结点:" +list.last.data);


        list.printList();

        System.out.println(list.last.data);
        System.out.println("----------开始删除操作");
        System.out.println("打印保存的数");

        System.out.println("·1删除的节点数量"+listArr.size());
        for (int a= 0 ; a<listArr.size() ; a++){
            list.deleteNode(listArr.get(a));
            System.out.print(listArr.get(a).data+" ");
        }
        System.out.println("·2删除结束");
        System.out.println("·3:链表长度:" + list.length());
        System.out.println("·4:头结点:" + list.head.data);
        System.out.println("·5:尾结点:" +list.last.data);
        list.printList();

    }

}

node类得单独提出来不然无法根据Node去删除节点

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值