数据结构--双向链表

一.结构图形

二.模拟实现

java 复制代码
public class Mylink {
    class ListNode{
        public ListNode next;
        public ListNode pre;
        public int value;

        public ListNode(int value) {
            this.value = value;
        }
    }
    public ListNode head;
    public ListNode last;
    //得到单链表的长度
    public int size(){
        int count =0;
        ListNode cur =head;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }
    public void display(){
        ListNode cur =head;
        while (cur!=null){
            System.out.print(cur.value+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur =head;
        while (cur!=null){
            if (cur.value==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        ListNode newN=new ListNode(data);
        ListNode cur =head;
        if (head==null){
            head=last=newN;
        }else {
            cur.pre=newN;
            newN.next=cur;
            head=newN;
        }
    }
    //尾插法
    public void addLast(int data){
     ListNode newN =new ListNode(data);
     if (head==null){
         head=last=newN;
     }else {
         newN.pre=last;
         last.next=newN;
         last=newN;

     }
    }
    public void checkPos (int pos)throws PostException{
        if(pos<0||pos>size()){
            throw new PostException("下标异常....;");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex (int index,int data){
        try {
            checkPos(index);
            if(index==0){
                addFirst(data);
                return;
            }
            if (index==size()){
                addLast(data);
                return;
            }
            ListNode newN =new ListNode(data);
            ListNode cur =head;
            while (index>0){
                cur=cur.next;
                index--;
            }
            newN.next=cur;
            newN.pre=cur.pre;
            cur.pre.next=newN;
            cur.pre=newN;
        }catch (PostException e){
            e.printStackTrace();
        }
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head==null){
            return;
        }
        ListNode cur =head;
        while (cur!=null){
            if (cur.value==key){
                if (cur.pre==null&&cur.next==null){
                   head=last=null;
                   return;
                }
                if (cur.pre==null){
                    cur.next.pre=null;
                    head=cur.next;
                    return;
                }
                if (cur.next==null){
                    cur.pre.next=null;
                    last=cur.pre;
                    return;
                }
                cur.next.pre=cur.pre;
                cur.pre.next=cur.next;
                return;
            }
            cur=cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if (head==null){
            return;
        }
        ListNode cur =head;
        while (cur!=null){
            if (cur.value==key){
                if (cur.pre==null&&cur.next==null){
                    head=last=null;
                } else if (cur.pre==null){
                    head=cur.next;
                    head.pre=null;
                } else if (cur.next==null){
                    cur.pre.next=null;
                    last=cur.pre;
                }else {
                    cur.next.pre=cur.pre;
                    cur.pre.next=cur.next;
                }
            }
            cur=cur.next;
        }
    }

    public void clear(){
        head=last=null;
    }

}

三.Linklist下方法的使用

java 复制代码
 public static void main(String[] args) {
 LinkedList<Integer> list = new LinkedList<>();
 list.add(1);   // add(elem): 表示尾插
 list.add(2);
 list.add(3);
 list.add(4);
 list.add(5);
 list.add(6);
 list.add(7);
 System.out.println(list.size()); //打印长度
 System.out.println(list);
 // 在起始位置插入0
 list.add(0, 0);  // add(index, elem): 在index位置插入元素elem
 System.out.println(list);
 list.remove();         
 // remove(): 删除第一个元素,内部调用的是removeFirst()
 list.removeFirst();    // removeFirst(): 删除第一个元素
 list.removeLast();    // removeLast():  删除最后元素
 list.remove(1);  // remove(index): 删除index位置的元素
 System.out.println(list);
 // contains(elem): 检测elem元素是否存在,如果存在返回true,否则返回false
 if(!list.contains(1)){
 list.add(0, 1);
}
  list.add(1);
    System.out.println(list);
    System.out.println(list.indexOf(1));   // indexOf(elem): 从前往后找到第一个elem的位置
    System.out.println(list.lastIndexOf(1));  // lastIndexOf(elem): 从后往前找第一个1的位置
    int elem = list.get(0);    // get(index): 获取指定位置元素
    list.set(0, 100);          // set(index, elem): 将index位置的元素设置为elem
    System.out.println(list);
    
    // subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回
    List<Integer> copy = list.subList(0, 3);     //返回类型为List
    System.out.println(list);
    System.out.println(copy);
    list.clear();              // 将list中元素清空
    System.out.println(list.size());
 }

注意:

这里的sublist与顺序表不一样,这里是构造一个新的链表。

相关推荐
二哈赛车手4 小时前
新人笔记---ApiFox的一些常见使用出错
java·笔记·spring
为何创造硅基生物4 小时前
C语言 结构体内存对齐规则(通俗易懂版)
c语言·开发语言
吃好睡好便好4 小时前
在Matlab中绘制横直方图
开发语言·学习·算法·matlab
栗子~~4 小时前
JAVA - 二层缓存设计(本地缓冲+redis缓冲+广播所有本地缓冲失效) demo
java·redis·缓存
星寂樱易李4 小时前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
YDS8295 小时前
DeepSeek RAG&MCP + Agent智能体项目 —— RAG知识库的搭建和接口实现
java·ai·springboot·agent·rag·deepseek
仰泳之鹅5 小时前
【C语言】自定义数据类型2——联合体与枚举
c语言·开发语言·算法
之歆5 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
未若君雅裁6 小时前
MyBatis 一级缓存、二级缓存与清理机制
java·缓存·mybatis
cen__y6 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git