2017-01-19 6 views
0
public String toString() { 
    Node current = head; 
    Node currentT = tail; 
    if (current != null) { 
    listString = current.getData() + "\n"; 
    while (current.getNext() != null) { 
    current = current.getNext(); 
    listString += current.getData() + "\n"; 
    } 
    return(listString); 
    } else if (currentT != null){ 

     if (currentT != null) { 

      listString = currentT.getData() + "\n"; 
      while (currentT.getNext() != null) { 
      currentT = currentT.getNext() ; 
      listString += currentT.getData() + "\n"; 
      } 
     } 
     }else{ 
      return("There is nothing in the list"); 
     } 
    return listString; 
    } 

私がしようとしているのは、リンクされたリストの最後に追加メソッドを追加することです。私はフォーマットについてお詫びします、私はまだそれのハングアップを得ることができませんでした。ここで、全体のコードであるが、私はバグがstart(リンクされたリスト)の代わりにendで文字列を追加できません。

コード全体から来ている場合、上記示したものであると信じている:

public class LinkedList{ 
private Node head; 
private Node tail; 
static int itemnums = 0; 


public LinkedList() { 
    head = null; 
    tail= null; 
    } 

    public void addAtFront(String str) { 
    Node newNode = new Node(str); 
    newNode.setNext(head); 
    head = newNode; 
    } 
    public void addAtend(String str){ 

      Node newNode = new Node(str); 
      newNode.setNext(tail); 
      tail = newNode; 
    } 



    public void remove(String str) { 
     tail = head; 
     itemnums--; 
    Node current = head; 
    Node previous = head; 
    if (current.getData().equals(str)) { 
    head = current.getNext(); 
    } else { 
    while (current.getNext() != null) { 
    previous = current; 
    current = current.getNext(); 
    if (current.getData().equals(str)) { 
    previous.setNext(current.getNext()); 
    } 
    } 
    } 

    } 

    static String listString; 


    public String toString() { 
    Node current = head; 
Node currentT = tail; 

    if (current != null) { 
    listString = current.getData() + "\n"; 
    while (current.getNext() != null) { 
    current = current.getNext(); 
    listString += current.getData() + "\n"; 
    } 
    return(listString); 
    } else if (currentT != null){ 

     if (currentT != null) { 

      listString = currentT.getData() + "\n"; 
      while (currentT.getNext() != null) { 
      currentT = currentT.getNext() ; 
      listString += currentT.getData() + "\n"; 
      } 

     } 

     }else{ 
      return("There is nothing in the list"); 
     } 
    return listString; 


    } 


    public void size() { 
     Node current = head; 
     Node currentT = tail; 

     if (current != null) { 
     listString = current.getData() + "\n"; 
     itemnums++; 
     while (current.getNext() != null) { 
      itemnums++; 

     current = current.getNext(); 
     listString += current.getData() + "\n"; 
     } 
     } 
     if (currentT != null) { 
      listString = currentT.getData() + "\n"; 
      itemnums++; 
      while (currentT.getNext() != null) { 
       itemnums++; 

      currentT = currentT.getNext(); 
      listString += currentT.getData() + "\n"; 
     } 
     } 
     if (current != null||currentT != null){ 

     System.out.println("There is/are "+(itemnums)+ " item(s) in the list"); 
     }else{ 
      System.out.println("There are no items in list."); 

     } 
     } 



     public void makeEmpty(){ 
      Node current = head; 
      Node currentT = tail; 
      if (current.removeAll()!=(head)) { 
      head = current.getNext(); 

      if (tail!= null){ 
      if (currentT.removeAll()!=(tail)) { 
       tail = currentT.getNext(); 

       System.out.println("data removed"); 
      } 
      }else{ 
       System.out.println("data removed"); 
      } 

      if (current != null||currentT !=null) { 

       itemnums--; 
       itemnums--; 
       itemnums--; 
       while (current.getNext() != null) { 
        itemnums--; 
       } 
      } 
      } 
      } 

    private class Node { 
    private String data; 
    private Node next; 


    public Node(String newData) { 
    data = newData; 
    next = null; 
    } 
    public Object removeAll() { 
     data = null; 
     next = null; 
     return "data removed"; 
    } 


    public void setNext(Node newNode) { 
     next = newNode; 
     } 


    public Node getNext() { 
    return(next); 
    } 

    public String getData() { 
     return(data); 
     } 





} 
} 

出力がで起こるコード:

public class LinkedListDemo { 
    static LinkedList list = new LinkedList(); 

    public static void main(String[] args) { 


    list.addAtFront("Sachar"); 
    list.addAtFront("Osborne"); 
    list.addAtFront("Suess"); 


    System.out.println(list+"\n"); 

    System.out.println("finding number of items \n"); 
    list.size(); 
    System.out.println(); 


    System.out.println("removing all items on the list \n"); 
    list.makeEmpty(); 

    System.out.println(); 
    System.out.println(list+"\n"); 

    System.out.println("Add at end"); 
    list.addAtend("asdfg"); 
    list.addAtend("ASDFG"); 
    list.addAtend("ASG"); 
    System.out.println(list); 
    list.size(); 
    } 
} 
+0

質問を編集して、重要なコードを入力してください。誰もあなたの「壁の壁」を読むつもりはない。 –

答えて

0

あなたの "addAtFront()"と "addAtend()"メソッドはまったく同じロジックを持っています。

tail.setNext(newNode); 

newNode.setNext(tail); 

を交換しようとすると、ヌルをチェックすることを忘れないでください!

関連する問題