顺序表的问题及思考
1. 顺序表中间/头部的插入删除,时间复杂度为o(n)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续 插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
思考: 如何解决以上问题呢?下面给出了链表的结构来看看。
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
链表结构分为8种:
这里我们只讲最下面两种,因为在工作中、业务里、考试题、刷到的链表题、面试题里面都是用到这两种链表。
链表是由一个一个节点组成的。(这里我们以单链表为例)
什么叫做节点?
节点分为两个域 ,假设一个叫做val域,一个叫做next域。
val:数据域
next:下一个节点的地址
//listnode代表一个节点 class listnode{ public int val; public listnode next; //构造方法 public listnode(int val){ this.val = val; }}
//mylinkedlist 代表这是一个链表 public class mylinkedlist { public listnode head;//链表的头引用,所以定义在链表里,head是链表的头,不是节点的头,节点只有两个属性,一个属性是val值,一个属性是next值,所以不能定义在listnode类里面 listnode listnode = new listnode(2);//节点实例化,val域赋值为2}
/mylinkedlist 代表这是一个链表public class mylinkedlist { public listnode head;//链表的头引用,所以定义在链表里 public void createlist(){ listnode listnode0 = new listnode(11); listnode listnode1 = new listnode(26); listnode listnode2 = new listnode(23); listnode listnode3 = new listnode(45); listnode listnode4 = new listnode(56); listnode0.next = listnode1; listn长春藤ode1.next = listnode2; listnode2.next = listnode3; listnode3.next = listnode4; this.head = listnode0; }
//打印链表 public void display(){ listnode cur = this.head; while (cur != null){ system.out.print(cur.val+" "); cur = cur.next; } system.out.println(); }
打印结果:
//查找是否包含关键字key是否在单链表当中 public boolean contains(int key) { listnode cur = this.head; while (cur != null) { if (cur.val == key) { return true; } cur = cur.next; } return fal; }
打印结果:
//得到单链表的长度 public int size(){ listnode cur = this.head; int count描写季节的成语 = 0; while(cur != null){ count++; cur = cur.next; } return count; }
打印结果:
//头插法 public void addfirst(int data) { listnode node = new listnode(data); if (this.head == null) { this.head = node; } el { node.next = this.head; head = node; } }
打印结果:
//尾插法 public void addlast(int data){ l京东大溶洞istnode node = new listnode(data); listnode cur = this.head; if(this.head == null){ this.head = node; }el { while(cur.next != null){ cur = cur.next; } cur.next = node; } }
打印结果:
public listnode findindex(int index){ listnode cur = this.head; while(ind抗震救灾 众志成城ex -1 != 0){ cur = cur.next; index--; } return cur; } //任意位置插入,第一个数据节点为0号下标 public void addindex(int index,int data){ if(index < 0 || index > size()){ system.out.println("位置不合法"); return; } if(index == 0){ addfirst(data); return; } if(index == size()){ addlast(data); return; } listnode cur = findindex(index); listnode node = new listnode(data); node.next = cur.next; cur.next = node; }
打印结果:
//删除第一次出现关键字为key的节点 public void remove(int key){ if(this.head == null){ system.out.println("没有你要删除的节"); return; } if (this.head.val == key){ this.head = this.head.next; return; } listnode cur = this.head; while (cur.next != null){ if(cur.next.val == key){ cur.next = cur.next.next; return; } cur = cur.next; } if(cur.next == null){ system.out.println("没有该节点"); return; } }
打印结果:
//删除所有值为key的节点 public listnode removeallkey(int key){ if(this.head == null) return null; listnode prev = this.head; listnode cur = this.head; while (cur != null){ if(cur.val == key){ prev.next = cur.next; cur = cur.next; }el{ prev = cur; 晋文公 cur = cur.next; } } if(this.head.val == key){ this.head = this.head.next; } return this.head; }
打印结果:
本文简单介绍了数据结构的链表,如何创建链表,链表上如何操作数据。通过简单例题的方式加深对顺序表的理解。上述就是今天的内容,有任何疑问的话可以随时私信我,文章哪里出现了问题我都会积极改正,也希望大家能更快的掌握自己想要的知识,让我们一起加油!!!!!
到此这篇关于java精炼解读数据结构的链表的概念与实现的文章就介绍到这了,更多相关java 数据结构的链表内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-06 03:41:02,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/69ffb02b5d7911a8c42101844dd92ff1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java 精炼解读数据结构的链表的概念与实现.doc
本文 PDF 下载地址:Java 精炼解读数据结构的链表的概念与实现.pdf
留言与评论(共有 0 条评论) |