在数据结构 (Java) 中使用链表删除数据



我需要删除链表中的数据。 用于删除节点的代码位于 deleteByKey 方法上。由于单行错误,此代码无法运行。

这是代码:

import java.util.*;
public class LinkedList2nd {
Node head; 

static class Node 
{   
// Data fields for Node   
Object info;  // data stored in the node
Node link;         // link to next node

// Methods
// Constructors
// postcondition: Creates a new empty node.
public Node() {
info = null;
link = null;

}
// postcondition: Creates a new node storing obj.
public Node(Object obj) {
info = obj;
link = null;
}
// postcondition: Creates a new node storing obj 
//   and linked to node referenced by next.
public Node(Object obj, Node next) {
info = obj;
link = next;
} 
// accessors
public Object getInfo() 
{
return info;
}
public Node getLink() 
{
return link;
}

// mutators
public void setInfo(Object newInfo) 
{
info = newInfo;
}
public void setLink(Node newLink) 
{
link = newLink;
}
} 
public static LinkedList2nd insert(LinkedList2nd list,Object info){
Node newNode = new Node(info);
newNode.link = null;
if(list.head == null){
list.head = newNode;
}
else {
//inserting last node 
Node current = list.head;
while(current.getLink() != null){
current = current.getLink();
}//while 
current.setLink(newNode);
}//else

return list;
}
public static void printList(LinkedList2nd list){
Node current = list.head; 
System.out.println("nStudent Namen--------------");
while(current != null){
System.out.println(current.getInfo() + " ");
current = current.getLink();//to next node 


}//while

} //printlist 

public static LinkedList2nd deleteByKey(LinkedList2nd list, String key) 
{ 
// Store head node 
Node current= list.head, prev = null; 
// 
// CASE 1: 
// If head node itself holds the key to be deleted 
if (current!= null && current.info == key) { 
list.head = current.getLink(); // Changed head 
// Display the message 
System.out.println(key + " found and deleted"); 
// Return the updated List 
return list; 
} 
// 
// CASE 2: 
// If the key is somewhere other than at head 
// 
// Search for the key to be deleted, 
// keep track of the previous node 
// as it is needed to change currNode.next 
while (current != null && current.info != key) { 
// If currNode does not hold key 
// continue to next node 
prev = current; 
current = current.getLink(); 
} 
// If the key was present, it should be at currNode 
// Therefore the currNode shall not be null 
if (current != null) { 
// Since the key is at currNode 
// Unlink currNode from linked list 
**prev.getInfo() = current.getLink();**
// Display the message 
System.out.println(key + " found and deleted"); 
} 
// 
// CASE 3: The key is not present 
// 
// If key was not present in linked list 
// currNode should be null 
if (current == null) { 
// Display the message 
System.out.println(key + " not found"); 
} 
// return the List 
return list; 
} 
} 

但它在行 prev.getInfo(( = current.getLink((; 上显示错误 :

if (current != null) { 
// Since the key is at currNode 
// Unlink currNode from linked list 
prev.getInfo() = current.getLink(); 

// Display the message 
System.out.println(key + " found and deleted"); 
} 

它说赋值的左侧必须是一个变量。我找不到此错误的替代方案。请帮助我使此代码工作。谢谢。

在有错误的行上; "prev.getInfo((" 是一个方法调用,而不是一个变量。 您不能从getLink((分配值。您必须使用"set"方法来更新链接。

它应该是这样的:

prev.setLink(current.getLink());

在这一行中 prev.getInfo(( = current.getLink((; 调用 prev 的 setter 方法,然后调用当前 getlink 的方法。 prev.serLink(current.getLink(((;

相关内容

  • 没有找到相关文章

最新更新