在数据结构(java)中使用链表删除数据
我需要删除链接列表中的数据。删除节点的代码位于deleteByKey方法上。由于单行错误,此代码无法运行 代码如下:在数据结构(java)中使用链表删除数据,java,data-structures,linked-list,nodes,Java,Data Structures,Linked List,Nodes,我需要删除链接列表中的数据。删除节点的代码位于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
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 Name\n--------------");
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()行显示错误;在这里:
它说赋值的左边必须是一个变量。我找不到这个错误的替代方法。请帮助我使这个代码工作。谢谢。在线上有错误;“prev.getInfo()”是一个方法调用,而不是一个变量。无法从getLink()分配值。您必须使用“set”方法来更新链接 应该是这样的:
prev.setLink(current.getLink())代码>在此行中prev.getInfo()=current.getLink();
调用prev的setter方法,然后调用当前getlink的方法。
prev.serLink(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");
}