java - 使用数据结构中的链表删除数据(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 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(); 行显示错误 这里 :
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() = current.getLink(); 先调用prev的setter方法,再调用当前getlink的方法。prev.serLink(current.getLink());
推荐阅读
- azure-devops - 使用同一个 YAML 文件的多个构建
- python - 随机数练习
- javascript - 设置 setHours() 时,Javascript Date() 不会实时更新
- c++ - QT 5.13.2 C++ 问题成员访问不完整类型“Ui::UsersWidget”
- unity3d - Unity NVIDIA Flex,加力
- javascript - 通过现有数组过滤Javascript对象数组
- oracle - 光标 PL/SQL ORACLE
- c++ - Qt 不会连接到子类中的插槽
- bash - 哈希冒号 (#:) 作为 shell 脚本的第一行是什么意思?
- c++ - 如何在数组中输入没有空格?