0

我不知道如何編寫函數搜索和按名稱在鏈表中刪除。通過輸入員工姓名編寫刪除員工的功能時,我遇到了問題。我仍然有與搜索功能相同的問題。並根據需要我不能使用數組列表。有人可以幫我嗎?非常感謝。如何在Java中通過名稱鏈接列表刪除和搜索對象?

這裏是我的源代碼:

import java.util.Scanner; 
import java.io.Serializable; 

/* Class Node */ 

class Employee implements Serializable{ 
    int ID; 
    String name; 
    String address; 

    Employee(int emp_ID, String emp_name, String emp_address){ 
     ID = emp_ID; 
     name = emp_name; 
     address = emp_address; 
    } 
    public void print(){ 
     System.out.println(ID); 
     System.out.println(name); 
     System.out.println(address); 

    } 
    @Override 
    public String toString() { 
     return ID + "-" + name + "-" + address; 
    } 

} 

class Node 
{ 
    protected Employee emp; 
    protected Node link; 
    public Object name; 
    public Node in; 

    /* Constructor */ 
    public Node() 
    { 
     link = null; 
     emp = null; 
    }  
    /* Constructor */ 
    public Node(Employee e,Node n) 
    { 
     emp = e; 
     link = n; 
    }  
    /* Function to set link to next Node */ 
    public void setLink(Node n) 
    { 
     link = n; 
    }  
    /* Function to set data to current Node */ 
    public void setData(Employee e) 
    { 
     emp = e; 
    }  
    /* Function to get link to next node */ 
    public Node getLink() 
    { 
     return link; 
    }  
    /* Function to get data from current Node */ 
    public Employee getData() 
    { 
     return emp; 
    } 
} 



/* Class linkedList */ 
class linkedList 
{ 
    protected Node start ; 
    protected Node end ; 
    public int size ; 

    /* Constructor */ 
    public linkedList() 
    { 
     start = null; 
     end = null; 
     size = 0; 
    } 
    /* Function to check if list is empty */ 


    /* Function to get size of the list */ 

    /* Function to insert element at the begining */ 
    public void insertAtStart(Employee e) 
    { 
     Node nptr = new Node(e,null);  
     nptr.setLink(start); 
     if(start == null) 
     {    
      start = nptr; 
      nptr.setLink(start); 
      end = start;    
     } 
     else 
     { 
      end.setLink(nptr); 
      start = nptr;   
     } 
     size++ ; 
    } 

    public void searchByName(){ 

    } 

    public void deleteByName(){ 

    } 

} 



/* Class CircularSinglyLinkedList */ 
public class CurrilarLinkedList 
{  
    public static void main(String[] args) 
    {  


     int ID = 0; 
     String name = null; 
     String address = null; 
     Employee emp = null; 
     Scanner scan = new Scanner(System.in); 
     /* Creating object of linkedList */ 
     linkedList list = new linkedList(); 
     System.out.println("Circular Singly Linked List Test\n");   
     char ch; 
     /* Perform list operations */ 
     do 
     { 
      System.out.println("\nCircular Singly Linked List Operations\n"); 
      System.out.println("1. insert at begining"); 
      System.out.println("2. delete by name"); 
      System.out.println("3. search by name"); 
      int choice = scan.nextInt(); 

      switch (choice) 
      { 
      case 1 : 
       System.out.print("Please input an Employee \n"); 
       Scanner myScanner = new Scanner(System.in); 
       System.out.println("Please input an Employee ID"); 
       ID = myScanner.nextInt(); 
       myScanner.nextLine(); 
       System.out.println("Please input an Employee Name"); 
       name = myScanner.nextLine(); 
       System.out.println("Please input an Employee Address"); 
       address = myScanner.nextLine(); 
       emp = new Employee(ID,name,address); 
       list.insertAtStart(emp);      
       break;       
      case 2 :     
       break;       
      case 3 : 
       break;           
      default : 
       System.out.println("Wrong Entry \n "); 
       break; 
      } 

      System.out.println("\nDo you want to continue (Type y or n) \n"); 
      ch = scan.next().charAt(0);    
     } while (ch == 'Y'|| ch == 'y');      
    } 

} 
+0

的可能的複製https://stackoverflow.com/問題/ 18852059/Java的列表containsobject與 - 域值,等於對X –

回答

-1

您可以在下面的書正確實施。我把它放在書裏。

數據結構與算法的Java™第六版

邁克爾·古德里奇,羅伯託·塔馬西亞,邁克爾H.戈德瓦塞爾

/∗∗ A basic doubly linked list implementation. ∗/ 
public class DoublyLinkedList <E> { 
     //---------------- nested Node class ---------------- 
     private static class Node <E> { 
      private E element; 
      private Node <E> prev; 
      private Node <E> next; 
      public Node(E e, Node <E> p, Node <E> n) { 
       element = e; 
       prev = p; 
       next = n; 
      } 
      public E getElement() { 
       return element; 
      } 
      public Node <E> getPrev() { 
       return prev; 
      } 
      public Node <E> getNext() { 
       return next; 
      } 
      public void setPrev(Node <E> p) { 
       prev = p; 
      } 
      public void setNext(Node <E> n) { 
       next = n; 
      } 
     } //----------- end of nested Node class ----------- 
     // instance variables of the DoublyLinkedList 
     private Node <E> header; 
     private Node <E> trailer; 
     private int size = 0; 
     /∗∗ Constructs a new empty list. ∗/ 
     public DoublyLinkedList() { 
      header = new Node < > (null, null, null); 
      trailer = new Node < > (null, header, null); 
      header.setNext(trailer); 
     } 
     // reference to the element stored at this node // reference to the previous node in the list 
     // reference to the subsequent node in the list 
     /∗∗ Returns the number of elements in the linked list. ∗/ 
     public int size() { 
      return size; 
     } 
     /∗∗ Tests whether the linked list is empty. ∗/ 
     public boolean isEmpty() { 
      return size == 0; 
     } 
     /∗∗ Returns (but does not remove) the first element of the list. ∗/ 
     public E first() { 
      if (isEmpty()) return null; 
      return header.getNext().getElement(); // first element is beyond header } 
      /∗∗ Returns (but does not remove) the last element of the list. ∗/ 
      public E last() { 
       if (isEmpty()) return null; 
       return trailer.getPrev().getElement(); // last element is before trailer 
      } 

      // public update methods 
      /∗∗ Adds element e to the front of the list. ∗/ 
      public void addFirst(E e) { 
       addBetween(e, header, header.getNext()); 
      } 
      /∗∗ Adds element e to the end of the list. ∗/ 
      public void addLast(E e) { 
       addBetween(e, trailer.getPrev(), trailer); 
      } 
      /∗∗ Removes and returns the first element of the list. ∗/ 
      public E removeFirst() { 
       if (isEmpty()) return null; // nothing to remove 
       return remove(header.getNext()); // first element is beyond header 
      } 
      /∗∗ Removes and returns the last element of the list. ∗/ 
      public E removeLast() { 
        if (isEmpty()) return null; 
        return remove(trailer.getPrev()); 
       } 
       // private update methods 
      /∗∗ Adds element e to the linked list in between the given nodes. ∗/ 
      private void addBetween(E e, Node <E> predecessor, Node <E> successor) { 
       // create and link a new node 
       Node <E> newest = new Node < > (e, predecessor, successor); 
       predecessor.setNext(newest); 
       successor.setPrev(newest); 
       size++; 
      } 
      /∗∗ Removes the given node from the list and returns its element. ∗/ 
      private E remove(Node <E> node) { 
       Node <E> predecessor = node.getPrev(); 
       Node <E> successor = node.getNext(); 
       predecessor.setNext(successor); 
       successor.setPrev(predecessor); 
       size−−; 
       return node.getElement(); 
      } 
     } //----------- end of DoublyLinkedList class -----------