数据结构之双链表

1.复杂方法的图分析

在这里插入图片描述
在这里插入图片描述

2.My_LinkedList代码

package My_liNKEDlIST;

public class My_LinkedList implements MY_lIST{
    static class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val){
           this.val = val;
        }

    }
    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
         if(head == null){
             head = node;
             last = node;
         }
         else{
             node.next = head;
             head.prev = node;
             head = node;
         }
    }

    @Override
    public void addLast(int data) {
       ListNode node = new ListNode(data);
       if(head == null){
           head = node;
           last = node;
       }
       else{
           last.next = node;
           node.prev = last;
           last = node;
       }
    }

    @Override
    public void addIndex(int index, int data) throws IndexOutOFException{
          int len = size();
          if(index < 0 || index > len){
              throw new IndexOutOFException("越界了");
          }
          if(index == 0){
              addFirst(data);
              return;
          }
          if(index == len){
              addLast(data);
              return;
          }
          ListNode cur = head;
          ListNode node = new ListNode(data);
          while(index != 0){
              cur = cur.next;
              index--;
          }
           cur.prev.next = node;
           node.next = cur;
           node.prev = cur.prev;
           cur.prev = node;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
        }
            cur = cur.next;
    }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //如果删除的结点为头结点
                if(cur == head){
                   head = head.next;
                   if(head != null) {
                       head.prev = null;
                   }
                }else {
                     if(cur == last){
                         cur.prev.next = cur.next;
                         last = last.prev;
                     }else {
                         cur.prev.next = cur.next;
                         cur.next.prev = cur.prev;
                     }
                }
                return;
            }
            else{
                cur = cur.next;
            }
        }
    }
    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                //如果删除的结点为头结点
                if(cur == head){
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }
                }else {
                    if(cur.next == null) {
                        cur.prev.next = cur.next;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
            }
                cur = cur.next;
        }
    }
    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
    }

    @Override
    public void clear() {
       ListNode cur = head;
       while(cur != null){
           ListNode tmp = cur.next;
           cur.prev = null;
           cur.next = null;
           cur = tmp;
       }
       this.head = null;
       this.last = null;
    }
}

3.接口MY_lIST

public interface MY_lIST {
        // 2、无头双向链表实现

        //头插法
        public void addFirst(int data);
        //尾插法
        public void addLast(int data);
        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data);
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key);
        //删除第一次出现关键字为key的节点
        public void remove(int key);
        //删除所有值为key的节点
        public void removeAllKey(int key);
        //得到单链表的长度
        public int size();
        public void display();
        public void clear();
    }

4.测试类

public class Test {
    public static void main(String[] args) {
             My_LinkedList LIST = new My_LinkedList();
             LIST.addFirst(1);
             LIST.addFirst(1);
             LIST.addFirst(1);
             //LIST.display();
             LIST.addLast(4);
             LIST.addLast(5);
             LIST.addLast(6);
             //LIST.display();
              LIST.addIndex(4,7);
              //LIST.display();
              //LIST.clear();
              //LIST.display();
           //LIST.remove(4);
        LIST.removeAllKey(1);
           LIST.display();
    }
}

双链表的思路和单链表大同小异,就是多加了一个前驱指针,希望大家好好学习双链表的代码

  • 18
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值