gpt4 book ai didi

java - 自定义列表类,如何循环hasNext方法?

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:46 24 4
gpt4 key购买 nike

大家好,我是 java 新手,所以我非常感谢对此的任何帮助。好吧,这就是问题所在:我有一个列表类和一个 listNode 类,列表类由名称、firstNode 和lastNode 表示。firstNode 和 lastNode 来自类型 listNode,listNode 由一个对象(例如 data 或 Object o)和一个 nextNode 表示,后者指向列表中的下一个节点,该节点也来自类型 listNode。

列表类:

public class List {

private ListNode firstNode;
private ListNode lastNode;
private String name;

public List() {
this("list");
}

public List(String listName) {
name = listName;
firstNode = lastNode = null;
}

public void insertAtFront(Object insertItem) {
if (isEmpty())
firstNode = lastNode = new ListNode(insertItem);
else
firstNode = new ListNode(insertItem, firstNode);
}

public void insertAtBack(Object insertItem) {
if (isEmpty())
firstNode = lastNode = new ListNode(insertItem);
else
lastNode = lastNode.nextNode = new ListNode(insertItem);
}

public Object removeFromFront() throws EmptyListException {
if (isEmpty())
throw new EmptyListException(name);
Object removedItem = firstNode.data;

if (firstNode == lastNode)
firstNode = lastNode = null;
else
firstNode = firstNode.nextNode;
return removedItem;
}

public Object removeFromBack() throws EmptyListException {
if (isEmpty())
throw new EmptyListException(name);

Object removedItem = lastNode.data;
if (firstNode == lastNode)
firstNode = lastNode = null;
else {
ListNode current = firstNode;

while (current.nextNode != lastNode)
current = current.nextNode;

lastNode = current;
current.nextNode = null;
}
return removedItem;
}

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

public void print() {
if (isEmpty()) {
System.out.printf("Empty %s\n", name);
return;
}
System.out.printf("The %s is : ", name);
ListNode current = firstNode;

while (current != null) {
System.out.printf("%s", current.data);
current = current.nextNode;
}
System.out.println("\n");
}

@Override
public String toString() {
String stk = "(";
if(isEmpty())return "Empty List";
ListNode checkNode = firstNode;
while (checkNode != null) {
stk += checkNode.data.toString()+ " , ";
checkNode = checkNode.nextNode;
}
return stk+")";
}
public ListNode removeAt (int k){
if(k<=0 || k>getLength())
try{
throw new IllegalValues();
}catch(IllegalValues iv){
iv.printStackTrace();
return null;
}
ListNode newNode = firstNode;
if(k==1){
newNode = firstNode;
firstNode = firstNode.nextNode;
return newNode;
}
if(k==2){
newNode = firstNode.nextNode;
firstNode.nextNode = firstNode.nextNode.nextNode;
return newNode;
}

if(k==3){
newNode = firstNode.nextNode;
firstNode.nextNode.nextNode = firstNode.nextNode.nextNode.nextNode;
return newNode;
}

if(k==4){
newNode = firstNode.nextNode;
firstNode.nextNode.nextNode.nextNode = firstNode.nextNode.nextNode.nextNode.nextNode;
return newNode;
}

return newNode;
}
public int getLength(){
ListNode checkNode = firstNode;
int count =0;
while (checkNode != null) {
count++;
checkNode = checkNode.nextNode;
}
return count;
}

}

列表节点类:

public class ListNode {

Object data;
ListNode nextNode;

public ListNode(Object o) {
this(o, null);
}

public ListNode(Object o, ListNode node) {
data = o;
nextNode = node;
}

public Object getObject() {
return data;
}

public ListNode getNext(){
return nextNode;
}

}

这是我正在使用的两个类。 我的问题是removeAt()方法我不知道如何概括它并为所有代码(如for语句)做出一般性答案我只能通过在if语句中单独编写每个案例来使其工作。我需要编写一个 for 循环,以某种方式可以循环抛出 hasNext() 方法。有任何想法吗??提前致谢

最佳答案

好吧,为了删除第 k 个节点(当 k>1 时),您始终应该执行一个节点:

someNode.nextNode = someNode.nextNode.nextNode;

这是您要删除的节点之前的节点,这使其成为第 k-1 个节点。

您可以通过 for 循环找到该节点:

if (k==1) {
ListNode removedNode = firstNode;
firstNode = firstNode.nextNode;
return removedNode;
}
ListNode someNode = firstNode;
for (int i = 1; i < k - 1; i++) {
someNode = someNode.nextNode;
}
ListNode removedNode = someNode.nextNode;
someNode.nextNode = someNode.nextNode.nextNode;
return removedNode;

请注意,k==1 的情况是单独处理的。

关于java - 自定义列表类,如何循环hasNext方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44822676/

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