我不知道如何编写函数搜索并在链表中按名称删除。当我编写通过输入员工姓名删除员工的函数时,我遇到了问题。我在搜索功能方面仍然有同样的问题。并且根据需要,我无法使用数组列表。有人可以帮我吗?非常感谢。
这是我的源代码:
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 Testn");
char ch;
/* Perform list operations */
do
{
System.out.println("nCircular Singly Linked List Operationsn");
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');
}
}
你可以在下面的书中找到一个正确的实现。我把书中原样放在这里。
Java™ 中的数据结构和算法第六版
迈克尔·古德里奇、罗伯托·塔马西亚、迈克尔·戈德瓦瑟
/∗∗ 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 -----------