为什么我无法将元素插入到我的链表实现中?

huangapple go评论109阅读模式
英文:

Why am I not able to insert elements into my linked-list implementation?

问题

我正在使用Java实现链表。特别是在进行插入操作时,如果给定的索引超过链表的长度,我必须将值追加到链表中。尽管我已经为此编写了代码,但情况并不是这样发生。

为了调试这个问题,我添加了打印链表的代码行。我可以在插入函数中的条件if(index >= this.length())之前打印列表,但不能在条件内部的代码行中打印链表。

  1. package com.learning.algorithms.LinkedList;
  2. public class Node {
  3. int data;
  4. Node next;
  5. public Node() {
  6. }
  7. public Node(int i) {
  8. this.data = i;
  9. this.next = null;
  10. }
  11. }
  1. package com.learning.algorithms.LinkedList;
  2. public class LinkedList {
  3. Node head;
  4. Node tail;
  5. public LinkedList() {
  6. this.head = new Node();
  7. this.tail = this.head;
  8. }
  9. public LinkedList(int value) {
  10. this.head = new Node();
  11. this.head.data = value;
  12. this.head.next = null;
  13. this.tail = this.head;
  14. }
  15. public LinkedList append(int value) {
  16. Node newNode = new Node(value);
  17. this.tail.next = newNode;
  18. this.tail = newNode;
  19. return this;
  20. }
  21. public void printList() {
  22. Node useNode = this.head;
  23. while (useNode != null) {
  24. System.out.println(useNode.data);
  25. useNode = useNode.next;
  26. }
  27. //print(useNode);
  28. }
  29. private void print(Node useNode) {
  30. if (useNode != null) {
  31. System.out.println(useNode.data);
  32. print(useNode.next);
  33. }
  34. }
  35. public LinkedList prepend(int i) {
  36. Node newNode = new Node(i);
  37. newNode.next = this.head;
  38. this.head = newNode;
  39. return this;
  40. }
  41. public LinkedList insert(int index, int value) {
  42. if (index == 0) {
  43. this.prepend(value);
  44. return this;
  45. }
  46. this.printList();
  47. if (index >= this.length()) {
  48. System.out.println("inside");
  49. this.printList();
  50. this.append(value);
  51. return this;
  52. }
  53. Node nodeJustBeforeGivenIndex = this.head;
  54. // getting node just before given index using while loop
  55. while (index > 1) {
  56. nodeJustBeforeGivenIndex = nodeJustBeforeGivenIndex.next;
  57. index--;
  58. }
  59. // make an insert
  60. Node newNode = new Node(value);
  61. newNode.next = nodeJustBeforeGivenIndex.next;
  62. nodeJustBeforeGivenIndex.next = newNode;
  63. return this;
  64. }
  65. private int length() {
  66. int counnt = 0;
  67. if (this.head != null) {
  68. while (this.head != null) {
  69. this.head = this.head.next;
  70. counnt++;
  71. }
  72. }
  73. return counnt;
  74. }
  75. }
  1. package com.learning.algorithms.LinkedList;
  2. public class LinkedListImplementation {
  3. public static void main(String[] args) {
  4. LinkedList list = new LinkedList(10);
  5. list.append(5);
  6. list.append(16);
  7. list.prepend(1);
  8. list.insert(0, 15);
  9. list.insert(10, 222);
  10. list.printList();
  11. }
  12. }

运行此实现类的控制台输出:

  1. 15
  2. 1
  3. 10
  4. 5
  5. 16
  6. inside
英文:

I am doing linkedlist implementation in java. Particularly, while doing insert operation, if the given index is more than the length of the linkedlist, I have to append the value to the linkedlist. Though I have written code for the same, it is not happening that way.

In order to debug this, I added lines for printing linkedlist. I could print the list just before the condition if(index >= this.length()) in the insert function but not able to print the linkedlist in the line inside the condition.

  1. package com.learning.algorithms.LinkedList;
  2. public class Node {
  3. int data;
  4. Node next;
  5. public Node() {
  6. }
  7. public Node(int i) {
  8. this.data = i;
  9. this.next = null;
  10. }
  11. }
  1. package com.learning.algorithms.LinkedList;
  2. public class LinkedList {
  3. Node head;
  4. Node tail;
  5. public LinkedList() {
  6. this.head = new Node();
  7. this.tail = this.head;
  8. }
  9. public LinkedList(int value) {
  10. this.head = new Node();
  11. this.head.data = value;
  12. this.head.next = null;
  13. this.tail = this.head;
  14. }
  15. public LinkedList append(int value) {
  16. Node newNode = new Node(value);
  17. this.tail.next = newNode;
  18. this.tail = newNode;
  19. return this;
  20. }
  21. public void printList() {
  22. Node useNode = this.head;
  23. while(useNode!=null)
  24. {
  25. System.out.println(useNode.data);
  26. useNode = useNode.next;
  27. }
  28. //print(useNode);
  29. }
  30. private void print(Node useNode) {
  31. if(useNode!=null)
  32. {
  33. System.out.println(useNode.data);
  34. print(useNode.next);
  35. }
  36. }
  37. public LinkedList prepend(int i) {
  38. Node newNode = new Node(i);
  39. newNode.next = this.head;
  40. this.head = newNode;
  41. return this;
  42. }
  43. public LinkedList insert(int index, int value) {
  44. if(index == 0)
  45. {
  46. this.prepend(value);
  47. return this;
  48. }
  49. this.printList();
  50. if(index >= this.length())
  51. {
  52. System.out.println("inside");
  53. this.printList();
  54. this.append(value);
  55. return this;
  56. }
  57. Node nodeJustBeforeGivenIndex = this.head;
  58. // getting node just before given index using while loop
  59. while(index>1)
  60. {
  61. nodeJustBeforeGivenIndex = nodeJustBeforeGivenIndex.next;
  62. index--;
  63. }
  64. // make an insert
  65. Node newNode = new Node(value);
  66. newNode.next = nodeJustBeforeGivenIndex.next;
  67. nodeJustBeforeGivenIndex.next = newNode;
  68. return this;
  69. }
  70. private int length() {
  71. int counnt = 0;
  72. if(this.head !=null ) {
  73. while(this.head !=null )
  74. {
  75. this.head = this.head.next;
  76. counnt++;
  77. }
  78. }
  79. return counnt;
  80. }
  81. }
  1. package com.learning.algorithms.LinkedList;
  2. public class LinkedListImplementation {
  3. public static void main(String[] args)
  4. {
  5. LinkedList list = new LinkedList(10);
  6. list.append(5);
  7. list.append(16);
  8. list.prepend(1);
  9. list.insert(0, 15);
  10. list.insert(10, 222);
  11. list.printList();
  12. }
  13. }

Console output for running this implementation class:

  1. 15
  2. 1
  3. 10
  4. 5
  5. 16
  6. inside

答案1

得分: 1

你不应该在 length 方法内部修改头部值。

这样会修复它:

  1. private int length() {
  2. int count = 0;
  3. Node iter = this.head;
  4. while(iter != null) {
  5. iter = iter.next;
  6. count++;
  7. }
  8. return count;
  9. }

仅注释掉 insert 方法内的两条打印语句,输出将会是:

  1. inside
  2. 15
  3. 1
  4. 10
  5. 5
  6. 16
  7. 222
英文:

You should not modify the head value inside the length method.

This will fix it:

  1. private int length() {
  2. int counnt = 0;
  3. Node iter = this.head;
  4. while(iter !=null )
  5. {
  6. iter = iter.next;
  7. counnt++;
  8. }
  9. return counnt;
  10. }

Commenting just the two print stamtents inside insert, the output gets to be:

  1. inside
  2. 15
  3. 1
  4. 10
  5. 5
  6. 16
  7. 222

huangapple
  • 本文由 发表于 2020年4月7日 00:32:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定