算法(Algorithms)第4版 练习 1.3.20

方法实现:

复制代码
//1.3.20
    /**
     * delete the kth element in a linked list, if it exists. 
     * 
     * @param k the kth element, it should larger than 1
     * @throws IllegalArgumentException if k < 1
     * @throws NoSuchElementException if the size of the list is less than k
     */
    public Item delete(int k) {
        
        if(k < 1)
            throw new IllegalArgumentException("k must larger than 1");
        
        Node<Item> precurrent = new Node<Item>();
        precurrent.next = first;
        Item item;
        
        while(precurrent.next != null && k > 1) {
            precurrent = precurrent.next;
            k--;
        }
        
        if(precurrent.next == null) 
            throw new NoSuchElementException("LinkedList hasn't so many elements");
        
        item = precurrent.next.item;
        if(precurrent.next == first)
            first = precurrent.next.next;
        else
            precurrent.next = precurrent.next.next;
        
        return item;
    }
复制代码

 

 

测试用例:

复制代码
package com.qiusongde.linkedlist;

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

public class Exercise1320 {

    public static void main(String[] args) {
        
        LinkedList<String> list = new LinkedList<String>();
        
        while(!StdIn.isEmpty()) {
            String s = StdIn.readString();
            list.insertAtBeginning(s);
            StdOut.println("insertAtBeginning success: " + s);
            StdOut.println(list);
        }
        
        int k = 5;
        String s = list.delete(k);
        StdOut.println("delete " + k + "th Element success: "+ s);
        StdOut.println(list);
        
        for(int i = 0; i < 4; i++) {
            k = 1;
            s = list.delete(k);
            StdOut.println("delete " + k + "th Element success: "+ s);
            StdOut.println(list);
        }
        
        k = 5;
        s = list.delete(k);
        StdOut.println("delete " + k + "th Element success: "+ s);
        StdOut.println(list);
    }

}
复制代码

 

 

测试数据:

to
be
or
not
to

 

 

输出结果:

复制代码
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 
delete 5th Element success: to
to not or be 
delete 1th Element success: to
not or be 
delete 1th Element success: not
or be 
delete 1th Element success: or
be 
delete 1th Element success: be

Exception in thread "main" java.util.NoSuchElementException: LinkedList hasn't so many elements
    at com.qiusongde.linkedlist.LinkedList.delete(LinkedList.java:130)
    at com.qiusongde.linkedlist.Exercise1320.main(Exercise1320.java:32)
复制代码

 

posted @   我是老邱  阅读(261)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示