我在如何使用类类型为 Element 的对象的上一个和下一个实例变量修改指针方面遇到了问题。双向链表填充了具有姓氏、名字、电话号码、上一个和下一个实例变量的 Element 对象。removeElement 方法接受 lastName 作为参数,并查找具有该确切字符串的元素,然后将其从列表中删除。但是,在修改应该从列表中删除元素的指针时,我遇到了异常。具体来说,在这个代码块中:
previousNode.nextElement = currentNode.nextElement;
currentNode = currentNode.nextElement;
currentNode.previousElement = previousNode;
在删除中间的节点,然后尝试删除列表末尾的节点后,它会引发异常。所以我绝对没有正确修改实例变量以链接已删除元素之前和旁边的元素。如何在遵循程序约束的同时将上一个和下一个元素的指针相互设置?
这是代码,以防它有助于答案:
package linkedlist;
import java.util.Scanner;
class Element
{
String firstName;
String lastName;
long phoneNumber;
Element nextElement; // Pointer to next element in the list
Element previousElement; // Pointer to the last element in the list
// Default Constructor
public Element()
{
this.firstName = null;
this.lastName = null;
this.phoneNumber = 0;
this.nextElement = null;
this.previousElement = null;
}
// Constructor providing first and last name
public Element( String first, String last, long number )
{
this.firstName = first;
this.lastName = last;
this.phoneNumber = number;
this.nextElement = null;
this.previousElement = null;
}
public String toString()
{
return( this.firstName + " " + this.lastName + " Cell: " + this.phoneNumber);
}
}
class ElementList
{
Element firstNode;
Element lastNode;
public ElementList()
{
this.firstNode = null;
this.lastNode = null;
}
public void addElement( String first, String last, long number )
{
Element newNode;
newNode = new Element( first, last, number );
if ( this.firstNode == null)
{
this.firstNode = newNode;
this.lastNode = newNode;
}
else
{
this.lastNode.nextElement = newNode;
newNode.previousElement = this.lastNode;
this.lastNode = newNode;
}
}
public void deleteElement( String last )
{
Element currentNode, previousNode = null;
currentNode = this.firstNode; //Temporarily assigns it
while( currentNode != null ) //Checks if there's items.
{
if ( currentNode.lastName.equalsIgnoreCase( last ) == true ) //Checks the last name
{
// We want to delete this node
if ( this.firstNode == currentNode )
{
// Delete first Node, point first node to next element
this.firstNode = this.firstNode.nextElement;
}
else
{
/* Point the next element of the previous element to the next element
of the current element, thus deleting the current element
*/
previousNode.nextElement = currentNode.nextElement;
currentNode = currentNode.nextElement;
currentNode.previousElement = previousNode;
}
break;
}
else
{
// Move to next element
previousNode = currentNode; // Save current node to previous
currentNode = currentNode.nextElement; // Move to next node
}
}
}
public void printElements()
{
Element currentNode;
System.out.println( "nList of Elementsn================");
if ( this.firstNode == null )
{
System.out.println( "No Elements in Listn" );
}
else
{
currentNode = this.firstNode; // Point to first element
while ( currentNode != null ) // Traverse entire list
{
System.out.println( currentNode ); // Print Element contents
currentNode = currentNode.nextElement; // Go to next node
}
}
}
public void printReverseElements()
{
Element currentNode;
System.out.println( "nList of Elements in Reversen================");
if(this.lastNode == null)
{
System.out.println("No Elements in Listn");
}
else
{
currentNode = this.lastNode;
while (currentNode != null)
{
System.out.println(currentNode);
currentNode = currentNode.previousElement;
}
}
}
}
public class LinkedList
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner( System.in );
ElementList list;
String first, last;
double number;
list = new ElementList(); // Instantiate the ElementList
System.out.print( "Enter Last Name, <CR> to Exit : " );
last = keyboard.nextLine(); // Read last Name
while ( last.length() != 0 )
{
System.out.print( "Enter First Name : " );
first = keyboard.nextLine(); // Read first Name
System.out.print("Enter Phone Number : ");
number = keyboard.nextLong();
list.addElement(first, last, (long) number);
list.printElements();// Add Element to ElementList
if ( keyboard.hasNextLine())
{
keyboard.nextLine();
}
System.out.print( "Enter Last Name, <CR> to Exit : " );
last = keyboard.nextLine();
}
list.printElements();
list.printReverseElements();
System.out.print( "Enter Last Name to Delete or <CR> to Exit: " );
last = keyboard.nextLine();
while ( last.length() != 0 )
{
list.deleteElement( last );
list.printElements();
System.out.print( "Enter Last Name to Delete or <CR> to Exit: " );
last = keyboard.nextLine();
}
}
}
或者只是为了方便起见,removeElement 方法:
public void deleteElement( String last )
{
Element currentNode, previousNode = null;
currentNode = this.firstNode; //Temporarily assigns it
while( currentNode != null ) //Checks if there's items.
{
if ( currentNode.lastName.equalsIgnoreCase( last ) == true ) //Checks the last name
{
// We want to delete this node
if ( this.firstNode == currentNode )
{
// Delete first Node, point first node to next element
this.firstNode = this.firstNode.nextElement;
}
else
{
/* Point the next element of the previous element to the next element
of the current element, thus deleting the current element
*/
previousNode.nextElement = currentNode.nextElement;
currentNode = currentNode.nextElement;
currentNode.previousElement = previousNode;
}
break;
}
else
{
// Move to next element
previousNode = currentNode; // Save current node to previous
currentNode = currentNode.nextElement; // Move to next node
}
}
}
作业(跳到任务 5): https://drive.google.com/file/d/1POEAsdNrB3wJPI0ddsbJp2HnUay5pgei/view?usp=sharing
任何答案将不胜感激!
从列表中删除最后一个元素时出现问题。
更新 firstNode, lastNode 对象,如下所示:
public void deleteElement(String last) {
Element currentNode, previousNode = null;
currentNode = this.firstNode;
while (currentNode != null) {
if (currentNode.lastName.equalsIgnoreCase(last) == true) {
if (this.firstNode == currentNode) {
this.firstNode = this.firstNode.nextElement;
if (this.firstNode != null) {
this.firstNode.previousElement = null;
} else {
this.lastNode = null;
}
} else {
previousNode.nextElement = currentNode.nextElement;
if (currentNode.nextElement != null) {
currentNode = currentNode.nextElement;
currentNode.previousElement = previousNode;
} else {
this.lastNode = previousNode;
}
}
break;
} else {
previousNode = currentNode;
currentNode = currentNode.nextElement;
}
}
}