我是节点列表的新手,我需要搜索检查与给定ID关联的PersonNode是否在列表中
是我的 PersonNode 类
public class PersonNode
{
// instance variables
private int m_ID;
private String m_name;
private PersonNode m_link;
// constructor
public PersonNode(int ID, String name)
{
m_ID = ID;
m_name = name;
m_link = null;
}
// getters and setters
public void setID(int ID)
{
m_ID = ID;
}
public int getID()
{
return m_ID;
}
public String getName()
{
return m_name;
}
public void setName(String name)
{
m_name = name;
}
public void setLink(PersonNode link)
{
m_link = link;
}
public PersonNode getLink()
{
return m_link;
}
}
这是我的方法类、构造函数和实例变量。 我不知道该怎么做的方法是包含方法。
我编辑了这个课程,我仍然遇到很多麻烦,任何帮助将不胜感激。
public class SortedPersonList
{ // instance variables
private PersonNode m_first;
private int m_numElements;
// constructor
// Do not make any changes to this method!
public SortedPersonList()
{
m_first = null;
m_numElements = 0;
}
// check whether the list is empty
// Do not make any changes to this method!
boolean isEmpty()
{
if (m_first == null)
return true;
else
return false;
}
// return the size of the list (# of Person nodes)
// Do not make any changes to this method!
public int size()
{
return m_numElements;
}
// check whether a PersonNode associated with the given ID is in the list
public boolean contains(int ID)
{This Method I need help with!!
}
// search for and return the PersonNode associated with the given ID
public PersonNode get(int ID)
{ PersonNode current=m_first;
while(current!=null)
{if (current.getID()==ID)
return current;
}
return null;
}
// add a new PersonNode to the list with the given ID and name
public boolean add(int ID, String name)
{ PersonNode newNode=new PersonNode(ID,name);
PersonNode previous=null;
if (m_first == null)
{ // add element to an empty list
m_first = newNode;
m_first.setLink(previous);
m_numElements++;}
else
{if (newNode.getID()<m_first.getID())
{previous=m_first;
m_first=newNode;
m_first.setLink(previous);
m_numElements++;
}
else
{ m_first.setLink(newNode);
newNode.setLink(previous);
m_numElements++;}}
return true; // replace this statement with your own return
}
// remove a PersonNode associated with the given ID from the list
public boolean remove(int ID)
{PersonNode remove = get(ID);
while(remove.getID()==ID)
{m_first=null;
m_first.setLink(remove.getLink());}
m_numElements --;
return true;}
public String toString()
{
String listContent = "";
PersonNode current = m_first;
while (current != null)
{
listContent += "[" + current.getID() + " | " + current.getName() + "] ";
current = current.getLink();
}
return listContent;
}
}
如果它有帮助,目标是修复 add、contains、remove 和 get 方法以实现排序链表。
您有一个List<PersonNode> lst
。您要检查带有id = ID
PersonNode
是否在此列表中 lst
。您可以通过迭代列表来轻松做到这一点:
public boolean contains(int ID) {
for (PersonNode node : lst) {
if (node.getID() == ID)
return true; // node found, we can finish iterating
}
return false; // no node with id = ID was found
}
请注意,截至目前,您的SortedPersonList
类中没有列表。您可能希望有一个作为实例变量。
或者,如果您决定要实现的是等效于 LinkedList
,那么您可以在没有foreach
的情况下进行相同的迭代,而是以node.getNext()
前进......