乐观学习,乐观生存,才华不断进步啊!!!
我的主页:optimistic_chen
我的专栏:c语言 ,Java
欢迎大家访问~
创作不易,大佬们点赞鼓励下吧~
媒介
上篇博客详细写了ArrayList的相干问题,包括上图(极其紧张),我会在最近几篇博客中都有附上。
ArrayList的长处很明显,底层逻辑是一个数组,它通过下标去访问数据的速度非常快。但是在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低
以是java集合框架中引入了LinkedList类,即链表结构。
链表(MySingleList)
链表,作为线性表的一种,它与顺序表截然相反:链表是一种物理存储结构 上 非连续存储结构, 数据元素的逻辑顺序是通过链表中的引用 链接序次实现的
之前C语言相识过这方面的知识C语言—链表专题,以是我们有一定的基础,我相信链表对于大家一定是熟悉的存在。我们先由易入难,先本身实验一下单链表(MySingleList)的功能实现,为接下来的 LinkedList(无头双向链表) 打下基础.
接口
- public interface IList {
- public void addFirst(int data);
- public void addLast(int data);
- public void addIndex(int index,int data);
- public boolean contains(int key);
- public void remove(int key);
- public void removeAllKey(int key);
- public int size();
- public void clear();
- public void display();
- }
复制代码 详细功能代码
头插法
- @Override
- public void addFirst(int data) {
- ListNode node = new ListNode(data);
- node.next=head;
- head=node;
- }
复制代码 尾插法
- @Override
- public void addLast(int data) {
- ListNode node =new ListNode(data);
- if(head==null){
- head = node;
- return;
- }
- ListNode cur=head;
- while(cur.next!=null){
- cur= cur.next;
- }
- cur.next=node;
- }
复制代码 任意位置插入,第一个数据节点为0号下标
- @Override
- public void addIndex(int index, int data) {
- int len=size();
- if(index<0||index>len){
- System.out.println("index位置不合法");
- return;
- }
- if(index==0){
- addFirst(data);
- return;
- }
- if(index==len){
- addLast(data);
- return;
- }
- ListNode cur=head;
- while(index-1!=0){
- cur=cur.next;
- index--;
- }
- ListNode node=new ListNode(data);
- node.next= cur.next;
- cur.next=node;
- }
复制代码 查找是否包含关键字key是否在单链表当中
- @Override
- public boolean contains(int key) {
- ListNode cur=head;
- while(cur!=null){
- if(cur.val==key){
- return true;
- }
- cur=cur.next;
- }
- return false;
- }
复制代码 删除第一次出现关键字为key的节点
- @Override
- public void remove(int key) {
- if(head==null){
- return;
- }
- if(head.val==key){
- head=head.next;
- return;
- }
- ListNode cur=findNodeOfKey(key);
- if(cur==null){
- return;
- }
- ListNode del= cur.next;
- cur.next=del.next;
- }
- private ListNode findNodeOfKey(int key){
- ListNode cur=head;
- while(cur.next!=null) {
- if (cur.next.val == key) {
- return cur;
- }
- cur= cur.next;
- }
- return null;
- }
复制代码 删除所有值为key的节点
- @Override
- public void removeAllKey(int key) {
- if(head==null){
- return;
- }
- ListNode prev=head;
- ListNode cur=head.next;
- while(cur!=null){
- if(cur.val==key){
- prev.next=cur.next;
- cur= cur.next;
- }else{
- prev=cur;
- cur= cur.next;
- }
- if(head.val==key){
- head=head.next;
- return;
- }
- }
- }
复制代码 得到单链表的长度
- @Override
- public int size() {
- int len=0;
- ListNode cur=head;
- while(cur!=null){
- len++;
- cur=cur.next;
- }
- return len;
- }
复制代码 清空链表
- @Override
- public void clear() {
- ListNode cur=head;
- while(cur!=null){
- ListNode curN=cur.next;
- cur.next=null;
- cur=curN;
- }
- head=null;
- }
复制代码 LinkedList简介
LinkedList的底层是双向链表结构,由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点毗连起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。
总结:
- LinkedList实现了List接口
- LinkedList的底层使用了双向链表
- LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问
- LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)
- LinkedList比较得当任意位置插入的场景
LinkedList的模仿实现
头插法
- @Override
- public void addFirst(int data) {
- ListNode node=new ListNode(data);
- if(head==null){
- head=last=node;
- }else{
- node.next=head;
- head.prev=node;
- head=node;
- }
- }
复制代码 尾插法
- @Override
- public void addLast(int data) {
- ListNode node=new ListNode(data);
- if(head==null){
- head=last=node;
- }else{
- last.next=node;
- node.prev=last;
- last= last.next;
- }
- }
复制代码 任意位置插入,第一个数据节点为0号下标
- @Override
- public void addIndex(int index, int data) {
- int len=size();
- if(index<0||index>len){
- return;
- }
- if(index==0){
- addFirst(data);
- return;
- }
- if(index==len){
- addLast(data);
- return;
- }
- ListNode cur=findIndex(index);
- ListNode node=new ListNode(data);
- node.next=cur;
- cur.prev.next=node;
- node.prev=cur.prev;
- cur.next=node;
- }
- private ListNode findIndex(int index){
- ListNode cur=head;
- while(index!=0){
- cur= cur.next;
- index--;
- }
- return cur;
- }
复制代码 查找是否包含关键字key是否在链表当中
- @Override
- public boolean contains(int key) {
- ListNode cur=head;
- int count=0;
- while(cur!=null){
- if(cur.val==key) {
- return true;
- }
- cur= cur.next;
- }
- return false;
- }
复制代码 删除第一次出现关键字为key的节点
- @Override
- public void remove(int key) {
- ListNode cur=head;
- while(cur!=null){
- //开始删除
- if(cur.val==key){
- //有可能是头节点
- if(cur==head){
- head = head.next;
- if(head!=null){
- head.prev=null;
- }
- }else{
- cur.prev.next= cur.next;
- if(cur.next==null){
- //有可能是尾节点
- last= last.prev;
- }else{
- cur.next.prev= cur.prev;
- }
- }
- return;
- }else{
- cur = cur.next;
- }
- }
- }
复制代码 删除所有值为key的节点
- @Override
- public void removeAllKey(int key) {
- ListNode cur=head;
- while(cur!=null){
- //开始删除
- if(cur.val==key){
- //有可能是头节点
- if(cur==head){
- head = head.next;
- if(head!=null){
- head.prev=null;
- }
- }else{
- cur.prev.next= cur.next;
- if(cur.next==null){
- //有可能是尾节点
- last= last.prev;
- }else{
- cur.next.prev= cur.prev;
- }
- }
- }else{
- cur = cur.next;
- }
- }
- }
复制代码 LinkedList的使用
前面我们自主实现了LinkedList的各种方法,只是为了大家更好的明白和学习,以后做题大概会运用到类似的想法或思路,可以更加高效的完成标题,平时也可以直接使用LinkedList下的方法。如下图:
LinkedList的构造
- public static void main(String[] args) {
- List<Integer> list1=new LinkedList<Integer>();
- LinkedList<Integer> list2 = new LinkedList<Integer>();
-
- //使用ArrayList构造LinkedList
- List<String> list3=new ArrayList<>();
- List<String> list4=new LinkedList<>(list3);
- }
复制代码 为什么可以有的构造可以用List,有的用LinkedList,甚至出现了ArrayList?一切都是这张贯穿整个数据结构的图。
LinkedList的方法
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<>();
- list.add(1); // add(): 表示尾插
- list.add(2);
- list.add(3);
- list.add(4);
- list.add(5);
- list.add(6);
- list.add(7);
- System.out.println(list.size());
- System.out.println(list);
- // subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回
- List<Integer> list2 = list.subList(0, 3);
- System.out.println(list);
- System.out.println(list2);
- list.clear(); // 将list中元素清空
- System.out.println(list.size());
- }
复制代码 LinkedList的遍历
之前ArrayList博客【Java数据结构】—List(ArrayList) 讲过线性表的遍历,这次补充一下…
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<>();
- list.add(1); // add(): 表示尾插
- list.add(2);
- list.add(3);
- list.add(4);
- list.add(5);
- list.add(6);
- list.add(7);
- System.out.println(list.size());
-
- // 使用反向迭代器---反向遍历
- ListIterator<Integer> it = list.listIterator(list.size());
- while (it.hasPrevious()){
- System.out.print(it.previous() +" ");
- }
- }
复制代码 ArrayList和LinkeddList的区别
不同ArrayListLinkedList存储空间物理上一定连续逻辑上连续,但物理上不一定连续随机访问支持O(1)不支持O(n)头插空间不够时扩容不存在容量的概念运用场景元素高效存储+频仍访问频仍任意位置的插入和删除 完结
好了,到这里Java语法部分就已经结束了~
如果这个系列博客对你有帮助的话,可以点一个免费的赞并收藏起来哟~
可以点点关注,避免找不到我~ ,我的主页:optimistic_chen
我们下期不见不散~~Java
下期预告: 【Java数据结构】- - - List
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |