2017-03-16 18 views
-3

我想做一個項目,我不知道如何找到並刪除鏈接列表中的某個節點。我的教授希望我們使用這些確切的類。如何從鏈接列表中找到並刪除某個節點。使用這些特定類的java

public class StudentList { 

    private StudentNode shead ; 

    public StudentList(){} 
    public void setShead(StudentNode sh) 
    { 
     shead = sh; 
    } 

    public StudentNode getShead() 
    { 
     return shead; 
    } 
    public void deleteStudentNode(StudentNode s) 
    {  
    } 

    public StudentNode findStudentByName(String s) 
    { 
    } 
} 

我不會浪費了accessor和mutator方法下一個類空間

學生類

public class Student extends Person{ 

    private String major; 
    private double gpa; 
    public Student(){} 
} 

人類

public class Person { 
    private String name; 
    private String gender; 
    public Person(){} 
    public Person (String n, String g) 
    { 
     n = name; 
     g = gender; 
    } 
} 

編輯,尤其是圓形的是學生節點

public class StudentNode { 
private Student student; 
private StudentNode sptr; 
private FriendList fptr; 

StudentNode() 
{ } 

public StudentNode(Student s) 
{ 
    s = student; 
}  

public void setStudent(Student s) 
{ 
    student = s; 
} 

public Student getStudent() 
{ 
    return(student); 
} 

public void setSptr(StudentNode s) 
{ 
    sptr = s; 
} 

public StudentNode getSptr() 
{ 
    return(sptr); 
} 
+0

所以你必須自己創建'StudentNode'類?你有沒有做過這方面的工作? (並告訴我們!) –

+0

@StephenP我現在加了它 – abc123

回答

0

如果要查找或刪除具有學生姓名的學生節點,則需要訪問姓名字段。你可以添加一些的getter/setter的Person類:

public class Person { 
    private String name; 
    private String gender; 

    public Person() { 
    } 

    public Person(String n, String g) { 
     n = name; 
     g = gender; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

然後實現查找和刪除這樣的:

public class StudentList { 

    private StudentNode shead; 

    public StudentList() { 
    } 

    public void setShead(StudentNode sh) { 
     shead = sh; 
    } 

    public StudentNode getShead() { 
     return shead; 
    } 


    private boolean isSameStudent(Student a, Student b) { 
     return a != null && b != null && a.getName().equals(b.getName()); 
    } 

    public void deleteStudentNode(StudentNode s) { 
     if (s == null) { 
      // Cannot delete a null node 
      return; 
     } 
     if (isSameStudent(s.getStudent(), shead.getStudent())) { 
      // If the node to delete is head 
      shead = shead.getSptr(); 
      return; 
     } 
     StudentNode ptr = shead; 

     while (ptr != null) { 
      if (ptr.getSptr() == null) { 
       // There is no node after ptr 
       return; 
      } 
      if (isSameStudent(ptr.getSptr().getStudent(), s.getStudent())) { 
       // The next node is the node to delete 
       ptr.setSptr(ptr.getSptr().getSptr()); 
       break; 
      } 
      ptr = ptr.getSptr(); 
     } 
    } 

    public StudentNode findStudentByName(String s) { 
     StudentNode ptr = getShead(); 
     while (ptr != null) { 
      if (ptr.getStudent().getName().equals(s)) { 
       return ptr; 
      } 
      ptr = ptr.getSptr(); 
     } 
     // not found 
     return null; 
    } 
} 

跳上它可以幫助你!