链表的通用方法

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

试图让这两种方法发挥作用,但我对泛型没有太多经验,而且这个概念让人很困惑。
anything getfirst():返回存储在列表中第一个节点中的值。它应该打印一条错误消息,如果列表为空,则返回null。
anything getlast():返回存储在列表中最后一个节点中的值。它应该打印一条错误消息,如果列表为空,则返回null。
这是我的代码:(上面的方法显示在底部)

  1. public class Node<Anything>{
  2. private Anything data;
  3. private Node next;
  4. Node(Anything a, Node n)
  5. {
  6. data = a;
  7. next = n;
  8. }
  9. public Anything getData()
  10. {
  11. return this.data;
  12. }
  13. public Anything setData(Anything newData)
  14. {
  15. Anything oldData = this.data;
  16. this.data = newData;
  17. return oldData;
  18. }
  19. public void setNext(Node newNext)
  20. {
  21. this.next = newNext;
  22. }
  23. public Node getNext()
  24. {
  25. return this.next;
  26. }
  27. }
  28. -----------------------------------------------
  29. public class CS2LinkedList<Anything>{
  30. private Node first;
  31. private Node last;
  32. public CS2LinkedList()
  33. {
  34. first = null;
  35. }
  36. public boolean isEmpty()
  37. {
  38. return (first == null);
  39. }
  40. public void addFirst(Anything d)
  41. {
  42. Node temp = first;
  43. first = new Node(d,temp);
  44. }
  45. public void clear()
  46. {
  47. first = null;
  48. }
  49. public boolean contains(Anything value)
  50. {
  51. for (Node curr = first; curr != null; curr = curr.getNext())
  52. {
  53. if (value.equals(curr.getData())){
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. public String toString()
  60. {
  61. StringBuilder result = new StringBuilder(); //String result = "";
  62. for (Node curr = first; curr != null; curr = curr.getNext())
  63. result.append(curr.getData() + "->"); //result = result + curr.data + "->";
  64. result.append("[null]");
  65. return result.toString(); //return result + "[null]";
  66. }
  67. public int size()
  68. {
  69. int size = 0;
  70. for (Node curr = first; curr != null; curr = curr.getNext()){
  71. size++;
  72. if (first==null){
  73. size = 0;
  74. }
  75. }
  76. return size;
  77. }
  78. // ------------------------ Question begins here ------------------------
  79. public Anything getFirst()
  80. {
  81. if (first != null){
  82. // What should I return here? I tried returning first, (Anything) first, but none of them seems to work.
  83. }
  84. else{
  85. return null;
  86. }
  87. }
  88. public Anything getLast()
  89. {
  90. if (first != null){
  91. // Same here
  92. }
  93. else{
  94. return null;
  95. }
  96. }
yizd12fk

yizd12fk1#

班级 Node 有一个类型参数,但在您的类中 CS2LinkedList 您使用它时不带类型参数。你把它当作原始类型使用。原始类型只存在于java中,用于向后兼容非常旧的java版本,而这些版本没有泛型。您不应该使用原始类型(除非绝对必要,因为您必须使用非常旧的代码)。
无论你在哪里写作 Node 在你们班 CS2LinkedListNode<Anything> 相反例如,如下所示声明成员变量:

  1. private Node<Anything> first;
  2. private Node<Anything> last;

写你的 addFirst 方法如下:

  1. public void addFirst(Anything d)
  2. {
  3. Node<Anything> temp = first;
  4. first = new Node<>(d,temp);
  5. }

等等。
然后,你可以写你的 getFirst() 方法如下:

  1. public Anything getFirst()
  2. {
  3. if (first != null){
  4. return first.getData();
  5. }
  6. else{
  7. return null;
  8. }
  9. }

和其他类似的情况 getLast() 方法。
您还需要修改类中的一些代码 Node . 构造函数参数和 setNext 方法还应具有类型参数:

  1. Node(Anything a, Node<Anything> n)
  2. {
  3. data = a;
  4. next = n;
  5. }
  6. public void setNext(Node<Anything> newNext)
  7. {
  8. this.next = newNext;
  9. }

以及 getNext 方法:

  1. public Node<Anything> getNext()
  2. {
  3. return this.next;
  4. }
展开查看全部

相关问题