方法实现:

//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)

最新文章

  1. Asp.Net Core 项目从 1.0.1 升级到 1.1.0 的小补丁
  2. struts下ajax提交与页面进行提示 返回值为null
  3. Android缓存学习入门
  4. PLSQL_基础系列11_递归和层次查询CONNECT BY(案例)
  5. [转] 学习React Native必看的几个开源项目
  6. Web文件管理:elFinder.Net(支持FTP)
  7. javascript版1024游戏源码
  8. linux下so动态库一些不为人知的秘密(上)
  9. OpenCV-Python教程(9、使用霍夫变换检测直线)
  10. UML简单梳理类图
  11. idea 端口占用
  12. ios字体大小适应不同屏幕
  13. python中从文件中读取数据2
  14. 团队作业10——事后分析(Beta版本)
  15. nodejs http小爬虫
  16. LeetCode - 185. Department Top Three Salaries
  17. bzoj1499[NOI2005]瑰丽华尔兹 单调队列优化dp
  18. docker log driver
  19. MySQL学习(二) 数据类型
  20. Python 练习:使用 * 输出直角三角形

热门文章

  1. dubbo zookeeper案例
  2. eclipse svn插件 删除原账号信息重新登录
  3. leetCode 61.Rotate List (旋转链表) 解题思路和方法
  4. hadoop生态系统学习之路(六)hive的简单使用
  5. 开发ActiveX控件调用另一个ActiveX系列0&mdash;&mdash;身份证识别仪驱动的问题
  6. Android Handler 异步消息处理机制的妙用 创建强大的图片载入类
  7. C++成员不通过对象调用的直接调用写法
  8. 线性表的链式实现(C++)
  9. Chrome自带恐龙小游戏的源码研究(完)
  10. 浅谈java反序列化工具ysoserial