数据结构--双向链表

一.结构图形

二.模拟实现

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

相关推荐
别摸我的婴儿肥31 分钟前
从0开始LLM-注意力机制-4
人工智能·python·算法
oh,huoyuyan32 分钟前
从效率瓶颈到自动化:火语言 RPA 在日常工作中的技术实践
java·前端·rpa
静若繁花_jingjing32 分钟前
电商项目_秒杀_初步分析
java
LZQqqqqo36 分钟前
C# 类 封装 属性 练习题
java·前端·c#
Mr.小海1 小时前
金融大模型与AI在金融业务中的应用调研报告(2025年)
人工智能·算法·机器学习·chatgpt·金融·gpt-3·文心一言
用户4472671933101 小时前
Redis + Caffeine 实现高效的两级缓存架构
java
Java水解1 小时前
美团Java面试题、笔试题(含答案)
java·后端·面试
苇柠1 小时前
Java常用API(1)
java·开发语言
雪碧聊技术1 小时前
HashMap和Hashtable的区别
java·hashmap·hashtable
意疏1 小时前
浙江大学PTA程序设计C语言基础编程练习题1-5
c语言·开发语言