能起飞的数据结构成王第二篇数据结构的链表1
目录
前言:
顺序表的问题及思考
1. 顺序表中间/头部的插入删除,时间复杂度为O(N)
2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。
3. 增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续 插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。
思考: 如何解决以上问题呢?下面给出了链表的结构来看看。
一、什么是链表
1.链表的概念
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
2.链表的结构
链表结构分为8种:
这里我们只讲最下面两种,因为在工作中、业务里、考试题、刷到的链表题、面试题里面都是用到这两种链表。
链表如何存储数据
链表是由一个一个节点组成的。(这里我们以单链表为例)
什么叫做节点?
节点分为两个域 ,假设一个叫做val域,一个叫做next域。
val:数据域
next:下一个节点的地址
3.链表的实现
//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; listNode1.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是否在单链表当中
//查找是否包含关键字key是否在单链表当中 public boolean contains(int key) { ListNode cur = this.head; while (cur != null) { if (cur.val == key) { return true; } cur = cur.next; } return false; }打印结果:
得到单链表的长度:
//得到单链表的长度 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; } else { node.next = this.head; head = node; } }打印结果:
尾插法
//尾插法 public void addLast(int data){ ListNode node = new ListNode(data); ListNode cur = this.head; if(this.head == null){ this.head = node; }else { while(cur.next != null){ cur = cur.next; } cur.next = node; } }打印结果:
任意位置插入,第一个数据节点为0号下标
public ListNode findIndex(int index){ ListNode cur = this.head; while(index -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的节点
//删除第一次出现关键字为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的节点
//删除所有值为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; }else{ prev = cur; cur = cur.next; } } if(this.head.val == key){ this.head = this.head.next; } return this.head; }打印结果:
总结:
本文简单介绍了数据结构的链表,如何创建链表,链表上如何操作数据。通过简单例题的方式加深对顺序表的理解。上述就是今天的内容,有任何疑问的话可以随时私信我,文章哪里出现了问题我都会积极改正,也希望大家能更快的掌握自己想要的知识,让我们一起加油!!!!!
我与你同在。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfifekf
系列文章
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24