首页 > 解决方案 > 使用数据结构中的链表删除数据(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"); 
        } 

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

标签: javadata-structureslinked-listnodes

解决方案


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


推荐阅读