gpt4 book ai didi

java排序节点列表示例

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:33 26 4
gpt4 key购买 nike

我是节点列表的新手,我需要搜索检查与给定 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;
}
}

这是我的方法类、构造函数和实例变量。我不知道怎么做的方法是contains方法。
我编辑了这个类(class),但我仍然遇到很多麻烦,我们将不胜感激任何帮助。

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;
}

如果有帮助,目标是修复添加、包含、删除和获取方法以实现排序链表。

最佳答案

假设您有一个 List<PersonNode> lst .你想检查是否 PersonNodeid = ID在此列表中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没有列表类(class)。您可能希望将一个作为实例变量。

或者,如果您决定要实现的内容等同于 LinkedList , 那么你可以在没有 foreach 的情况下进行相同的迭代而是使用 node.getNext() 前进...

关于java排序节点列表示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35962847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com