【问题标题】:java sorted node list examplejava排序节点列表示例
【发布时间】:2016-03-12 20:22:52
【问题描述】:

我是节点列表的新手,我需要搜索与给定 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 方法。
我编辑了这个课程,但我仍然遇到很多麻烦,任何帮助将不胜感激。

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 方法来实现排序的链表。

【问题讨论】:

    标签: java linked-list jgrasp


    【解决方案1】:

    假设您有一个List&lt;PersonNode&gt; 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 类中没有列表。您可能希望有一个作为实例变量。

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

    【讨论】:

    • 这就是我感到困惑的地方。我不允许使用列表,并且 getNext() 方法也不可用。测试类发起一个 SortedPersonList myList = new SortedPersonList();
    • @Jennifer 那么这是不可能的。向你的教授询问他希望你如何完成这个(或其他同学)
    • 您可能误解了分配,因为当对象不包含或不包含列表时,即使有contains 方法也没有意义,您明白吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多