数据结构--双向链表

一.结构图形

二.模拟实现

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与顺序表不一样,这里是构造一个新的链表。

相关推荐
方安乐17 小时前
python之向量、向量和、向量点积
开发语言·python·numpy
代码AI弗森18 小时前
一文理清楚“算力申请 / 成本测算 / 并发评估”
java·服务器·数据库
Old Uncle Tom18 小时前
OpenClaw 记忆系统 -- 记忆预加载
java·数据结构·算法·agent
会编程的土豆19 小时前
洛谷题单入门1 顺序结构
数据结构·算法·golang
小小小米粒19 小时前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
生信碱移19 小时前
PACells:这个方法可以鉴定疾病/预后相关的重要细胞亚群,作者提供的代码流程可以学习起来了,甚至兼容转录组与 ATAC 两种数据类型!
人工智能·学习·算法·机器学习·数据挖掘·数据分析·r语言
智者知已应修善业19 小时前
【51单片机中的打飞机设计】2023-8-25
c++·经验分享·笔记·算法·51单片机
摇滚侠19 小时前
expdp 查看帮助
java·数据库·oracle
czhc114007566319 小时前
C# 428 线程、异步
开发语言·c#
:12120 小时前
java基础
java·开发语言