链表,它可以在末尾添加、在开头添加、删除和显示内容 — Java

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

Linked List that appends, prepends, delete and show contents -- Java

问题

  1. public class LinkedListLogic {
  2. private class Node {
  3. Node next;
  4. int data;
  5. public Node(int data) {
  6. this.data = data;
  7. }
  8. }
  9. Node head;
  10. public void addToLastElement(int data) {
  11. if (head == null) {
  12. head = new Node(data);
  13. return;
  14. }
  15. Node current = head;
  16. while (current.next != null) {
  17. current = current.next;
  18. }
  19. current.next = new Node(data); // Appending a new node at the end
  20. }
  21. public void insertAtBeginning(int data) {
  22. Node newHead = new Node(data);
  23. newHead.next = head;
  24. head = newHead;
  25. }
  26. public void deleteAtSpecificValue(int data) {
  27. if (head == null) {
  28. return;
  29. }
  30. if (head.data == data) {
  31. head = head.next; // Removing the first node
  32. return;
  33. }
  34. Node current = head;
  35. while (current.next != null && current.next.data != data) {
  36. current = current.next;
  37. }
  38. if (current.next != null) {
  39. current.next = current.next.next; // Deleting the node with specific value
  40. }
  41. }
  42. public void printAll() {
  43. for (Node current = head; current != null; current = current.next) {
  44. System.out.println(current.data);
  45. }
  46. }
  47. public static void main(String[] args) {
  48. LinkedListLogic a = new LinkedListLogic();
  49. a.addToLastElement(2);
  50. a.addToLastElement(4);
  51. a.printAll();
  52. }
  53. }
英文:

I've just started to learn about linked lists and I am attempting to implement my understanding of it. However, it does not work and program runs without errors.

The program should be able to append, insert at the beginning, delete a node that contains a specific value and print out the contents. The program runs but does not show anything

  1. public class LinkedListLogic {
  2. private class Node {
  3. Node next;
  4. int data;
  5. public Node(int data) {
  6. this.data = data;
  7. }
  8. }
  9. Node head;
  10. public void addToLastElement(int data) {
  11. if (head == null) {
  12. head = new Node(data);
  13. return;
  14. }
  15. Node current = head;
  16. while (current.next != null) {
  17. current = current.next;
  18. }
  19. }
  20. public void insertAtBeginning(int data) {
  21. Node newHead = new Node(data);
  22. newHead.next = head;
  23. head = newHead;
  24. }
  25. public void deleteAtSpecificValue(int data) {
  26. if (head == null) {
  27. return;
  28. }
  29. if (head.data == data) {
  30. head = head.next;
  31. }
  32. Node current = head;
  33. while (current.next.data != data) {
  34. current = current.next;
  35. }
  36. current.next = current.next.next;
  37. return;
  38. }
  39. public void printAll() {
  40. for (Node current = head; current.next != null; current = current.next) {
  41. System.out.println(current.data);
  42. }
  43. return;
  44. }
  45. public static void main(String[] args) {
  46. LinkedListLogic a = new LinkedListLogic();
  47. a.addToLastElement(2);
  48. a.addToLastElement(4);
  49. a.printAll();
  50. }
  51. }

答案1

得分: 0

感谢Andy Turner指出的错误

  1. public class LinkedListLogic {
  2. private class Node {
  3. Node next;
  4. int data;
  5. public Node(int data) {
  6. this.data = data;
  7. }
  8. }
  9. Node head;
  10. public void addToLastElement(int data) {
  11. if (head == null) {
  12. head = new Node(data);
  13. return;
  14. }
  15. Node current = head;
  16. while (current.next != null) {
  17. current = current.next;
  18. }
  19. current.next = new Node(data);
  20. }
  21. public void insertAtBeginning(int data) {
  22. Node newHead = new Node(data);
  23. newHead.next = head;
  24. head = newHead;
  25. }
  26. public void deleteAtSpecificValue(int data) {
  27. if (head == null) {
  28. return;
  29. }
  30. if (head.data == data) {
  31. head = head.next;
  32. }
  33. Node current = head;
  34. while (current.next.data != data) {
  35. current = current.next;
  36. }
  37. current.next = current.next.next;
  38. return;
  39. }
  40. public void printAll() {
  41. Node current;
  42. for (current = head; current.next != null; current = current.next) {
  43. System.out.println(current.data);
  44. }
  45. System.out.println(current.data);
  46. return;
  47. }
  48. public static void main(String[] args) {
  49. LinkedListLogic a = new LinkedListLogic();
  50. a.addToLastElement(2);
  51. a.addToLastElement(4);
  52. a.insertAtBeginning(5);
  53. a.deleteAtSpecificValue(2);
  54. a.addToLastElement(9);
  55. a.addToLastElement(9);
  56. a.addToLastElement(9);
  57. a.addToLastElement(9);
  58. a.deleteAtSpecificValue(9);
  59. a.printAll();
  60. }
  61. }
英文:

Thanks to Andy Turner for pointing out the errors

  1. public class LinkedListLogic {
  2. private class Node {
  3. Node next;
  4. int data;
  5. public Node(int data) {
  6. this.data = data;
  7. }
  8. }
  9. Node head;
  10. public void addToLastElement(int data) {
  11. if (head == null) {
  12. head = new Node(data);
  13. return;
  14. }
  15. Node current = head;
  16. while (current.next != null) {
  17. current = current.next;
  18. }
  19. current.next = new Node(data);
  20. }
  21. public void insertAtBeginning(int data) {
  22. Node newHead = new Node(data);
  23. newHead.next = head;
  24. head = newHead;
  25. }
  26. public void deleteAtSpecificValue(int data) {
  27. if (head == null) {
  28. return;
  29. }
  30. if (head.data == data) {
  31. head = head.next;
  32. }
  33. Node current = head;
  34. while (current.next.data != data) {
  35. current = current.next;
  36. }
  37. current.next = current.next.next;
  38. return;
  39. }
  40. public void printAll() {
  41. Node current;
  42. for (current = head; current.next != null; current = current.next) {
  43. System.out.println(current.data);
  44. }
  45. System.out.println(current.data);
  46. return;
  47. }
  48. public static void main(String[] args) {
  49. LinkedListLogic a = new LinkedListLogic();
  50. a.addToLastElement(2);
  51. a.addToLastElement(4);
  52. a.insertAtBeginning(5);
  53. a.deleteAtSpecificValue(2);
  54. a.addToLastElement(9);
  55. a.addToLastElement(9);
  56. a.addToLastElement(9);
  57. a.addToLastElement(9);
  58. a.deleteAtSpecificValue(9);
  59. a.printAll();
  60. }
  61. }

答案2

得分: 0

  1. public class LinkedListLogic {
  2. private class Node {
  3. Node next;
  4. int data;
  5. public Node(int data) {
  6. this.data = data;
  7. }
  8. }
  9. Node head;
  10. public void addToLastElement(int data) {
  11. if (head == null) {
  12. head = new Node(data);
  13. return;
  14. }
  15. Node current = head;
  16. while (current.next != null) {
  17. current = current.next;
  18. }
  19. current.next = new Node(data);
  20. }
  21. public void insertAtBeginning(int data) {
  22. Node newHead = new Node(data);
  23. newHead.next = head;
  24. head = newHead;
  25. }
  26. public void deleteAtSpecificValue(int data) {
  27. if (head == null) {
  28. return;
  29. }
  30. Node current = head;
  31. Node previous = null;
  32. while (current != null && current.data != data) {
  33. previous = current;
  34. current = current.next;
  35. }
  36. if (current != null && current.data == data) {
  37. previous.next = current.next;
  38. }
  39. return;
  40. }
  41. public void printAll() {
  42. for (Node current = head; current != null; current = current.next) {
  43. System.out.print(current.data + " ");
  44. }
  45. System.out.println();
  46. return;
  47. }
  48. public static void main(String[] args) {
  49. LinkedListLogic a = new LinkedListLogic();
  50. a.addToLastElement(2);
  51. a.addToLastElement(4);
  52. a.addToLastElement(6);
  53. a.addToLastElement(9);
  54. a.printAll();
  55. a.insertAtBeginning(1);
  56. a.printAll();
  57. a.deleteAtSpecificValue(6);
  58. a.printAll();
  59. a.deleteAtSpecificValue(9);
  60. a.printAll();
  61. }
  62. }
英文:
  1. public class LinkedListLogic {
  2. private class Node {
  3. Node next;
  4. int data;
  5. public Node(int data) {
  6. this.data = data;
  7. }
  8. }
  9. Node head;
  10. public void addToLastElement(int data) {
  11. if (head == null) {
  12. head = new Node(data);
  13. return;
  14. }
  15. Node current = head;
  16. while (current.next != null) {
  17. current = current.next;
  18. }
  19. current.next = new Node(data);
  20. }
  21. public void insertAtBeginning(int data) {
  22. Node newHead = new Node(data);
  23. newHead.next = head;
  24. head = newHead;
  25. }
  26. public void deleteAtSpecificValue(int data) {
  27. if (head == null) {
  28. return;
  29. }
  30. Node current = head;
  31. Node previous = null;
  32. while ( current != null && current.data != data) {
  33. previous = current;
  34. current = current.next;
  35. }
  36. if(current != null && current.data == data) {
  37. previous.next = current.next;
  38. }
  39. return;
  40. }
  41. public void printAll() {
  42. for (Node current = head; current != null; current = current.next) {
  43. System.out.print(current.data+" ");
  44. }
  45. System.out.println();
  46. return;
  47. }
  48. public static void main(String[] args) {
  49. LinkedListLogic a = new LinkedListLogic ();
  50. a.addToLastElement(2);
  51. a.addToLastElement(4);
  52. a.addToLastElement(6);
  53. a.addToLastElement(9);
  54. a.printAll();
  55. a.insertAtBeginning(1);
  56. a.printAll();
  57. a.deleteAtSpecificValue(6);
  58. a.printAll();
  59. a.deleteAtSpecificValue(9);
  60. a.printAll();
  61. }
  62. }

huangapple
  • 本文由 发表于 2020年9月2日 03:08:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63693958.html
匿名

发表评论

匿名网友

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

确定