当前位置:   article > 正文

[数据结构] 链表(图文超详解讲解)_链表数据结构

链表数据结构

 

一、链表是什么?

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。

二、链表

1.链表的结构

链表的结构如图:

d7aedac5d7c84b73aca0572b3418987b.png

 

链表有8种结构:

1.单向带头循环 2.单向带头非循环. 3.单向不带头循环. 4.单向不带头非循环.

5.双向带头循环. 6.双向带头非循环. 7.双向不带头循环. 8.双向不带头非循环.

接下来介绍链表种主要的一些结构

1.单链表和双链表的结构如下:

d00201bcc92549a3a1089ac1ba8b1791.png

2.不带头单链表丶带头单链表结构:

7f7b849f0fee4f67be58e9b1c1ac9487.png

3.单链表丶循环单链表结构:

943ae14d8eb146ddb665d35c3fd84a9c.png

4. 无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。

1dce295c1ee04702a5a3fb9bbe74a7ce.png

2.链表的方法代码实现

1.单链表实现方法:

1.我们先创建链表的val值和next

  1. import java.util.Scanner;
  2. /**
  3. * Created with InteliJ IDEA.
  4. * /Description:
  5. * User:PANJIAPENG
  6. * Date:2022-08-11
  7. * Time:9:20
  8. */
  9. //节点
  10. class ListNode{
  11. public int val;
  12. public ListNode next;
  13. public ListNode (int val){
  14. this.val = val;
  15. }
  16. }

2.我们可以用最简单方法创建出一链表但是比较low,不建议使用,我们可以使用下面的头插法和尾插法来实现出链表

  1. public class MylLnkedList {
  2. public ListNode head;
  3. //创建链表 low
  4. public void Mff(){
  5. ListNode listNode1 = new ListNode(11);
  6. ListNode listNode2 = new ListNode(12);
  7. ListNode listNode3 = new ListNode(13);
  8. ListNode listNode4 = new ListNode(14);
  9. ListNode listNode5 = new ListNode(15);
  10. listNode1.next = listNode2;
  11. listNode2.next = listNode3;
  12. listNode3.next = listNode4;
  13. listNode4.next = listNode5;
  14. this.head = listNode1;
  15. }

3.打印链表:

  1. //打印链表
  2. public void display() {
  3. ListNode cur = this.head;
  4. while (cur != null) {
  5. System.out.print(cur.val+" ");
  6. cur = cur.next;
  7. }
  8. System.out.println();
  9. }

 4.求链表的长度:

  1. //求单链表长度
  2. public int size(){
  3. int count = 0;
  4. ListNode cur = this.head;
  5. while(cur != null){
  6. count++;
  7. cur = cur.next;
  8. }
  9. return count;
  10. }

5.头插法:

  1. //头插法
  2. public void addFirst(int data){
  3. ListNode node = new ListNode(data);
  4. if(this.head == null){
  5. this.head = node;
  6. }
  7. else{
  8. node.next = this.head;
  9. this.head = node;
  10. }
  11. }

 

6.尾插法:

  1. //尾插法
  2. public void addLast(int data){
  3. ListNode node = new ListNode(data);
  4. ListNode cur = this.head;
  5. if(this.head==null){
  6. this.head = node;
  7. }
  8. else{
  9. while (cur.next != null){
  10. cur = cur.next;
  11. }
  12. cur.next = node;
  13. }
  14. }

7.找到链表中是否有val值:

  1. //找链表中是否有这个数
  2. public boolean contains(int key){
  3. ListNode cur = this.head;
  4. while(cur != null){
  5. if(key == cur.val){
  6. return true;
  7. }
  8. cur = cur.next;
  9. }
  10. return false;
  11. }

8.删除第一次出在链表出现的key的关节点:

  1. //找删除的前驱
  2. public ListNode searchPery(int key){
  3. ListNode cur = this.head;
  4. while(cur.next != null) {
  5. if (cur.next.val == key) {
  6. return cur;
  7. }
  8. cur = cur.next;
  9. }
  10. return null;
  11. }
  12. //删除第一次出现的关键字为key的节点
  13. public void remove(int key){
  14. if (this.head == null){
  15. System.out.println("单链表为空,不能删除");
  16. return;
  17. }
  18. if(key == this.head.val){
  19. this.head = this.head.next;
  20. return;
  21. }
  22. ListNode cur = searchPery(key);
  23. if(cur == null){
  24. System.out.println("没有你要删除的节点");
  25. return ;
  26. }
  27. ListNode del = cur.next;
  28. cur.next = del.next;
  29. }

9.删除所有出在链表出现的key的关节点:

  1. //删除出现所以是key的节点
  2. public ListNode removeAllkey(int key){
  3. if(this.head == null){
  4. return null;
  5. }
  6. ListNode cur = this.head.next;
  7. ListNode pver = this.head;
  8. while (cur != null){
  9. if(cur.val == key){
  10. pver.next = cur.next;
  11. cur = cur.next;
  12. }
  13. else {
  14. pver = cur;
  15. cur = cur.next;
  16. }
  17. return this.head;
  18. }
  19. if(this.head.val == key){
  20. this.head = this.head.next;
  21. }
  22. return this.head;
  23. }

10.清空链表:

  1. //清空链表
  2. public void clear(){
  3. //粗暴做法 this.head = null;
  4. ListNode curNext = this.head.next;
  5. while(this.head != null){
  6. this.head.next = null;
  7. this.head = curNext;
  8. }
  9. }
  10. public boolean chkPalindrome(){
  11. if(this.head == null){
  12. return true;
  13. }
  14. ListNode last = this.head;
  15. ListNode slow = this.head;
  16. while (last != null && last.next != null){
  17. last = last.next.next;
  18. slow = slow.next;
  19. }
  20. ListNode cur = slow.next;
  21. ListNode curNext = cur.next;

2.双向链表的方法实现代码:在已经了解单链表的结构后我们就会发现单链表和双链表的实现是大同小异的

  1. class ListNode {
  2. public int val;
  3. public ListNode next;
  4. public ListNode prev;
  5. public ListNode(int val) {
  6. this.val = val;
  7. }
  8. }
  9. public class MyLinjedlist {
  10. public ListNode head;
  11. public ListNode last;
  12. //打印链表
  13. public void display() {
  14. ListNode cur = this.head;
  15. while (cur != null) {
  16. System.out.print(cur.val+" ");
  17. cur = cur.next;
  18. }
  19. System.out.println();
  20. }
  21. //得到链表的长度
  22. public int size() {
  23. int count = 0;
  24. ListNode cur = this.head;
  25. while (cur != null) {
  26. count++;
  27. cur = cur.next;
  28. }
  29. return count;
  30. }
  31. //查找是否包含关键字key是否在链表中
  32. public boolean contains(int key) {
  33. if (this.head == null) {
  34. return false;
  35. }
  36. ListNode cur = this.head;
  37. while (cur != null) {
  38. if (cur.val != key) {
  39. cur = cur.next;
  40. } else {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. //头插法
  47. public void addFirst(int data){
  48. ListNode node = new ListNode(data);
  49. if(this.head == null){
  50. this.head = node;
  51. this.last = node;
  52. }else {
  53. node.next = this.head;
  54. this.head.prev = node;
  55. this.head = node;
  56. }
  57. }
  58. //尾插法
  59. public void addLast(int data){
  60. ListNode node = new ListNode(data);
  61. if(this.head == null){
  62. this.head = node;
  63. this.last = node;
  64. }else {
  65. this.last.next = node;
  66. node.prev = this.last;
  67. this.last = node;
  68. }
  69. }
  70. //删除第一次出现关键字为key的节点
  71. public void remove(int key){
  72. ListNode cur = this.head;
  73. if(this.head == null){
  74. System.out.println("此链表为空,没有要删除的key值");
  75. return;
  76. }
  77. while(cur != null){
  78. if(cur.val == key){
  79. if(cur == head){
  80. this.head = this.head.next;
  81. if(head != null) {
  82. head.prev = null;
  83. }
  84. else {
  85. last = null;
  86. }
  87. }
  88. else {
  89. cur.prev.next = cur.next;
  90. if (cur.next != null) {
  91. //中间位置
  92. cur.next.prev = cur.prev;
  93. } else {
  94. last = last.prev;
  95. }
  96. }
  97. return;
  98. }
  99. cur = cur.next;
  100. }
  101. }
  102. //删除所以key的节点
  103. public void removeAll(int key){
  104. ListNode cur = this.head;
  105. if(this.head == null){
  106. System.out.println("此链表为空,没有要删除的key值");
  107. return;
  108. }
  109. while(cur != null){
  110. if(cur.val == key){
  111. if(cur == head){
  112. this.head = this.head.next;
  113. if(head != null) {
  114. head.prev = null;
  115. }
  116. else {
  117. last = null;
  118. }
  119. }
  120. else {
  121. cur.prev.next = cur.next;
  122. if (cur.next != null) {
  123. //中间位置
  124. cur.next.prev = cur.prev;
  125. } else {
  126. last = last.prev;
  127. }
  128. }
  129. }
  130. cur = cur.next;
  131. }
  132. }
  133. //任意位置插入,第一个数据节点默认为0下标
  134. public void addIndex(int index,int data){
  135. ListNode node = new ListNode(data);
  136. if(index == 0){
  137. addFirst(data);
  138. return;
  139. }
  140. if (index == size()-1){
  141. addLast(data);
  142. return;
  143. }
  144. ListNode cur = this.head;
  145. while (index != 0){
  146. index--;
  147. cur = cur.next;
  148. }
  149. node.next = cur;
  150. node.prev= cur.prev;
  151. cur.prev.next = node;
  152. cur.prev = node;
  153. }
  154. //清理链表
  155. public void clear(){
  156. while (head != null){
  157. ListNode curNext = this.head.next;
  158. head.next = null;
  159. head.prev = null;
  160. head = curNext;
  161. }
  162. last = null;
  163. }
  164. }

总结

根据读完本文后大家会对链表有更深的理解,并且能够掌握对方法的深层理解,相信大家在过后时间能够保持求学的态度,我们一起加油!offer拿到手软!对博主感兴趣可以关注博主,我会继续更新博客,努力完善我的文章内容不会辜负大家的期待!

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/510195
推荐阅读
相关标签
  

闽ICP备14008679号