簡體   English   中英

鏈表與迭代器

[英]Linked List With Iterator

大家。 我必須用某些成分做一個三明治,並且需要在鏈表的某些元素之間進行迭代以插入某些澆頭。 我需要在代碼的最后兩個部分提供一些幫助,我必須在雞肉和西紅柿之間插入培根。 由於某些原因,培根出現在鹽和面包2之間的盡頭。 任何幫助將不勝感激。 感謝您的時間。

/*
 Date: 03/27/2016
 Purpose: Demonstrate Use and Knowledge of LinkedList and Iterator. You CAN'T use an index number for inserting elements into
 linked list. You must only use the list iterator. Submit one java file only. 
*/
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListDemo {
    public static void main(String[] args) 
  {

    List<String> myLinkedList = new LinkedList<String>();

    String strOutput="";

    //BUILD THE SANDWICH

    myLinkedList.add("Bread1");
    myLinkedList.add("mustard");
    myLinkedList.add("lettuce");
    myLinkedList.add("chicken");
    myLinkedList.add("tomato");
    myLinkedList.add("Bread2");

    ListIterator<String> lit = myLinkedList.listIterator();

    while(lit.hasNext()) 
    {
      strOutput += (lit.next().toString() + ",") ;
    }
   strOutput +=("You have reached the end of the sandwich.\n");


   //SHOW THE CURRENT SANDWICH IN REVERSE USING "PREVIOUS()" METHOD
    while(lit.hasPrevious())
    {
        strOutput += (lit.previous().toString() + ",");
    }
    strOutput +=("You have reached the end of the sandwich.\n");


    //ADD PICKLES BETWEEN LETTUCE AND CHICKEN
    while(lit.hasNext())
    {
        if(lit.next().toString().equals("lettuce"))
        {
            lit.add("pickles");
            break;
        }
    }

    while(lit.hasPrevious())
    {
        lit.previous();
    }

    while(lit.hasNext()) 
    {
      strOutput += (lit.next().toString() + ",") ;
    }
   strOutput +=("You have reached the end of the sandwich.\n");


   //ADD CHEESE BETWEEN TOMATO AND BREAD2
   while(lit.hasPrevious())
   {
       if(lit.previous().toString().equals("Bread2"))
       {
          lit.add("cheese");
          break;
       }
   }

   while(lit.hasPrevious())
   {
       lit.previous();
   }

   while(lit.hasNext())
   {
       strOutput += (lit.next().toString() + ",");
   }
   strOutput += ("You have the reached the end of the sandwich.\n");


    //ADD SALT BETWEEN CHEESE AND BREAD2
   while(lit.hasPrevious())
   {
       if(lit.previous().toString().equals("Bread2"))
       {
          lit.add("salt");
          break;
       }
   }

   while(lit.hasPrevious())
    {
       lit.previous();
    }

   while(lit.hasNext())
    {
       strOutput += (lit.next().toString() + ",");
    }
    strOutput += ("You have the reached the end of the sandwich.\n");


   //GO BACKWARDS AND INSERT BACON BETWEEN CHICKEN AND TOMATO
    while(lit.hasPrevious())    
    {
        if(lit.previous().toString().equals("chicken"));
        {
            lit.add("bacon");
            break;
        }
    }

    while(lit.hasPrevious())
    {
        lit.previous();
    }

    while(lit.hasNext())
    {
        strOutput += (lit.next().toString() + ",");
    }
    strOutput += ("You have the reached the end of the sandwich.\n");


    //SHOW FINAL SANDWICH IN FORWARD ORDER

    javax.swing.JOptionPane.showMessageDialog(null, strOutput);
    System.exit(0);
  }

}

您有錯字:

    if(lit.previous().toString().equals("chicken")); // <-- semi-colon
    {
        lit.add("bacon");
        break;
    }

編輯:

分號會盡早關閉if語句。 這意味着lit.add("bacon"); 始終運行,無論if語句如何。

暫無
暫無

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

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