• 数据结构-链表的简单操作代码实现3-LinkedList【Java版】


    写在前:

    本篇博客主要介绍关于双向链表的一些简答操作实现,其中有有部分代码的实现和前两篇博客中的单向链表是相类似的。例如:查找链表中是否包含关键字key、求链表的长度等。

    其余的涉及到prev指向的需要特别注意,区分和单向链表之间的差异。

    目录

    写在前:

    0.首先定义一个结点

    1.双向链表-头插法

    2.双向链表-尾插法

    3.双向链表-任意位置插

    4.打印双向链表

    5.求双向链表的长度

    6.找到双向链表中下标为index的元素

    7.查找双向链表中是否包含关键字key

    8.删除双向链表中第一个值为key的结点

    9.删除双向链表中所有值为key的结点

    10.清空双向链表


    0.首先定义一个结点

    实现双向链表,首先需要自己定义一个结点,结点中包含三个域:val、prev、next;

    定义头结点的引用head

    定义尾结点的引用last

    1. static class ListNode{
    2. public int val;
    3. public ListNode prev;//前驱
    4. public ListNode next;//后继
    5. //提供一个构造方法
    6. public ListNode(int val){
    7. this.val = val;
    8. }
    9. }
    10. public ListNode head;//代表当前链表头结点的引用
    11. public ListNode last;//代表当前链表尾结点的引用

    1.双向链表-头插法

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

    2.双向链表-尾插法

    1. public void addLast(int data){
    2. ListNode node = new ListNode(data);
    3. if(head == null){
    4. head = node;
    5. last = node;
    6. }else{
    7. last.next = node;
    8. node.prev = last;
    9. last = node;
    10. }
    11. }

    3.双向链表-任意位置插

    1. public void addIndex(int index,int data){
    2. //首先判断插入位置的合法性;
    3. if(index < 0 || index >size()){
    4. throw new ListIndexOutOfException();
    5. }
    6. //当下标为0-头插法
    7. if(index == 0){
    8. addFirst(data);
    9. return;
    10. }
    11. //当下标为size,尾插法
    12. if(index == size()){
    13. addLast(data);
    14. return;
    15. }
    16. //排除其他情况--正常插入
    17. //定义结点cur指向需要插入到的index位置
    18. ListNode cur = findIndex(index);
    19. ListNode node = new ListNode(data);
    20. cur.prev.next = node;
    21. node.prev = cur.prev;
    22. node.next = cur;
    23. cur.prev = node;
    24. }

    4.打印双向链表

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

    5.求双向链表的长度

    1. public int size(){
    2. int len = 0;
    3. ListNode cur = head;
    4. while(cur != null){
    5. len++;
    6. cur = cur.next;
    7. }
    8. return len;
    9. }

    6.找到双向链表中下标为index的元素

    1. private ListNode findIndex(int index){
    2. ListNode cur = head;
    3. while (index != 0){
    4. cur = cur.next;
    5. index--;
    6. }
    7. return cur;
    8. }

    7.查找双向链表中是否包含关键字key

    1. public boolean contains(int key){
    2. ListNode cur = head;
    3. while(cur != null){
    4. if(cur.val == key){
    5. return true;
    6. }
    7. cur = cur.next;
    8. }
    9. return false;
    10. }

    8.删除双向链表中第一个值为key的结点

    1. public void remove(int key){
    2. ListNode cur = head;
    3. while (cur != null){
    4. //判断cur的值和所要删除的值是否相同
    5. //相同
    6. if(cur.val == key){
    7. //这里需要分多种情况
    8. //1.删除的是头结点
    9. if(cur == head){
    10. head = head.next;
    11. //如果链表中有多个结点,则需要将前head.prev置空
    12. if(head != null){
    13. head.prev = null;
    14. }
    15. }else {//2.删除的不是头结点
    16. //删除的是尾巴结点
    17. if(cur.next == null){
    18. last = last.prev;
    19. cur.prev.next = null;
    20. }else {//删除的是中间结点
    21. cur.prev.next = cur.next;
    22. cur.next.prev = cur.prev;
    23. }
    24. }
    25. return;
    26. //不相同
    27. }
    28. cur = cur.next;
    29. }
    30. }

    9.删除双向链表中所有值为key的结点

    1. public void removeAllKey(int key){
    2. ListNode cur = head;
    3. while (cur != null){
    4. //判断cur的值和所要删除的值是否相同
    5. //相同
    6. if(cur.val == key){
    7. //这里需要分多种情况
    8. //1.删除的是头结点
    9. if(cur == head){
    10. head = head.next;
    11. //如果链表中有多个结点,则需要将前head.prev置空
    12. if(head != null){
    13. head.prev = null;
    14. }
    15. }else {//2.删除的不是头结点
    16. //删除的是尾巴结点
    17. if(cur.next == null){
    18. last = last.prev;
    19. cur.prev.next = null;
    20. }else {//删除的是中间结点
    21. cur.prev.next = cur.next;
    22. cur.next.prev = cur.prev;
    23. }
    24. }
    25. //不相同
    26. }
    27. cur = cur.next;
    28. }
    29. }

    10.清空双向链表

    1. public void clear(){
    2. ListNode cur = head;
    3. while (cur != null){
    4. ListNode curNext = cur.next;
    5. cur.next = null;
    6. cur.prev = null;
    7. cur = curNext;
    8. }
    9. head = null;
    10. last = null;
    11. }

  • 相关阅读:
    echart宽度100px原因(解决el-tabs里的echarts图表宽度不自适应,只有100px问题)
    SpringMvc如何向Session域中设置数据
    针对DGL的few-shot数据集划分方法
    Redis持久化
    C# 语法分析器(二)LR(0) 语法分析
    dubbo3 遇坑小结
    Spring Boot之统一处理异常
    Solr搜索参数详解
    pytorch模型量化和移植安卓详细教程
    wireshark数据结构
  • 原文地址:https://blog.csdn.net/weixin_47285608/article/details/134415731