带链表的泛型方法

jmo0nnb3  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(401)

嗨,我正在努力使这两种方法发挥作用,但即使我尝试了几乎所有我能想到的方法,它仍然不起作用。请告诉我如何修理它!
void add(anything value):将包含newvalue的节点添加到列表的末尾。
void addafter(int index,anything value):在索引的节点之后添加一个包含newvalue的节点(假设索引从0开始)。
这是我的代码:(上面的方法显示在底部)

  1. public class Node<Anything>
  2. {
  3. private Anything data;
  4. private Node next;
  5. Node(Anything a, Node<Anything> n)
  6. {
  7. data = a;
  8. next = n;
  9. }
  10. public Anything getData()
  11. {
  12. return this.data;
  13. }
  14. public Anything setData(Anything newData)
  15. {
  16. Anything oldData = this.data;
  17. this.data = newData;
  18. return oldData;
  19. }
  20. public void setNext(Node<Anything> newNext)
  21. {
  22. this.next = newNext;
  23. }
  24. public Node<Anything> getNext()
  25. {
  26. return this.next;
  27. }
  28. }
  29. ------------------------------------------
  30. public class CS2LinkedList<Anything>
  31. {
  32. private Node<Anything> first;
  33. private Node<Anything> last;
  34. public CS2LinkedList()
  35. {
  36. first = null;
  37. }
  38. public boolean isEmpty()
  39. {
  40. return (first == null);
  41. }
  42. public void addFirst(Anything d)
  43. {
  44. Node<Anything> temp = first;
  45. first = new Node<>(d,temp);
  46. }
  47. public void clear()
  48. {
  49. first = null;
  50. }
  51. public boolean contains(Anything value)
  52. {
  53. for (Node curr = first; curr != null; curr = curr.getNext())
  54. {
  55. if (value.equals(curr.getData())){
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. public String toString()
  62. {
  63. StringBuilder result = new StringBuilder(); //String result = "";
  64. for (Node curr = first; curr != null; curr = curr.getNext())
  65. result.append(curr.getData() + "->"); //result = result + curr.data + "->";
  66. result.append("[null]");
  67. return result.toString(); //return result + "[null]";
  68. }
  69. public int size()
  70. {
  71. int size = 0;
  72. for (Node curr = first; curr != null; curr = curr.getNext()){
  73. size++;
  74. if (first==null){
  75. size = 0;
  76. }
  77. }
  78. return size;
  79. }
  80. public Anything getFirst()
  81. {
  82. if (first!=null){
  83. return first.getData();
  84. }
  85. else{
  86. System.out.println("Sorry, the list is empty.");
  87. return null;
  88. }
  89. }
  90. public Anything getLast()
  91. {
  92. if (first!= null){
  93. for(Node curr = first; curr != null; curr = curr.getNext()){
  94. first = curr;
  95. }
  96. return first.getData();
  97. //FIX: list2's size decreases by 1 after executing list2.getLast()
  98. }
  99. else{
  100. System.out.println("Sorry, the list is empty.");
  101. return null;
  102. }
  103. }
  104. public void add(Anything value){
  105. if (first==null){
  106. first = new Node<>(value,first);
  107. }
  108. Node<Anything> next = new Node<>(value, first);
  109. first.setNext(null);
  110. last = next;
  111. }
  112. public void addAfter(int index, Anything value)
  113. {
  114. return;
  115. }
  116. }
gt0wga4j

gt0wga4j1#

回答有点长,试图解释每一个问题并给出解决方案。耐心点!
您所看到的代码正是按照您告诉它的方式进行的,以相反的方式添加新节点。它正在添加 new node 作为 first 设定 current first 作为新节点的 next 虽然 addFirst() 被称为。
但在当前情况下 add() 方法,您将始终得到一个不超过 one 通过调用 printing toString() 方法,尽管列表中包含的节点不超过两个。让我们看看您的代码以了解这一点:

  1. public void add(Anything value){
  2. if (first==null){
  3. first = new Node<>(value,first);
  4. }
  5. Node<Anything> next = new Node<>(value, first);
  6. first.setNext(null);
  7. last = next;
  8. }

所以你要做的是,你要检查 first 为空,表示当前列表是否为空。如果 true 然后初始化 first 具有新的价值(例如 "x" )下一个是 current first ,即 null . 所以我们得到了 [first(x)] -> [null] 作为我们目前的名单。
接下来,您将初始化一个 nextvaluefirst 因为它是下一个。然后,您将设置下一个 first 作为 null 分配 nextlast . 到目前为止,我们已经:

  1. [next(x)] -> [first(x)] -> [null] //where last node is next(x)

现在如果你想 add(y) 另一个 next 将使用当前 first 作为此新节点的下一个节点。然后再次重复制造 nullnextfirst . 然后再次将此新节点分配给 last . 现在我们有:

  1. [next(y) -> [first(x)] -> [null]

好了,我们开始吧!你正在失去信心 current last 通过将新节点指定给 last ,还有这个 last 将有 first 下一步,总是要添加多少节点,它遵循相同的路径。
因此,您将得到一个 two 元素,总是这样 add() 方法。
但是为什么tostring()只提供 [first(x)] -> [null] 作为链接列表?
好吧,看看这个 toString() 功能,它从 first ,直至 first.next == null . 而且,你有 first.next == null ,始终,如果使用此选项添加节点 add() 方法。因此,政府的这种行为 toString() .
解决办法是什么?
设置两个 firstlast 初始化期间,构造函数中的链接列表的值为null。在 add() 方法,初始化 first 第一次与 valuelast . 并将其分配给 last . return 之后,我们将添加第一个节点。移除 first.setNext(null) 语句,初始化 new node 有价值 null . 使 nextcurrent last 作为 new node . 并将新节点指定为最后一个节点。代码如下:

  1. public void add(Anything value) {
  2. if (first == null) {
  3. first = new Node<>(value, last);
  4. last = first;
  5. return;
  6. }
  7. Node<Anything> next = new Node<>(value, null);
  8. last.setNext(next);
  9. last = next;
  10. }

现在如果你打印 toString() ,您将获得:

  1. node1->node2->node3->[null]

希望你得到你的答案和想要的解决方案!

展开查看全部

相关问题