单链表pro max:<<双链表>>

前言

讲双向链表之前,首先我们要先了解双向链表的结构是怎么样的?下面我用一张图片来介绍.

可以看到下面的图片,双向链表有三个区,分别代表的是  val:数值区    prev:前一个节点的地址    next:下一个节点的地址.

了解完单链表之后,双链表写的跟玩的一样.看过了双链表方法的实现就知道了话不多说,我们进入正题.

头插法

先来一个开胃小菜,头插法.头插法就是在链表的开头插入节点,要考虑两种情况.

第一种情况:当链表为空时,新插入的节点既是头节点也是尾节点,所以head和tail都指向新插入的节点.

第二种情况:当链表不为空时,要改变头节点的prev和新插入节点的next的指向.

处理完了上面的两种情况,记得要将head的指向改一下.

    public void addFirst(int data){
        Note note = new Note(data);
        if(this.head == null){
            this.head = note;
            this.tail = note;
            return;
        }

        this.head.prev = note;
        note.next = head;
        this.head = note;
    }

尾插法

尾插也是同理,注意最后修改指向就行了.

public void addLast(int data){
        Note note = new Note(data);
        if(this.head == null){
            this.head = note;
            this.tail = note;
            return;
        }
        this.tail.next = note;
        note.prev = tail;
        this.tail = note;
    }

在任意位置插入

看整个链表,插入的位置只有两个地方.

第一个:头部和尾部

第二个:中间

有一个很容易忽视的地方,涉及插入的位置的时候,要先判断插入的位置是否合法,不能小于零并且不能隔着位置插入.

在index位置插入元素,首要任务是要找到index位置,就有了找cur的方法,到了这里插入的准备工作就做完了,假设是在1的位置插入,具体实现插入看下面的动态图片.

public boolean addIndex(int index,int data)throws IndexWrongfulException{
        if(index < 0 || index > size()){
            throw new IndexWrongfulException("位置非法,请重新输入");
        }
        if(index == 0){
            addFirst(data);
            return true;
        }
        if(index == size()){
            addLast(data);
            return true;
        }
        Note note = new Note(data);
        Note cur = findCur(data);
        note.next = cur;
        note.prev = cur.prev;
        cur.prev.next = note;
        cur.prev = note;
        return true;
    }

 private Note findCur(int index){
        Note cur = this.head;
        while(index != 0){
            cur = cur.next;
        }
        return cur;
    }

查看是否包含关键字key

这个方法实现思路就是利用while循环让cur走,找到了就返回true,没找到就返回false.

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

删除第一次出现的关键字key

注意我这里说的是第一次出现的关键字key,后面假如再出现key不用删除,例如:  1 2 3 4 4,假如要删除的关键字key是4,删除之后的结果就是1 2 3 4.就只删除了前一个4,而后面的4不用被删除.

介绍完了规则之后就是代码的具体细节了,整个删除逻辑核心代码是这两行,

cur.prev.next = cur.next;
cur.next.prev = cur.prev;

被数据结构折磨了这么久,看完这两行代码,大脑条件反射会注意到这样一个问题,这两行代码会不会出现空指针异常的情况?答案是肯定的.

当要删除的节点是头节点时,上面第一行代码会报空指针异常.因为head的prev为null,所以会报空指针异常.

当要删除的节点是尾节点时,上面第二行代码会报空指针异常,因为tail的next为null,所以会报空指针异常.

那么我们要怎样处理这两尊神仙呢?

我们先来看第一行代码可能出现空指针异常的情况.结合图片来看,可以把cur.prev = null情况单独处理,cur.prev = null肯定就是头节点,我们只要改变head节点的指向就行了,然后才能走cur.prev.next == cur.next; 这行代码.

当要删除3号节点的时候,cur.prev.next = cur.next;这行代码是可以正常走的,cur.next == null的情况就要单独考虑了,处理完空指针的情况才能走cur.next.prev = cur.prev;这行代码.

删完之后就结束这个方法.

public void remove(int key){
        Note cur = this.head;
        while(cur != null){
            if(cur.val == key){
                if(cur.prev == null){
                    this.head = this.head.next;
                    head.prev = null;
                }else{
                    cur.prev.next = cur.next;
                    if(cur.next == null){
                        this.tail = tail.prev;
                        this.tail.next = null;

                    }else {
                        cur.next.prev = cur.prev;
                    }

                }
                return;
            }
            cur = cur.next;
        }
    }

删除所有关键字为key的节点

这个方法就是删除所有要删除的节点,就不是上面删除第一次出现的关键字key了.

会了上面的方法,这个方法就很简单了,只要将return去掉就行了.有的人就不懂了,仔细想想,上面的方法是删除完了要删除的节点就结束方法,假如删除完不结束方法,就会继续删除后面要删除的节点.这样就会达到这个方法的目的.

总结

好啦!以上就是双链表的所有的内容,如果对你有帮助的话,麻烦点一个免费的赞和关注.

好的,根据您的需求,我修改后的 HTML 代码如下: ```html <div id="main"> <h1>异常记录登记</h1> <form method="post" action="/submit"> <div class="form-row"> <label>确认线别:</label> <input type="text" name="line"> </div> <div class="form-row"> <label>确认日期:</label> <input type="date" name="date"> </div> <div class="form-row"> <label>异常机种:</label> <input type="text" name="model"> </div> <div class="form-row"> <label>异常现象:</label> <textarea name="issue"></textarea> </div> <div class="form-row"> <label>生产日期:</label> <input type="date" name="prod_date"> </div> <div class="form-row"> <label>生产班别:</label> <input type="text" name="shift"> </div> <div class="form-row"> <label>生产线别:</label> <input type="text" name="prod_line"> </div> <div class="form-row"> <label>责任人:</label> <input type="text" name="responsible"> </div> <div class="form-row"> <input type="submit" value="提交"> </div> </form> </div> ``` 这里针对每个输入框都添加了一个 `div` 容器,并使用了 CSS 样式来实现左对齐和大小限制。具体的 CSS 代码如下: ```css #main { width: 80%; margin: 50px auto; padding: 20px; background-color: #f5f5f5; } .form-row { display: flex; flex-wrap: wrap; align-items: center; margin-bottom: 10px; } .form-row label { flex-basis: 30%; max-width: 150px; margin-right: 10px; } .form-row input[type="text"], .form-row input[type="date"], .form-row textarea { flex: 1; max-width: 200px; } ``` 这里使用了 `display: flex;` 属性将每个输入框的 `label` 和输入框本身放在同一行,并使用了 `flex-wrap: wrap;` 属性来实现自动换行。 使用了 `align-items: center;` 属性来使每个输入框垂直居中对齐。 使用 `flex-basis: 30%;` 和 `max-width: 150px;` 属性来限制 `label` 标签的宽度,使用 `max-width: 200px;` 属性来限制每个输入框的宽度。 希望这次修改能够满足您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值