使用另一个类实现LinkedList的另一个问题



员工类别:

public class Employee
{
String ID;
String Fname;
String Lname;
String City;
String Major;
int GPA;
    public Employee()
{
    GPA = 0;
}

public void ShowInfo()
{
    JOptionPane.showMessageDialog(null, "ID: " + ID + "nFirst Name: " + Fname + "nLast Name:" + Lname + "nCity: " + City + "nMajor: " + Major + "nGPA: " + GPA); 
}
public void EnterInfo()
{
    ID = JOptionPane.showInputDialog("Enter Student ID");
    Fname = JOptionPane.showInputDialog("Enter Student First Name");
    Lname = JOptionPane.showInputDialog("Enter Student Last Name");
    City = JOptionPane.showInputDialog("Enter Student City");
    Major = JOptionPane.showInputDialog("Enter Student Major");
    String gpa = JOptionPane.showInputDialog("Enter Student GPA");
    GPA = Integer.parseInt(gpa);
}
  }
}

链表:

public class EmployeeList
{
Node first;
Node last;
int count;

public EmployeeList()
{
    first = new Node();
    first = null;
    last = new Node();
    last = null;
    count = 0;
}
public boolean empty()
{
    return first == null;
}
public void add(Employee emp)
{
    Node newEmployee = new Node();
    newEmployee.e = emp;
    newEmployee.e.EnterInfo();
    newEmployee.next=null;
    if(empty())
    {
        first=newEmployee;
        last=first;
    }       
    else  
    {
        last.next = newEmployee; 
        last = last.next;
    }
    count++;
}
public boolean search(String id)
{
    Node temp = new Node();
    Employee emp = new Employee();
    temp = first;
    while(temp!=null)
    {
        emp = temp.e;
        if(id.equals(emp.ID)) 
        {
            emp.ShowInfo();
            return true;
        }
        temp=temp.next;
    }  
    return false;  
 }
public boolean delete(String id)
{
    Employee emp = new Employee();
    if(!empty())
    { 
        if(first.e.ID.equals(id))
        { 
            first=first.next; 
            return true; 
        }
        else
        {
            Node previous = new Node();
            Node temp = new Node();
            previous = first;
            temp = first.next;
            while(temp!=null)
            {   
                emp = temp.e;
                if(id.equals(emp.ID))
                {
                    count--;
                    previous.next = temp.next;
                    return true;
                }
                previous = previous.next;
                temp = temp.next;
            }  
            return false;
        }
    }
return false;
}
public String ALL()
{
    String all = new String();
    Node temp = new Node();
    Employee emp = new Employee();
    temp = first;
    while(temp!=null)
    {
        emp = temp.e;
        all = all + emp.ID + "-";
        temp = temp.next;
    } 
    all = all + "null";
    return all;
} 

}

我真的不知道这里有什么问题,如果我试图打印所有的,我会一直得到最后输入的值。

节点类别:

public class Node
{
Employee e = new Employee();
Node next;
}

通过搜索我没有得到任何结果,只是没有找到员工ID。EnterInfo方法只用于输入变量(ID,Fname…..)

有什么帮助吗?谢谢。

编辑:我知道这样做是错误的,我应该添加getter和setter,但老师就是这样开始的,并告诉我们要这样开始。

您的搜索失败,因为您错误地测试了字符串相等性。此行:

if(emp.ID == id)

测试对象引用的相等性。它只适用于内部价值观(这超出了你的任务范围)。你也应该改变它:

if(id.equals(emp.ID))

关于你的代码的一些快速注释:

  • 您没有遵循命名方面的最佳做法。您的变量应该以小写字母开头
  • 您没有遵循属性范围界定的最佳实践。你的班级变量应该是私有的,带有适当的getter/setter
  • 在搜索方法开始时,您不必要地创建节点实例

    Node temp=new Node();

  • 您在delete中错误地测试字符串相等性方法

相关内容

  • 没有找到相关文章

最新更新