数据结构--双向链表

一.结构图形

二.模拟实现

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

相关推荐
小白学大数据14 小时前
爬虫性能天花板:asyncio赋能 Aiohttp,并发提速 10 倍
开发语言·爬虫·数据分析
凡人叶枫14 小时前
Effective C++ 条款07:为多态基类声明 virtual 析构函数
linux·c语言·开发语言·c++
Black蜡笔小新14 小时前
自动化AI算法训练服务器DLTM训推一体工作站赋能多行业智能化升级
人工智能·算法·自动化
凡人叶枫14 小时前
Effective C++ 条款10:令 operator= 返回一个 reference to *this
java·linux·服务器·开发语言·c++·effective c++
摇滚侠14 小时前
JavaSE 和 JavaEE 是什么意思
java·java-ee
想带你从多云到转晴14 小时前
03、JAVAEE---多线程(三)
java
怪兽学LLM15 小时前
LeetCode 438 找到字符串中所有字母异位词(Python 固定滑动窗口+字符计数解法)
python·算法·leetcode
满怀冰雪15 小时前
第04篇-双指针算法-从有序数组到回文判断的高频解法
java·算法
CC数学建模15 小时前
2026年江西省研究生数学建模竞赛1题:空间数据分析中的过拟合识别完整思路、代码、模型、文章,全网首发高质量分享!
python·算法·数学建模
matlabgoodboy15 小时前
计算机java程序代写python代码编写c/c++代做qt设计php开发matlab
java·c语言·python