当前位置:   article > 正文

Java实现链表_java链表

java链表

在Java编程中,顺序表是一种基础且重要的数据结构。它通常用来表示线性结构数据,如数组等。通过使用顺序表,我们可以轻松管理和操作大量的数据,并实现各种算法和功能。


一、认识List

List是一个接口,继承自Collection, Collection同样是接口。

List就是一个线性表,即n个具有相同类型元素的有限序列,在该序列上可以执行增删 改查以及变量等操作。

二、常见的Collection中有那些方法?

这可是面试题常问的哦!

具体如下:

static void swap(List list, int i, int j)将指定列表中的两个索引进行位置互换
static void  sort(List<T> list)按照列表中元素的自然顺序进行排序
static void shuffle(List list)随机置换
static void reverse(List list)反转
static void fill(List list, Object obj)使用指定的对象填充指定列表的所有元素
static void copy(List dest, List src)是把源列表中的数据覆盖到目标列表
static int  binarySearch(List list, Object key)使用二分查找法查找指定元素在指定列表的索引位置

三、线性表

线性表(底层就是数组):如顺序表、栈、队列等

逻辑上是线性结构,物理结构上不一定是连续的,通常以数组和链式结构的形式存储

  • 方法空白代码给你们放出来了,小伙伴们可以自己动手试试哦
  1. public class SeqList {
  2. private int[] array;
  3. private int size;
  4. // 默认构造方法
  5. SeqList(){ }
  6. // 将顺序表的底层容量设置为initcapacity
  7. SeqList(int initcapacity){ }
  8. // 新增元素,默认在数组最后新增
  9. public void add(int data) { }
  10. // 在 pos 位置新增元素
  11. public void add(int pos, int data) { }
  12. // 判定是否包含某个元素
  13. public boolean contains(int toFind) { return true; }
  14. // 查找某个元素对应的位置
  15. public int indexOf(int toFind) { return -1; }
  16. // 获取 pos 位置的元素
  17. public int get(int pos) { return -1; }
  18. // 给 pos 位置的元素设为 value
  19. public void set(int pos, int value) { }
  20. //删除第一次出现的关键字key
  21. public void remove(int toRemove) { }
  22. // 获取顺序表长度
  23. public int size() { return 0; }
  24. // 清空顺序表
  25. public void clear() { }
  26. // 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
  27. public void display() { }
  28. }

实现线性表:

  • 遍历单链表:
  1. //遍历单链表
  2. public void show() {
  3. //这里不是定义了一个节点 这里只是一个引用
  4. ListNode cur = head;
  5. while (cur != null) {
  6. System.out.print(cur.val+" ");
  7. cur = cur.next;
  8. }
  9. System.out.println();
  10. }
  11. public void show(ListNode newHead) {
  12. //这里不是定义了一个节点 这里只是一个引用
  13. ListNode cur = newHead;
  14. while (cur != null) {
  15. System.out.print(cur.val+" ");
  16. cur = cur.next;
  17. }
  18. System.out.println();
  19. }

 

  • 链表的长度:
  1. //得到单链表的长度 --》 链表中节点的个数
  2. public int size(){
  3. int count = 0;
  4. ListNode cur = head;
  5. while (cur != null) {
  6. count++;
  7. cur = cur.next;
  8. }
  9. return count;
  10. }

 

  •  查找是否包含关键字key是否在单链表当中:
  1. //查找是否包含关键字key是否在单链表当中
  2. public boolean contains(int key){
  3. ListNode cur = head;
  4. while (cur != null) {
  5. //如果val值 是引用类型 那么这里得用equals来进行比较!!!
  6. if(cur.val == key) {
  7. return true;
  8. }
  9. cur = cur.next;
  10. }
  11. return false;
  12. }

 

  • 头插法&&尾插法:
  1. //头插法
  2. public void addFirst(int data){
  3. ListNode node = new ListNode(data);
  4. node.next = head;
  5. head = node;
  6. }
  7. //尾插法
  8. public void addLast(int data){
  9. ListNode node = new ListNode(data);
  10. if(head == null) {
  11. head = node;
  12. return;
  13. }
  14. ListNode cur = head;
  15. while (cur.next != null) {
  16. cur = cur.next;
  17. }
  18. //cur 指向的节点就是尾巴节点
  19. cur.next = node;
  20. }

 

  •  任意位置插入,第一个数据节点为0号下标
  1. //任意位置插入,第一个数据节点为0号下标
  2. public void addIndex(int index,int data){
  3. int len = size();
  4. //0、判断index位置的合法性
  5. if(index < 0 || index > len) {
  6. throw new IndexOutOfBounds("任意位置插入数据的时候,index位置不合法: "+index);
  7. }
  8. if(index == 0) {
  9. addFirst(data);
  10. return;
  11. }
  12. if(index == len) {
  13. addLast(data);
  14. return;
  15. }
  16. //1、先找到index-1位置的节点
  17. ListNode cur = findIndex(index);
  18. //2、进行插入
  19. ListNode node = new ListNode(data);
  20. node.next = cur.next;
  21. cur.next = node;
  22. }
  •  删除所有值为key的节点
  1. //删除所有值为key的节点
  2. public void removeAllKey(int key){
  3. if(head == null) {
  4. return;
  5. }
  6. ListNode cur = head.next;
  7. ListNode prev = head;
  8. while (cur != null) {
  9. if(cur.val == key) {
  10. prev.next = cur.next;
  11. cur = cur.next;
  12. }else {
  13. prev = cur;
  14. cur = cur.next;
  15. }
  16. }
  17. if(head.val == key) {
  18. head = head.next;
  19. }
  20. }
  •     完整代码

    1. import java.util.List;
    2. import java.util.Stack;
    3. public class MySingleList {
    4. static class ListNode {
    5. public int val;
    6. public ListNode next;
    7. public ListNode(int val) {
    8. this.val = val;
    9. }
    10. }
    11. public ListNode head;//永远指向我的头节点
    12. //遍历单链表
    13. public void show() {
    14. //这里不是定义了一个节点 这里只是一个引用
    15. ListNode cur = head;
    16. while (cur != null) {
    17. System.out.print(cur.val+" ");
    18. cur = cur.next;
    19. }
    20. System.out.println();
    21. }
    22. public void show(ListNode newHead) {
    23. //这里不是定义了一个节点 这里只是一个引用
    24. ListNode cur = newHead;
    25. while (cur != null) {
    26. System.out.print(cur.val+" ");
    27. cur = cur.next;
    28. }
    29. System.out.println();
    30. }
    31. //得到单链表的长度 --》 链表中节点的个数
    32. public int size(){
    33. int count = 0;
    34. ListNode cur = head;
    35. while (cur != null) {
    36. count++;
    37. cur = cur.next;
    38. }
    39. return count;
    40. }
    41. //查找是否包含关键字key是否在单链表当中
    42. public boolean contains(int key){
    43. ListNode cur = head;
    44. while (cur != null) {
    45. //如果val值 是引用类型 那么这里得用equals来进行比较!!!
    46. if(cur.val == key) {
    47. return true;
    48. }
    49. cur = cur.next;
    50. }
    51. return false;
    52. }
    53. //头插法
    54. public void addFirst(int data){
    55. ListNode node = new ListNode(data);
    56. node.next = head;
    57. head = node;
    58. }
    59. //尾插法
    60. public void addLast(int data){
    61. ListNode node = new ListNode(data);
    62. if(head == null) {
    63. head = node;
    64. return;
    65. }
    66. ListNode cur = head;
    67. while (cur.next != null) {
    68. cur = cur.next;
    69. }
    70. //cur 指向的节点就是尾巴节点
    71. cur.next = node;
    72. }
    73. //任意位置插入,第一个数据节点为0号下标
    74. public void addIndex(int index,int data){
    75. int len = size();
    76. //0、判断index位置的合法性
    77. if(index < 0 || index > len) {
    78. throw new IndexOutOfBounds("任意位置插入数据的时候,index位置不合法: "+index);
    79. }
    80. if(index == 0) {
    81. addFirst(data);
    82. return;
    83. }
    84. if(index == len) {
    85. addLast(data);
    86. return;
    87. }
    88. //1、先找到index-1位置的节点
    89. ListNode cur = findIndex(index);
    90. //2、进行插入
    91. ListNode node = new ListNode(data);
    92. node.next = cur.next;
    93. cur.next = node;
    94. }
    95. //删除所有值为key的节点
    96. public void removeAllKey(int key){
    97. if(head == null) {
    98. return;
    99. }
    100. ListNode cur = head.next;
    101. ListNode prev = head;
    102. while (cur != null) {
    103. if(cur.val == key) {
    104. prev.next = cur.next;
    105. cur = cur.next;
    106. }else {
    107. prev = cur;
    108. cur = cur.next;
    109. }
    110. }
    111. if(head.val == key) {
    112. head = head.next;
    113. }
    114. }
    115. public void clear() {
    116. //this.head = null;
    117. while (head != null) {
    118. ListNode headNext = head.next;
    119. head.next = null;
    120. head = headNext;
    121. }
    122. }


总结


以上就是今天要讲的内容,本文仅仅简单介绍了链表的基本知识与链表的实现,链表是数据结构的开门红,好好理解,慢慢消化,多打代码,坚持住,别放弃。

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/823026
推荐阅读
相关标签
  

闽ICP备14008679号