为链接列表创建getLast方法

时间:2014-04-27 15:49:41

标签: java data-structures linked-list

我正在尝试用Java编写getLast方法代码。我知道我要做什么,我只是编码时遇到了麻烦。我需要做的是遍历链表,直到p.link指向null,然后我可以添加到列表的末尾。这是我到目前为止所做的:

class MyLinkedList 
{
   private class Node           
   {
      private Node link;
      private int x;
   }
   //----------------------------------
   private Node first = null;    
   //----------------------------------
   public void addFirst(int d)
   {
      Node newNode = new Node(); 
      newNode.x = d;            
      newNode.link = first;      
      first = newNode;           
   } 

   public void addLast(int q)
   {
      Node newNode = new Node();
      newNode.x = q;
      newNode.link = null;  //points to null because nothing should follow it
   //----------------------------------
   public void traverse()
   {
      Node p = first;
      while (p != null)            
      {
         System.out.println(p.x);  
         p = p.link;               
      }
   }
}
//==============================================
class C15h1  
{
   public static void main(String[] args)
   {
      MyLinkedList list = new MyLinkedList();
      list.addLast(4);
      list.addLast(5);
     list.addLast(6);
      list.addFirst(1);
      list.addFirst(2);
      list.addFirst(3);
      System.out.println("Numbers on list");
      list.traverse();
   }
}

5 个答案:

答案 0 :(得分:1)

这可能是你想要的?

public Node getLast()
{
    Node lastNode = first;
    Node next = first.link;

    while (next != null)
    { 
         lastNode = next;
         next = next.link; 
    }

    return lastNode;
}

答案 1 :(得分:1)

我会跟踪最后一个节点:

class MyLinkedList 
{
   private class Node           
   {
        private Node link;
        private int x;
   }
    //----------------------------------
    private Node first = null;    
    private Node last = null;    
    //----------------------------------

    public void addFirst(int d)
    {
        Node newNode = new Node(); 
        newNode.x = d;            
        newNode.link = first;      
        first = newNode;
        if(last == null)
            last = newNode;           
    } 

    public void addLast(int q)
    {
        Node newNode = new Node();
        newNode.x = q;
        newNode.link = null;  //points to null because nothing should follow it
        if(last != null)
            last.link = newNode;
        last = newNode;
        if(first == null) // This is the first node
            first = newNode;
    }

    //----------------------------------
    public void traverse()
    {
        Node p = first;
        while (p != null)            
        {
            System.out.println(p.x);  
            p = p.link;               
        }
    }

    public int getLast()
    {
        if(last != null)
            return last.q;
        return -1;
    }
}

答案 2 :(得分:0)

当前代码中的问题在traverse()方法中。它会一直循环到p == null,这意味着当p"掉落"不在列表中。你想要的是在这个p指向列表的最后一项之前停止一步。为此,您只需将条件从while (p != null)更改为while (p.link != null)

以下是addLast()的样子:

public void addLast(int q)
{
   Node newNode = new Node();
   newNode.x = q;
   newNode.link = null;  //points to null because nothing should follow it

   Node p = first;
   while (p.link != null) {
      p = p.link;               
   }
   p.link = newNode;
}

答案 3 :(得分:0)

迭代直到你使用while循环获得最后一个节点,并且一旦最后一个节点返回它就返回它..

 public Node getLast(){
    Node p = first;
          while (p.link != null)            
          { 
             p = p.link;               
          }
    return p;
    }

答案 4 :(得分:0)

您需要遍历列表直至结束。 (就像你提到的那样。'直到p.link指向null,')

你可以这样做:

public void addLast(int q) {
    Node newNode = new Node();
    newNode.x = q;
    newNode.link = null;
    if (first == null) {
        first = newNode;
    } else {
        Node lastNode = first;
        while (lastNode != null && lastNode.link != null) {
            lastNode = lastNode.link;
        }
        lastNode.link = newNode;
    }
}