数据结构--双向链表

发布于:2025-07-23 ⋅ 阅读:(15) ⋅ 点赞:(0)

一.结构图形

二.模拟实现

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下方法的使用

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


网站公告

今日签到

点亮在社区的每一天
去签到