Search code examples
javalinked-listjgrasp

java sorted node list example


I am new to node lists and I need to search check whether a PersonNode associated with the given ID is in the list

the is my PersonNode class

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

here is my method class, constructor and instance variable. The method I don't know how to do is the contains method.
I edited this Class and I am still having so much trouble with this any help would be greatly appreciated.

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

}

If it helps the goal is to fix the add, contains, remove and get methods to implement a sorted linked list.


Solution

  • Let's assume you have a List<PersonNode> lst. You want to check if a PersonNode with id = ID is in this list lst. You can do that rather easily by just iterating over the list:

    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
    }
    

    Take note that as of this moment you don't have a list in your SortedPersonList class. You probably want to have one as an instance variable.

    Alternatively, if you decide that what you wanted to implement is an equivalent of a LinkedList, then you can do the same iteration just without foreach and instead advance with node.getNext()...