当前位置:   article > 正文

使用Java实现数据结构单链表_java 输入几个整数到单链表中

java 输入几个整数到单链表中

使用Java语言实现了单链表,包括基本的增删改查。

  1. /**
  2. * 链表类
  3. * 1、包含一个存储内容为空的头结点指针;
  4. * 2、索引为0-index
  5. * @author Haigp
  6. *
  7. * @param <T>
  8. */
  9. public class LinkedList<T> {
  10. public ListNode<T> head;
  11. public Integer total;
  12. // 创建单链表
  13. public LinkedList() {
  14. this.head = new ListNode<T>();
  15. this.total= 0;
  16. }
  17. // 求链表长度
  18. public int length() {
  19. // 空链直接返回
  20. if(this.head.next == null)return 0;
  21. // 遍历链
  22. int length = 0;
  23. ListNode<T> next = this.head.next;
  24. while(next!=null) {
  25. length++;
  26. next = next.next;
  27. }
  28. return length;
  29. }
  30. // 判空
  31. public boolean isEmpty() {
  32. return this.total == 0;
  33. }
  34. // 查找
  35. // 查找返回结点值为val的结点
  36. // 链表不能像数组一样进行随机访问,只能从头结点head开始,逐个的往后遍历。时间复杂度为O(n)
  37. public ListNode<T> find(T val){
  38. ListNode<T> node = this.head.next;
  39. while(node!=null) {
  40. if(node.val == val)return node;
  41. else node = node.next
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/823041
推荐阅读
相关标签
  

闽ICP备14008679号