英文:
Linked List that appends, prepends, delete and show contents -- Java
问题
public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data); // Appending a new node at the end
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
if (head.data == data) {
head = head.next; // Removing the first node
return;
}
Node current = head;
while (current.next != null && current.next.data != data) {
current = current.next;
}
if (current.next != null) {
current.next = current.next.next; // Deleting the node with specific value
}
}
public void printAll() {
for (Node current = head; current != null; current = current.next) {
System.out.println(current.data);
}
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic();
a.addToLastElement(2);
a.addToLastElement(4);
a.printAll();
}
}
英文:
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
public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
if (head.data == data) {
head = head.next;
}
Node current = head;
while (current.next.data != data) {
current = current.next;
}
current.next = current.next.next;
return;
}
public void printAll() {
for (Node current = head; current.next != null; current = current.next) {
System.out.println(current.data);
}
return;
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic();
a.addToLastElement(2);
a.addToLastElement(4);
a.printAll();
}
}
答案1
得分: 0
感谢Andy Turner指出的错误
public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
if (head.data == data) {
head = head.next;
}
Node current = head;
while (current.next.data != data) {
current = current.next;
}
current.next = current.next.next;
return;
}
public void printAll() {
Node current;
for (current = head; current.next != null; current = current.next) {
System.out.println(current.data);
}
System.out.println(current.data);
return;
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic();
a.addToLastElement(2);
a.addToLastElement(4);
a.insertAtBeginning(5);
a.deleteAtSpecificValue(2);
a.addToLastElement(9);
a.addToLastElement(9);
a.addToLastElement(9);
a.addToLastElement(9);
a.deleteAtSpecificValue(9);
a.printAll();
}
}
英文:
Thanks to Andy Turner for pointing out the errors
public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
if (head.data == data) {
head = head.next;
}
Node current = head;
while (current.next.data != data) {
current = current.next;
}
current.next = current.next.next;
return;
}
public void printAll() {
Node current;
for (current = head; current.next != null; current = current.next) {
System.out.println(current.data);
}
System.out.println(current.data);
return;
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic();
a.addToLastElement(2);
a.addToLastElement(4);
a.insertAtBeginning(5);
a.deleteAtSpecificValue(2);
a.addToLastElement(9);
a.addToLastElement(9);
a.addToLastElement(9);
a.addToLastElement(9);
a.deleteAtSpecificValue(9);
a.printAll();
}
}
答案2
得分: 0
public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
Node current = head;
Node previous = null;
while (current != null && current.data != data) {
previous = current;
current = current.next;
}
if (current != null && current.data == data) {
previous.next = current.next;
}
return;
}
public void printAll() {
for (Node current = head; current != null; current = current.next) {
System.out.print(current.data + " ");
}
System.out.println();
return;
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic();
a.addToLastElement(2);
a.addToLastElement(4);
a.addToLastElement(6);
a.addToLastElement(9);
a.printAll();
a.insertAtBeginning(1);
a.printAll();
a.deleteAtSpecificValue(6);
a.printAll();
a.deleteAtSpecificValue(9);
a.printAll();
}
}
英文:
public class LinkedListLogic {
private class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
}
}
Node head;
public void addToLastElement(int data) {
if (head == null) {
head = new Node(data);
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = new Node(data);
}
public void insertAtBeginning(int data) {
Node newHead = new Node(data);
newHead.next = head;
head = newHead;
}
public void deleteAtSpecificValue(int data) {
if (head == null) {
return;
}
Node current = head;
Node previous = null;
while ( current != null && current.data != data) {
previous = current;
current = current.next;
}
if(current != null && current.data == data) {
previous.next = current.next;
}
return;
}
public void printAll() {
for (Node current = head; current != null; current = current.next) {
System.out.print(current.data+" ");
}
System.out.println();
return;
}
public static void main(String[] args) {
LinkedListLogic a = new LinkedListLogic ();
a.addToLastElement(2);
a.addToLastElement(4);
a.addToLastElement(6);
a.addToLastElement(9);
a.printAll();
a.insertAtBeginning(1);
a.printAll();
a.deleteAtSpecificValue(6);
a.printAll();
a.deleteAtSpecificValue(9);
a.printAll();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论