java如何删除单链表中的当前节点?
我正试图完成一个项目,但我被抓住了。我的deleteCurrentNode方法只能部分工作。由于某种原因,当我试图遍历链表以查找currentNode时,它从未找到它。有人能给我一个如何让它工作的提示吗
该方法本身检查4个条件:
- 如果列表为空李>
- 如果currentNode为空李>
- 如果currentNode是列表中的第一个节点李>
- 如果currentNode在列表中的某个位置李>
(据我所知)其他条件有效。4是问题所在
public class LinkedList
{
private Node currentNode;
private Node firstNode;
private int nodeCount;
public static void main(String[] args)
{
LinkedList test;
String dataTest;
test = new LinkedList();
dataTest = "abcdefghijklmnopqrstuvwxyz";
for(int i=0; i< dataTest.length(); i++) { test.insert(new String(new char[] { dataTest.charAt(i) })); }
System.out.println("[1] "+ test);
for(int i=0; i< dataTest.length(); i++) { test.deleteCurrentNode(); }
System.out.println("[2] "+test);
for(int i=0; i< dataTest.length(); i++)
{
test.insertBeforeCurrentNode(new String(new char[] { dataTest.charAt(i) }));
if(i%2 == 0) { test.first(); } else { test.last(); }
}
System.out.println("[3] "+test);
for(int i=0; i< dataTest.length(); i++) { test.last(); test.deleteCurrentNode(); }
System.out.println("[4] "+test);
for(int i=0; i< dataTest.length(); i++) { test.insert(new String(new char[] { dataTest.charAt(i) })); test.last(); }
System.out.println("[5] "+test);
while(!test.isEmpty()) { test.deleteFirstNode(); }
System.out.println("[6] "+test);
for(int i=0; i< dataTest.length(); i++) { test.insert(new String(new char[] { dataTest.charAt(i) })); test.last(); }
System.out.println("[7] "+test);
while(!test.isEmpty()) { test.deleteFirstNode(false); }
System.out.println("[8] "+test);
for(int i=0; i< dataTest.length(); i++) { test.insertBeforeCurrentNode(new String(new char[] { dataTest.charAt(i) })); test.first(); }
System.out.println("[9] "+test);
}
public LinkedList()
{
setListPtr(null);
setCurrent(null);
nodeCount = 0;
}
public boolean atEnd()
{
//checkCurrent();
return getCurrent().getNext() == null;
}
public boolean isEmpty()
{
return getListPtr() == null;
}
public void first()
{
setCurrent(getListPtr());
}
public void next()
{
checkCurrent();
if (atEnd()) {throw new InvalidPositionInListException("You are at the end of the list. There is no next node. next().");}
setCurrent(this.currentNode.getNext());
}
public void last()
{
if (isEmpty()) {throw new ListEmptyException("The list is currently empty! last()");}
while (!atEnd())
{
setCurrent(getCurrent().getNext());
}
}
public Object getData()
{
return getCurrent().getData();
}
public void insertBeforeCurrentNode(Object bcNode) //beforeCurrentNode
{
Node current;
Node hold;
boolean done;
hold = allocateNode();
hold.setData(bcNode);
current = getListPtr();
done = false;
if (isEmpty())
{
setListPtr(hold);
setCurrent(hold);
}
else if (getCurrent() == getListPtr())
{
// System.out.println("hi" + hold);
hold.setNext(getCurrent());
setListPtr(hold);
}
else if (!isEmpty() && getCurrent() != getListPtr())
{
while (!done && current.getNext() != null)
{
//System.out.println("in else if " + hold);
if (current.getNext() == getCurrent())
{
//previous.setNext(hold);
//System.out.println("hi"+ "yo" + " " + getListPtr());
hold.setNext(current.getNext());
current.setNext(hold);
done = true;
}
//previous = current;
current = current.getNext();
}
}
//System.out.println("current " + getCurrent());
//System.out.println("pointer " + getListPtr());
}
public void insertAfterCurrentNode(Object acNode) //afterCurrentNode
{
Node hold;
hold = allocateNode();
hold.setData(acNode);
if (isEmpty())
{
setListPtr(hold);
setCurrent(hold);
//System.out.println(hold + " hi");
}
else
{
//System.out.println(hold + " hia");
hold.setNext(getCurrent().getNext());
getCurrent().setNext(hold);
}
}
public void insert(Object iNode)
{
insertAfterCurrentNode(iNode);
}
public Object deleteCurrentNode()
{
//System.out.println("in delete current");
Object nData;
Node previous;
if (isEmpty()) {throw new ListEmptyException("The list is currently empty! last()");} //if list is empty throw exception
checkCurrent(); //check if currentNode is null, method throws exception if it is.
nData = getCurrent().getData();
if (getCurrent() == getListPtr())
{
setListPtr(getCurrent().getNext());
setCurrent(getCurrent().getNext());
nodeCount = nodeCount -1;
}
else
{
previous = getListPtr();
while (previous.getNext() != getCurrent())
{
previous = previous.getNext();
//System.out.println("test"+ previous);
}
if (getCurrent().getNext() != null)
{
previous.setNext(null);
}
previous.setNext(getCurrent().getNext()); }
return nData;
}
public Object deleteFirstNode(boolean toDelete)
{
if (toDelete)
{
setListPtr(null);
}
return getListPtr();
}
public Object deleteFirstNode()
{
Object deleteFirst;
deleteFirst = deleteFirstNode(true);
return deleteFirst;
}
public int size()
{
return this.nodeCount;
}
public String toString()
{
String nodeString;
Node sNode;
sNode = getListPtr();
//System.out.println(nodeCount);
nodeString = ("List contains " + nodeCount + " nodes");
while (sNode != null)
{
nodeString = nodeString + " " +sNode.getData();
sNode = sNode.getNext();
}
return nodeString;
}
private Node allocateNode()
{
Node newNode;
newNode = new Node();
nodeCount = nodeCount + 1;
return newNode;
}
private void deAllocateNode(Node dNode)
{
dNode.setData(null);
}
private Node getListPtr()
{
return this.firstNode;
}
private void setListPtr(Node pNode)
{
this.firstNode = pNode;
}
private Node getCurrent()
{
return this.currentNode;
}
private void setCurrent(Node cNode)
{
this.currentNode = cNode;
}
private void checkCurrent()
{
if (getCurrent() == null) {throw new InvalidPositionInListException("Current node is null and is set to an invalid position within the list! checkCurrent()");}
}
/**NODE CLASS ----------------------------------------------*/
private class Node
{
private Node next; //serves as a reference to the next node
private Object data;
public Node()
{
this.next = null;
this.data = null;
}
public Object getData()
{
return this.data;
}
public void setData(Object obj)
{
this.data = obj;
}
public Node getNext()
{
return this.next;
}
public void setNext(Node nextNode)
{
this.next = nextNode;
}
public String toString()
{
String nodeString;
Node sNode;
sNode = getListPtr();
//System.out.println(nodeCount);
nodeString = ("List contains " + nodeCount + " nodes");
while (sNode != null)
{
nodeString = nodeString + " " +sNode.getData();
sNode = sNode.getNext();
}
return nodeString;
}
}
}
[4]的读数应与[2]相同(列表包含0个节点)
注意:不能再使用我已有的任何变量/方法/等。我不能使用头部节点
# 1 楼答案
您可能需要考虑在代码中防范^ {CD1>}。最好不要做假设
比如说,
可能写得更好
这不一定是最好的方法;你可能想扔个
NoCurrentNodeException
什么的。这取决于你要寻找的语义不管怎样,你都不会被埋在
NullPointerExceptions
里# 2 楼答案
如果您有一个节点类,表示列表中的一个节点,如:
在列表实现(比如MyList)中,您将拥有(仅用于删除的方法):
编辑:当数组仅包含1个元素时,会出现一些问题
例如,在您的last()方法中->
# 3 楼答案
非常感谢大家的贡献。他们帮我解决了我的问题(虽然有些模糊)
我在回答我自己的问题,这样如果其他人碰巧有类似的问题,他们可以将其作为参考工具
再一次,谢谢