数据结构-双向链表

  • 定义

    • 双向链表是一种特殊的单链表,它每个节点都有一个指向前一个节点的引用(称为 prev)和一个指向后一个节点的引用(称为 next)。这使得双向链表中的元素可以双向遍历。
  • 底层实现

    • 节点

      public class Node {
          int data;
          Node next;
          Node prev;
          public Node(int data) {
              this.data = data;
          }
      }
      
    • 链表实现

      public class DoubleLinkedList {
          //表头节点指针
          private Node head;
          //表尾节点指针
          private Node tail;
          //1.增加节点
          public void append(int data){
              Node newNode = new Node(data);
              //如果链表为空
              if (head==null){
                  head=newNode;
                  tail=newNode;
              //如果链表不为空
              }else {
                  tail.next=newNode;
                  newNode.prev=tail;
                  tail=newNode;
              }
          }
          //2.插入节点
          public void insert(int data,int position){
              Node newNode = new Node(data);
              //如果在表头插入
              if (position<=0){
                  newNode.next=head;
                  head.prev=newNode;
                  head=newNode;
              //如果在表尾插入
              }else if (position>=size()){
                  tail.next=newNode;
                  newNode.prev=tail;
                  tail=newNode;
              //如果在中间插入
              }else {
                  Node current=head;
                  for (int i = 0; i < position-1; i++) {
                      current=current.next;
                  }
                  newNode.next=current.next;
                  newNode.prev=current;
                  current.next.prev=newNode;
                  current.next=newNode;
              }
          }
          //3.删除节点
          public void delete(int data){
              Node current=head;
              //链表不为空
              while (current!=null){
                  //当前节点不是要找的节点则向后移动
                  if (current.data!=data){
                      current=current.next;
                  //当前节点是要找的节点
                  }else {
                      //如果当前节点不是头节点
                      if (current.prev!=null){
                          current.prev.next=current.next;
                      //如果当前节点是头节点
                      }else {
                          head=current.next;
                      }
                      //如果当前节点不是尾节点
                      if (current.next!=null){
                          current.next.prev=current.prev;
                      //如果当前节点是尾节点
                      }else {
                          tail=current.prev;
                      }
                  }
              }
          }
          //4.获取节点个数
          public int size(){
              int count=0;
              Node current=head;
              while (current!=null){
                  count++;
                  current=current.next;
              }
              return count;
          }
          //5.打印链表
          public void print(){
              Node current=head;
              while (current!=null){
                  System.out.print(current.data+" ");
                  current=current.next;
              }
              System.out.println();
          }
      }
      
  • LinkedList实现双向链表

    public class DoubleLinkedJavaDemo {
        //使用final保证线程安全
        private final LinkedList<Integer> list;
    
        public DoubleLinkedJavaDemo() {
            this.list = new LinkedList<>();
        }
        //1.添加元素
        public void append(int data){
            list.addLast(data);
        }
        //2.插入元素
        public void insert(int index,int data){
            if (index < 0 || index > list.size()){
                throw new IndexOutOfBoundsException();
            }
            list.add(index,data);
        }
        //3.删除元素
        public void delete(int data){
            //删除所有值为data的数据
            list.removeIf(item->item==data);
        }
        //4.获取大小
        public int size(){
            return list.size();
        }
        //5.打印链表
        public void print(){
            for (int item:list){
                System.out.println(item+" ");
            }
            System.out.println();
        }
    }
    
  • 循环链表

    • 在单循环链表中尾节点的next指针指向第一个带数据的节点
    • 在双循环链表中,每个节点都有一个prev域和next域,它们分别指向链表中前一个节点和下一个节点。在只有一个节点的双循环链表中,该节点的prev域指向它自己,next域也指向它自己,除此之外,尾节点的next域指向头节点。头节点的prev域指向尾节点。
相关推荐
夏末秋也凉2 小时前
力扣-回溯-46 全排列
数据结构·算法·leetcode
王老师青少年编程2 小时前
【GESP C++八级考试考点详细解读】
数据结构·c++·算法·gesp·csp·信奥赛
liuyuzhongcc5 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
计算机小白一个6 小时前
蓝桥杯 Java B 组之背包问题、最长递增子序列(LIS)
java·数据结构·蓝桥杯
卑微的小鬼7 小时前
数据库使用B+树的原因
数据结构·b树
cookies_s_s7 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法
醉城夜风~8 小时前
[数据结构]双链表详解
数据结构
gyeolhada9 小时前
2025蓝桥杯JAVA编程题练习Day5
java·数据结构·算法·蓝桥杯
阿巴~阿巴~9 小时前
多源 BFS 算法详解:从原理到实现,高效解决多源最短路问题
开发语言·数据结构·c++·算法·宽度优先
刃神太酷啦11 小时前
堆和priority_queue
数据结构·c++·蓝桥杯c++组