簡體   English   中英

java中的鏈表教程

[英]linked list tutorial in java

我正在學習Java中的鏈表,並為練習編寫了一些示例代碼。 基本上它是一個單一的鏈表。 代碼工作正常,但它會反轉輸出。 那就是打印出cory,joe和tom,我希望輸出是tom,joe和cory。 湯姆是第一個節點。 我該怎么做或是單個鏈表的工作方式。 那是它總是逆轉輸出?

public class LinkedList {
public String name;
public LinkedList next;
public LinkedList(String name)
{
  this.name = name;
  this.next = null;
}
public String toString()
{
  return name;
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
      Linked l = new Linked();
      l.insert("tom");
      l.insert("joe");
      l.insert("cory");
      l.print();

    }
 }
class Linked
{
LinkedList first;

public Linked()//initialize 
{
    this.first = null;
}

public void insert(String name)
{    
    LinkedList g = new LinkedList(name);
    g.next = first;
    first = g;  
}
 //checks if the list is empty
public boolean isEmpty()
{
    return (first ==null);
}
public void print() //displays the list
 {
    LinkedList t = first;
    while(t!=null)
    {
        System.out.println(t);
        t = t.next;
    }
  }
}

您正在LinkedList的開頭插入。 如果要添加,則在最后一個節點后插入新節點。 你需要一個尾部參考。

public String name;
    public LinkedList next;
    public LinkedList(String name)
    {
      this.name = name;
      this.next = null;
    }
    public String toString()
    {
      return name;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
          Linked l = new Linked();
          l.insert("tom");
          l.insert("joe");
          l.insert("cory");
          l.print();

        }
     }
    class Linked
    {
    LinkedList first;
    LinkedList tail;

    public Linked()//initialize 
    {
        this.first = null;
        this.tail = first;
    }

    public void insert(String name)
    {    
        LinkedList g = new Test(name);
        if(isEmpty())
        {
            first = g;
            tail = first;
        }
        else
        {
            tail.next = g;
            tail = g;
        } 
    }
     //checks if the list is empty
    public boolean isEmpty()
    {
        return (first ==null);
    }
    public void print() //displays the list
     {
        LinkedList t = first;
        while(t!=null)
        {
            System.out.println(t);
            t = t.next;
        }
}

如果您注意到我添加了尾部引用,而不是在開頭插入新對象,我將它附加到LinkedList的末尾。 您可以更改要添加的方法名稱。 實際上你可以有兩種方法保存你的方式...然后添加我的新方法插入但是調用它添加你可以在開始時插入或添加到LinkedList的結尾。

正如@ brso05指出的那樣,你要將值插入頭部而不是尾部。

那是

tom
joe -> tom
cory -> joe -> tom

相反,你應該把它插入尾巴,像這樣

public void insert(String name)
{ 
  if(first==null) 
  {
        LinkedList g = new LinkedList(name);
        g.next = null;
        first = g;  
  } else {

        LinkedList g = new LinkedList(name);

        if (first.next==null) {
          g.next = null;
          first = g;  
          return;
        }

        LinkedList l=first.next;
        for(;l!=null;l=l.next){
         if(l.next==null) {
           l.next = g;
           g.next = null;  
           break;
         }
        }


  }

}

這不是一個很好的解決方案,應該是即興的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM