无法在结束时添加字符串而不是开始(链表)

时间:2017-01-19 17:43:55

标签: java linked-list

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

我正在尝试做的是为链接列表添加end in 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();
     }
}

1 个答案:

答案 0 :(得分:0)

您的“addAtFront()”和“addAtend()”方法具有完全相同的逻辑。 尝试替换

newNode.setNext(tail);

tail.setNext(newNode);

不要忘记检查空值!