gpt4 book ai didi

java - 如何使用删除方法删除链表中的第一个节点

转载 作者:行者123 更新时间:2023-12-01 13:12:02 24 4
gpt4 key购买 nike

我想创建一个链接列表,然后用 1-100 填充它。之后,我打印出所有偶数,但实际上没有从列表中删除奇数,然后再次打印出偶数,但将它们加倍。在这些事情之后,我从链接列表中删除奇数并打印出列表。我提到的最后一步是我挂断的地方。其他一切都工作正常,只是我的删除方法删除了除 1 之外的所有奇数。在我的主要方法中,我使用 if 语句表示如果节点 % 2 中包含的数字等于零,则删除该节点。它适用于除第一个节点之外的每个节点。谢谢你们能给我的任何帮助。我的代码如下。

import java.util.*;

/*
* My ListNode class
*/
class ListNode<Integer> {
private Integer item;
private ListNode<Integer> next;

public ListNode(Integer item) {
this.item = item;
next = null;
}

public ListNode(Integer item, ListNode<Integer> next) {
this.item = item;
this.next = next;
}

public Integer getItem() {
return item;
}

public ListNode<Integer> getNext() {
return next;
}

public void setItem(Integer item) {
this.item = item;
}

public void setNext(ListNode<Integer> next) {
this.next = next;

}
}

/*
* My LinkedList class
*/
class LinkedList<Integer> {
public ListNode<Integer> front;

public LinkedList() {
front = null;
}

public boolean isEmpty() {
return front == null;
}

public boolean contains(int target) {
for (ListNode<Integer> node = front;
node != null;
node = node.getNext()) {
if (node.getItem().equals(target)) {
return true;
}
}
return false;
}

public int size() {
int count = 0;
for (ListNode<Integer> node = front;
node != null;
node = node.getNext()) {
count++;
}
return count;
}

public String toString() {
String result = "( ";
for (ListNode<Integer> node = front;
node != null;
node = node.getNext()) {
result += node.getItem() + " ";
}
return result + ")";
}

public Integer get(int index) {
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
return node.getItem();
}

public void set(int index, Integer target) {
ListNode<Integer> node = front;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
node.setItem(target);
}

public void add(int index, int target) {
if (isEmpty()) {
front = new ListNode(target);
} else {
ListNode last = front;
while (last.getNext() != null) {
last = last.getNext();
}
last.setNext(new ListNode(target));
}
}

public Integer remove(int index) {
ListNode<Integer> node = front;
ListNode<Integer> prev = front;
for (int i = 0; i < index; i++) {
prev = node;
node = node.getNext();
}
prev.setNext(node.getNext());
return node.getItem();
}
}
public class LinkedListTest {
//interface Removal {
//Integer remove (Integer item);
//}
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();

System.out.println(list);

System.out.println("The list size is " + list.size());
System.out.println();

/*
* This adds the numbers 1 through 100 to a LinkedList
*/
for (int i = 1; i <= 100; i++)
list.add(0, i);

System.out.println(list);


System.out.println("The list size is " + list.size());
System.out.println();

/*
* This prints out only even numbers by excluding indexes that are even,
* because all the even numbers are held in the odd numbered indexes, thus
* index 0 is 1 but index 1 is 2, index 3 is 4
*/
for (int i = 0; i < list.size(); i++)
if (i % 2 == 1) {
System.out.print(list.get(i) + " ");
}

System.out.println();

System.out.println("The list size is " + list.size());
System.out.println();

/*
* This doubles even numbers
*/
for (int i = 0; i < list.size(); i++)
if (i % 2 == 1) {
int result = list.get(i) * 2;
System.out.print(result + " ");
}
System.out.println();
System.out.println("The list size is " + list.size());
System.out.println();

for (int i = 0; i < list.size(); i++)
if (list.get(i) % 2 == 1) {
list.remove(i);
}

System.out.print(list);
System.out.println();
System.out.println("The list size is " + list.size());
System.out.println();
/*
* These contain methods only work for the first list created
*/
System.out.println("Does the list contain 32? " + list.contains(32));

System.out.println("Does the list contain 33? " + list.contains(33));

}
}

最佳答案

您的remove当值为 index 时,该方法实际上不起作用是 0 ..因为你的nodeprev初始化为front并且循环没有被执行,因为 i<index条件为假。所以你必须为 index=0 的情况添加另一个条件在 remove 的开头添加这些行方法解决了问题..

if(index==0){
ListNode<integer>temp=front;
front=front.getNext();
return temp.getItem();
}

希望对你有帮助...

关于java - 如何使用删除方法删除链表中的第一个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22775742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com