如何删除链表的第一个节点?
How do I delete the first node of a Linked List?
我正在为我的数据结构 class 开发一个项目,该项目要求我编写一个 class 来实现一个整数链表。
- Use an inner class for the Node.
- Include the methods below.
- Write a tester to enable you to test all of the methods with whatever data you want in any order.
我必须创建一个名为“public int deleteFromFront()”的方法。此方法旨在“删除列表前面的节点和 return 其中的 int,如果列表为空,则为 null。”我在下面有这个方法的代码。但是,当我测试此方法时,我得到了错误的输出。它应该 return 被删除的节点的值或者如果列表为空则为 null。因此,例如,如果我有一个像这样的列表“4 3 10 11 3 15 6 11 18 17”并且我想删除第一个节点,该方法应该 return 4 因为 4 是第一个节点。虽然该方法确实删除了 4 它 returns 3 新头的值。有人知道我做错了什么吗?以及如何解决?
import java.util.Random;
import java.util.Scanner;
public class LinkedListOfInts {
Node head;
Node tail;
private class Node {
int value;
Node nextNode;
public Node(int value, Node nextNode) {
this.value = value;
this.nextNode = nextNode;
}
}
public LinkedListOfInts(LinkedListOfInts other) {
Node tail = null;
for (Node n = other.head; n != null; n = n.nextNode) {
if (tail == null)
this.head = tail = new Node(n.value, null);
else {
tail.nextNode = new Node(n.value, null);
tail = tail.nextNode;
}
}
}
public LinkedListOfInts(int[] other) {
Node[] nodes = new Node[other.length];
for (int index = 0; index < other.length; index++) {
nodes[index] = new Node(other[index], null);
if (index > 0) {
nodes[index - 1].nextNode = nodes[index];
}
}
head = nodes[0];
}
public LinkedListOfInts(int N, int low, int high) {
Random random = new Random();
for (int i = 0; i < N; i++)
this.addToFront(random.nextInt(high - low) + low);
}
public void addToFront(int x) {
head = new Node(x, head);
}
public int deleteFromFront() {
if (head == null)
return -1;
else {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.nextNode;
}
}
return head.value;
}
public String toString() {
String result = " ";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
result += ptr.value + " ";
return result;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
LinkedListOfInts copy = new LinkedListOfInts(list);
boolean done = false;
while (!done) {
System.out.println("1. Delete from Front");
System.out.println("2. toString");
switch (input.nextInt()) {
case 1:
System.out.println("Delete an Item at the Front of the List");
System.out.println(list.deleteFromFront());
break;
case 2:
System.out.println("toString");
System.out.println(list.toString());
break;
}
}
}
}
您应该将旧的 head
节点值保存在一个变量中,稍后删除 head node
。稍后,您应该 return 这个变量。
public int deleteFromFront() {
int headValue = -1;
if (head == null)
return headValue;
else {
if (head == tail) {
head = null;
tail = null;
} else {
headValue = head.value;
head = head.nextNode;
}
}
return headValue;
}
我正在为我的数据结构 class 开发一个项目,该项目要求我编写一个 class 来实现一个整数链表。
- Use an inner class for the Node.
- Include the methods below.
- Write a tester to enable you to test all of the methods with whatever data you want in any order.
我必须创建一个名为“public int deleteFromFront()”的方法。此方法旨在“删除列表前面的节点和 return 其中的 int,如果列表为空,则为 null。”我在下面有这个方法的代码。但是,当我测试此方法时,我得到了错误的输出。它应该 return 被删除的节点的值或者如果列表为空则为 null。因此,例如,如果我有一个像这样的列表“4 3 10 11 3 15 6 11 18 17”并且我想删除第一个节点,该方法应该 return 4 因为 4 是第一个节点。虽然该方法确实删除了 4 它 returns 3 新头的值。有人知道我做错了什么吗?以及如何解决?
import java.util.Random;
import java.util.Scanner;
public class LinkedListOfInts {
Node head;
Node tail;
private class Node {
int value;
Node nextNode;
public Node(int value, Node nextNode) {
this.value = value;
this.nextNode = nextNode;
}
}
public LinkedListOfInts(LinkedListOfInts other) {
Node tail = null;
for (Node n = other.head; n != null; n = n.nextNode) {
if (tail == null)
this.head = tail = new Node(n.value, null);
else {
tail.nextNode = new Node(n.value, null);
tail = tail.nextNode;
}
}
}
public LinkedListOfInts(int[] other) {
Node[] nodes = new Node[other.length];
for (int index = 0; index < other.length; index++) {
nodes[index] = new Node(other[index], null);
if (index > 0) {
nodes[index - 1].nextNode = nodes[index];
}
}
head = nodes[0];
}
public LinkedListOfInts(int N, int low, int high) {
Random random = new Random();
for (int i = 0; i < N; i++)
this.addToFront(random.nextInt(high - low) + low);
}
public void addToFront(int x) {
head = new Node(x, head);
}
public int deleteFromFront() {
if (head == null)
return -1;
else {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.nextNode;
}
}
return head.value;
}
public String toString() {
String result = " ";
for (Node ptr = head; ptr != null; ptr = ptr.nextNode)
result += ptr.value + " ";
return result;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedListOfInts list = new LinkedListOfInts(10, 1, 20);
LinkedListOfInts copy = new LinkedListOfInts(list);
boolean done = false;
while (!done) {
System.out.println("1. Delete from Front");
System.out.println("2. toString");
switch (input.nextInt()) {
case 1:
System.out.println("Delete an Item at the Front of the List");
System.out.println(list.deleteFromFront());
break;
case 2:
System.out.println("toString");
System.out.println(list.toString());
break;
}
}
}
}
您应该将旧的 head
节点值保存在一个变量中,稍后删除 head node
。稍后,您应该 return 这个变量。
public int deleteFromFront() {
int headValue = -1;
if (head == null)
return headValue;
else {
if (head == tail) {
head = null;
tail = null;
} else {
headValue = head.value;
head = head.nextNode;
}
}
return headValue;
}