java排序节点列表示例

标签 java linked-list jgrasp

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

相关文章:

JAVA区分曲线和直线

java - SoapFault 异常 : [HTTP] Unsupported Media Type when accessing Java web-service from PHP

c++ - C++中链表实现的问题

c - 这个c代码(链表实现)有什么问题?

Java 分数计算器

java - 猜谜游戏添加方法

java - 单例实例化创建多个对象

Java:同步关键字不会阻塞不同线程上的对象

Java - 按行总和对二维数组进行排序

c - 如何知道链表是​​否被正确删除?