嗨我有以下代码,我试图从链接列表中删除链接。我可以删除节点,但每当我尝试遍历列表时,它都会给我原来的计数。有人可以帮我吗?删除链接列表中的链接
//Code from a different Class where I have created an object of the LinkedList class
ll.deleteLinkNumber(6);
//Code from the LinkedList Class
public void deleteLinkNumber(int linkNumber) {
//Link lastLink = getLastLink();
Link currentLink = first;
Link firstLink = currentLink;
int linkCount = 1;
while (first != null) {
if (linkCount == linkNumber) {
first = first.nextLink.nextLink;
System.out.println("Deleted one link: " + linkCount);
}
else {
first = first.nextLink;
System.out.println("Not deleting: " + linkCount);
}
linkCount++;
}
first = firstLink;
System.out.println("dd" + first.nextLink.nextLink.nextLink.nextLink.nextLink.dataElementInt);
//first.nextLink.nextLink.nextLink = first.nextLink.nextLink.nextLink.nextLink.nextLink;
/*
int count = getLinkedListCount(first);
Link firstLink = first;
Link currentLink = first;
first.nextLink.nextLink.nextLink = first.nextLink.nextLink.nextLink.nextLink;
System.out.println("Count of initial list: " + count);
while (currentLink !=null) {
if (count-linkNumber != 1) {
currentLink = currentLink.nextLink;
System.out.println("Not deleting: " + count);
}
else {
currentLink = currentLink.nextLink.nextLink;
System.out.println("Deleted one link: " + count);
}
count--;
}
*/
}
public void printList() {
/*
In the class LinkedList, first is always the first link from the LEFT. For example below
ThirdLink->SecondLink->FirstLink.nextLink = null;
first = ThirdLink;
*/
Link currentLink = first;
int count = 1;
while (currentLink != null) {
currentLink.printLink(count);
count++;
currentLink = currentLink.nextLink;
}
}
}