博客
关于我
数据结构 链表的各种插入
阅读量:377 次
发布时间:2019-03-05

本文共 1424 字,大约阅读时间需要 4 分钟。

链表插入方法的实现与分析

链表作为一种高效的数据结构,因其灵活性和插入效率而受到广泛关注。链表的插入操作时间复杂度均为O(1),无需考虑增容问题,仅需为插入节点分配内存空间即可完成。以下将详细介绍链表在不同位置的插入方法。

  • 中间位置插入
  • 中间位置插入是链表操作中常见的一种插入方式。插入位置由用户提供下标决定,具体实现步骤如下:

    public static Node MidInsertion(Node head, Node Insert, int index) {Node cur = head;if (index < 1 || index > Length(head)) {return cur; // 插入位置越界,返回原头节点}for (int i = 0; i < index; i++) {cur = cur.next;}cur.next = Insert;return head;}

    需要注意的是,在循环结束后,cur指向插入位置的下一个节点,此时只需将该节点的next指向插入节点即可完成操作,需注意循环过程中不得打乱原有节点的指向关系。

    1. 头部插入
    2. 头部插入是链表插入中最简单的一种操作。插入节点直接作为新的头节点,实现方式如下:

      public static Node HeadInsertion(Node head, Node Insert) {Node cur = head;Insert.next = cur;cur = Insert;return cur;}

      在这种插入方式下,只需将插入节点的next指向原有头节点即可,无需遍历链表,操作简单高效。

      1. 尾部插入
      2. 尾部插入是链表中另一种常见插入方式,插入位置位于链表末尾。具体实现步骤如下:

        public static Node LastInsertion(Node head, Node Insert) {Node cur = head;while (cur.next != null) {cur = cur.next;}cur.next = Insert;return head;}

        尾部插入需要先遍历链表,找到末尾节点,然后将插入节点挂在其后。这种插入方式适用于需要将节点追加至链表末尾的场景。

        1. 随机位置插入
        2. 在某些应用中,可能需要在链表的随机位置插入节点。这种插入方式与中间位置插入类似,但插入位置由具体需求决定。具体实现与中间位置插入方法一致。

          1. 插入操作的验证
          2. 为了验证上述插入方法的正确性,可以编写以下验证代码:

            public static void main(String[] args) {Node head = CreateLink();print(head); // 打印初始链表Node Insert = new Node(10);MidInsertion(head, Insert, 3);print(head); // 打印中间插入后的链表Node Insert1 = new Node(10);head = HeadInsertion(head, Insert1);print(head); // 打印头部插入后的链表Node Insert2 = new Node(10);LastInsertion(head, Insert2);print(head); // 打印尾部插入后的链表}

            通过上述验证代码,可以直观地观察链表在不同插入位置后的状态。

    转载地址:http://tsog.baihongyu.com/

    你可能感兴趣的文章
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>