2016-10-16 6 views
0

リンクリストが空白に印刷されています。誰かがここで私が逃していることを説明することができますリンクリスト印刷ブランク

class Linklist { 
private Linklist first; 
public int items; 
public int itemLocation; 
public int lastIndex = -1; 
private final String[] list; 
public Linklist nextlink; 

//Link constructor 
public Linklist(int totalItems) { 
    items = 0; 
    list = new String[totalItems]; 
} 

public Linklist getNext() 
{ 
    return this.nextlink; 
} 

public void setNext(Linklist n) 
{ 
    nextlink = n; 
} 

public void insert (String item){ 
    list[items] = item; 
     items++; 
} 

public void delete(String item){ 
    int location = 0; 

    while(item.compareTo(list[location]) != 0) 
     location++; 

    list[location] = list[items -1]; 
    items--; 
} 

public boolean doesExist (String item){ 
    boolean search; 
    int location = 0; 
    boolean found = false; 

    search = (location < items); 
    while (search && !found) 
    { 
     if (item.compareTo(list[location])==0) 
      found = true; 
     else 
     { 
     location++; 
     search = (location<items); 
     } 
    } 
    return found; 
} 

public void printUnsortedlist(){ 
    System.out.print("{" + list + "} "); 
} 

public void printList(){ 
    Linklist currentLink = first; 
    System.out.print("List: "); 
    while(currentLink != null){ 
     currentLink.printUnsortedlist(); 
     currentLink = currentLink.getNext(); 
    } 
    System.out.println(""); 
} 
} 

public class Unsortedlist{ 
/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) {   
    Linklist list = new Linklist(9); 

    list.insert("Sun"); 
    list.insert("Mercury"); 
    list.insert("Venus"); 
    list.insert("Earth"); 
    list.insert("Mars"); 
    list.insert("Jupiter"); 
    list.insert("Neptune"); 
    list.insert("Saturn"); 
    list.insert("Uranus"); 


    list.printList(); 

    list.delete("Sun"); 

    if(list.doesExist("Earth")) 
     System.out.println("Earth is in the list"); 
    else 
     System.out.println("Earth does not exist!"); 

    list.printList(); 

} 
} 

これは私の出力である:私は例えばsize()方法統合すると

List: 
Earth is in the list 
List: 
BUILD SUCCESSFUL (total time: 0 seconds) 

は:

public int size(){ 
int currentSize = 0; 
Linklist current = head; 
while(current != null){ 
    currentSize = currentSize + 1; 
    current = current.getNext(); 
} 

    return currentSize; 
} 

を私は出力としてこれを取得:

{8} Earth is in the list 

マイリンクされたリストはthしかし、なぜそれが空白であるかを理解することはできません。

+0

'printList'は' first'で始まります。クラスで最初に設定した場所をすべて探し、正しい値に設定されていることを確認します。 – ajb

答えて

0

あなたのコードでは、いくつかの場所が私をもっと混乱させます。

最初に、まだフィールド「リスト」を持っていますが、次のリンクへのポインタも設定します。リストとして印刷する場合は、2つのフィールドString valueLinklist nextしか設定できません。あなたのリストを印刷してください。

第二に、すべてのtime.Toは、多分あなたはfirstthisを変更することができます。この問題を解決するため、私はあなたのリストが空白になっている理由は、あなたがfirstcurrentLinkに設定していることだと思う、と最初はnullである、そして、あなたのwhileコードが実行されますお好きなように。ただ、このような

public void printList(){ 
    Linklist currentLink = this; 
    System.out.print("List: "); 
    while(currentLink != null){ 
     currentLink.printUnsortedlist(); 
     currentLink = currentLink.getNext(); 
    } 
    System.out.println(""); 
} 

しかし、それでもまだ、あなたは多くの場所であなたのコードを改善する必要があるあなたが本当にコードをしたいされ、このヘルプおうち完璧なjob.Hopeを行います。