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

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

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

链表的任意的位置插入时间复杂度都是O(1),没有增容问题,插入一个就开辟一个空间,这是链表相对于顺序表的优点,那么我们就来看看这个各种插入是怎样插入的。

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

因为输入了要插入位置的下标,那就得到个位置结束for循环,这时候cur就指向该下标的的节点了,这时候修改要插入的节点的指向就可以了,注意这里修改的循序不能打乱。

2.头部插入

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

头部插入这里只需要更改指向就可以了,不用在进行遍历了,使头结点的引用指向要插入的结点,插入的节点的next指向原来的头结点。

3.尾部插入

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

尾插入需要先走到链表的末尾,然后只要让原来指向null的最后一个节点,指向要插入的节点就可以了。

验证

public static void main(String[] args) {           Node head=GreateLink();        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/

你可能感兴趣的文章
nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
查看>>
nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
查看>>
Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
查看>>
nginxWebUI runCmd RCE漏洞复现
查看>>
nginx_rtmp
查看>>
Vue中向js中传递参数并在js中定义对象并转换参数
查看>>
Nginx、HAProxy、LVS
查看>>
nginx一些重要配置说明
查看>>
Nginx一网打尽:动静分离、压缩、缓存、黑白名单、跨域、高可用、性能优化......
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx与Tengine安装和使用以及配置健康节点检测
查看>>
Nginx中使用expires指令实现配置浏览器缓存
查看>>
Nginx中使用keepalive实现保持上游长连接实现提高吞吐量示例与测试
查看>>
Nginx中如何配置WebSocket代理?
查看>>
Nginx中实现流量控制(限制给定时间内HTTP请求的数量)示例
查看>>
nginx中配置root和alias的区别
查看>>
nginx主要流程(未完成)
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
vue中各模块加载和渲染的过程
查看>>
Nginx从入门到精通
查看>>