java—在双链表中的旧链表之后添加新链表

9ceoxa92  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(429)

你好,我有一个接口;

  1. public interface List<Type> {
  2. void addAll(List<Type> list);
  3. int addAll(int index, List<Type> list);
  4. }

以及你所看到的清晰的实施;

  1. public class DoublyLinkedList<Type> implements List<Type> {
  2. @Override
  3. public void addAll(List<Type> list) {
  4. Node<Type> old = first(); //returns old linked list.Also I can call the tail node with
  5. // tail variable.
  6. }
  7. @Override
  8. public int addAll(int index, List<Type> list) {
  9. // TODO Auto-generated method stub
  10. return 0;
  11. }
  12. }

有一个构造函数类;

  1. public class Node<Type> {
  2. protected Type data;
  3. protected Node<Type> next;
  4. protected Node<Type> previous;
  5. public Node(Type data, Node<Type> next,Node<Type> previous) {
  6. this.data = data;
  7. this.next = next;
  8. this.previous = previous;
  9. }
  10. public Type getData() {
  11. return data;
  12. }
  13. public Node<Type> getNext() {
  14. return next;
  15. }
  16. public Node<Type> getPrevious() {
  17. return previous;
  18. }
  19. }

我没有把我所有的方法都放在上面。因为我的项目,所以我的问题是如何实现这些方法?我想在旧的链表之后通过接口添加一个新的链表。

flmtquvp

flmtquvp1#

可能有不同的方法来实现您的需求。下面是我的实现。注意我改了名字。我给接口命名了 Listing 以免与…发生冲突 java.util.List . 类型参数的约定也是一个大写字母,所以我改了 TypeT . 我换了班 NodeListNode 因为已经有很多 Node 班级。我还添加了一个 toString() 方法到类 ListNode 还有上课 DoublyLinkedList 作为测试辅助。我还添加了方法 main() 上课 DoublyLinkedList 使测试实现成为可能。最后,我添加了方法 add() 上课 DoublyLinkedList 使创建非空列表成为可能。
解释下面的代码是如何工作的需要大量的文本,可能还需要一些图表。与其这样做,我建议您只需在调试器中运行代码。大多数ide都有一个调试器。
接口 Listing ```
public interface Listing {

  1. /**
  2. * Append 'list' to the end of this 'Listing'.
  3. *
  4. * @param list list to append.
  5. */
  6. void addAll(Listing<T> list);
  7. /**
  8. * Insert 'list' after element at 'index'. If 'index' greater than size of this
  9. * 'Listing', append 'list' to this 'Listing'.
  10. *
  11. * @param index insertion index
  12. * @param list list to insert
  13. *
  14. * @return The index after which 'list' was inserted.
  15. */
  16. int addAll(int index, Listing<T> list);

}

  1. 班级 `ListNode` ```
  2. public class ListNode<T> {
  3. protected T data;
  4. protected ListNode<T> next;
  5. protected ListNode<T> previous;
  6. public ListNode(T data) {
  7. this.data = data;
  8. }
  9. public T getData() {
  10. return data;
  11. }
  12. public ListNode<T> getNext() {
  13. return next;
  14. }
  15. public ListNode<T> getPrevious() {
  16. return previous;
  17. }
  18. public String toString() {
  19. return String.format("<-%s->", String.valueOf(data));
  20. }
  21. }

班级 DoublyLinkedList ```
public class DoublyLinkedList implements Listing {
private ListNode head;

  1. public void add(T data) {
  2. ListNode<T> newNode = new ListNode<T>(data);
  3. if (head == null) {
  4. head = newNode;
  5. }
  6. else {
  7. ListNode<T> current = head;
  8. while (current.next != null) {
  9. current = current.next;
  10. }
  11. current.next = newNode;
  12. newNode.previous = current;
  13. }
  14. }
  15. @Override
  16. public void addAll(Listing<T> list) {
  17. if (list instanceof DoublyLinkedList) {
  18. DoublyLinkedList<T> lst = (DoublyLinkedList<T>) list;
  19. if (lst.head != null) {
  20. if (head == null) {
  21. head = lst.head;
  22. }
  23. else {
  24. ListNode<T> current = head;
  25. while (current.next != null) {
  26. current = current.next;
  27. }
  28. current.next = lst.head;
  29. lst.head.previous = current;
  30. }
  31. }
  32. }
  33. }
  34. @Override
  35. public int addAll(int index, Listing<T> list) {
  36. if (index < 0) {
  37. throw new IllegalArgumentException("Negative index.");
  38. }
  39. int counter = 0;
  40. if (list instanceof DoublyLinkedList) {
  41. DoublyLinkedList<T> lst = (DoublyLinkedList<T>) list;
  42. if (lst.head != null) {
  43. if (head == null) {
  44. if (index == 0) {
  45. head = lst.head;
  46. }
  47. }
  48. else {
  49. ListNode<T> current = head;
  50. while (current.next != null && counter < index) {
  51. counter++;
  52. current = current.next;
  53. }
  54. if (counter < index) {
  55. current.next = lst.head;
  56. lst.head.previous = current;
  57. }
  58. else {
  59. current.previous.next = lst.head;
  60. ListNode<T> tmp = current;
  61. ListNode<T> curr = lst.head;
  62. while (curr.next != null) {
  63. curr = curr.next;
  64. }
  65. curr.next = tmp;
  66. curr.previous = tmp.previous;
  67. tmp.previous = curr;
  68. }
  69. }
  70. }
  71. }
  72. return counter;
  73. }
  74. public String toString() {
  75. StringBuilder sb = new StringBuilder();
  76. if (head != null) {
  77. ListNode<T> current = head;
  78. while (current != null) {
  79. sb.append(current);
  80. current = current.next;
  81. }
  82. }
  83. return sb.toString();
  84. }
  85. public static void main(String[] args) {
  86. DoublyLinkedList<String> list1 = new DoublyLinkedList<>();
  87. list1.add("One");
  88. DoublyLinkedList<String> list2 = new DoublyLinkedList<>();
  89. list2.add("First");
  90. list1.addAll(list2);
  91. System.out.println(list1);
  92. DoublyLinkedList<String> list3 = new DoublyLinkedList<>();
  93. list3.add("TOO");
  94. int result = list1.addAll(1, list3);
  95. System.out.printf("[%d] %s%n", result, list1);
  96. }

}

展开查看全部

相关问题