算法(Algorithms)第4版 练习 1.3.26

本文介绍了一种从链表中移除所有指定键值元素的方法,并提供了完整的代码实现及测试用例。通过该方法,可以有效地遍历链表并移除匹配指定键的所有节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法实现:

//1.3.26
    /**
     * remove all of the nodes in the list that have key as its item field
     * 
     * @param list the linked list of T
     * @param key the T key
     * 
     * @return void
     * 
     */
    public static <T> void remove(LinkedList<T> list, T key) {
        Node<T> precurrent;
        precurrent = findPreNode(list, key);
        
        //remove all of the nodes
        while(precurrent.next != null) {
            
            if(precurrent.next == list.first)
                list.first = list.first.next;
            else
                precurrent.next = precurrent.next.next;
            
            precurrent = findPreNode(list, key);
        }
        
    }
    
    //1.3.26
    /**
     * find the node in the list whose item equals key
     * 
     * @param key the T key
     * 
     * @return return the previous node whose item equals key
     */
    private static <T> Node<T> findPreNode(LinkedList<T> list, T key) {
        Node<T> precurrent = new Node<T>();
        precurrent.next = list.first;
        
        while(precurrent.next != null && !precurrent.next.item.equals(key)) {
            precurrent = precurrent.next;
        }
        
        return precurrent;
    }

 

 

测试用例:

package com.qiusongde.linkedlist;

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class Exercise1326 {

    public static void main(String[] args) {
        
        String key = "to";
        
        LinkedList<String> list = new LinkedList<String>();
        
        while(!StdIn.isEmpty()) {
            String s = StdIn.readString();
            list.insertAtBeginning(s);
            StdOut.println("insertAtBeginning success: " + s);
            StdOut.println(list);
        }
        
        LinkedList.remove(list, key);
        StdOut.println("remove success:" + key);
        StdOut.println(list);
        
    }

}

 

 

输入数据:

to
be
or
not
to

 

 

结果1:

insertAtBeginning success: to
to 
insertAtBeginning success: be
be to 
insertAtBeginning success: or
or be to 
insertAtBeginning success: not
not or be to 
insertAtBeginning success: to
to not or be to 
remove success:to
not or be 

 

 

结果2:

insertAtBeginning success: to
to 
insertAtBeginning success: be
be to 
insertAtBeginning success: or
or be to 
insertAtBeginning success: not
not or be to 
insertAtBeginning success: to
to not or be to 
remove success:not
to or be to 

 

 

结果3:

insertAtBeginning success: to
to
insertAtBeginning success: be
be to
insertAtBeginning success: or
or be to
insertAtBeginning success: not
not or be to
insertAtBeginning success: to
to not or be to
remove success:qiu
to not or be to

 

转载于:https://www.cnblogs.com/songdechiu/p/6512182.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值