如何在每个链接列表的链接列表中输入新的节点集

时间:2017-02-22 06:04:17

标签: java linked-list nodes

我想在每个乘客的链表中输入一组新的节点。 例如:

对于乘客姓名,我输入约翰。

对于国家/地区代码,我输入:BI

对于航班号,我输入:095

对于行李数量,我可以输入任何金额。

假设我输入:John,BI,095,3。

这就是我得到的:[John with baggage(s) [BI0950, BI0951, BI0952]]这就是我想要的。

然后我按'b'进入新乘客。

然后我进入下一位乘客:Jane,BU,096,3。

这就是我得到的:[John with baggage(s) [BI0950, BI0951, BI0952], Jane with baggage(s) [BI0950, BI0951, BI0952, BU0960, BU0961, BU0962]]

每当我点击'b'('b'是添加新乘客)时,如何从baggage链接列表中删除旧节点(BI0950,BI0951,BI0952)?

import java.util.*;

public class baggage_system{

  static LinkedList<String> baggagex = new LinkedList<String>();  

  public static String getUser_command(){
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter command B-baggage, n-next, q-quit");
    String s = keyboard.nextLine();
    return s;
  }

  public static String getUser_flight(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter the flight number");
     String s = keyboard.nextLine();
     return s;
  }

  public static String getPassenger(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter passenger name");
     String s = keyboard.nextLine();
     return s;
  }

   public static String getUser_country(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter the country code");
     String s = keyboard.nextLine();
     return s;
  }

   public static int getUser_number(){
     Scanner keyboard = new Scanner(System.in);
     System.out.println("Please enter number of baggage");
     int s = keyboard.nextInt();
     return s;
  }


   public static String nextbg(ListIterator<String> iteratorbg){   
     return iteratorbg.next();      
   }

   public static LinkedList<String> makeBaggage(String country, String flight, int num){

     baggagex.add(country + flight + num);

     return baggagex;

   }

   public static int count(){
     System.out.println(baggagex.size());
     return baggagex.size();

   }

  public static void main(String args[]) {

    LinkedList<Passenger> passenger = new LinkedList<Passenger>();
    LinkedList<String> baggage = new LinkedList<String>();
    ListIterator<String> iteratorbg = baggage.listIterator();

      LinkedList<String> counting = new LinkedList<String>();

    String command = "";

    while (!command.equals("q")){
      command = getUser_command();

     if(command.equals("B") || command.equals("b")){
       String p = "";
       p = getPassenger();
       passenger.add(new Passenger(p));


       String country = "";
       country = getUser_country();

       String flight = "";
       flight = getUser_flight();

       int amount = 0;
       amount = getUser_number();

      String[] bg = new String[amount];



      for(int i = 0; i < amount; i++){
  //      LinkedList<String> bgg = new LinkedList<String>();

        baggage = makeBaggage(country, flight, i);

        System.out.println(baggage);

       }
      LinkedList<String> bgg = new LinkedList<String>(baggage);



      passenger.getLast().setBaggages(bgg);


      System.out.println(passenger);

/*      if(baggage.size() != 0 && baggage.size() > baggage.size() - 1){
        for(int j = 0; j < bgg.size(); j++){
          baggage.remove(j);
        } 
      */
//      System.out.println(bgg);



/*       while (!baggage.isEmpty()) {
        baggage.removeFirst();
    }
       */

     } else if(command.equals("n")){
       count();
     } else if (command.equals("p")){
       System.out.println(baggage.peekFirst());
     }

     else
       System.out.println("Enter 'q' to end the program");

     }
  }

public static class Passenger {

    String passengers;    
    List<String> baggage;

    public Passenger(String passengers) {
        this.passengers = passengers;
        baggage = null;
    }

    public void setBaggages(List<String> baggages) {   
        this.baggage = baggages;
    }

    public void pBags(){

    }

    @Override
    public String toString() {      

        return passengers + " with baggage(s) " + baggage;

    }
  }
}

1 个答案:

答案 0 :(得分:1)

问题在于您的编码逻辑,您为所有乘客使用相同的链表行李对象。

static LinkedList<String> baggagex = new LinkedList<String>();  

如果您在此LinkedList中添加或删除任何对象,它将影响所有乘客。

解决方案: 为每位乘客使用新的链表对象。如下代码:

public static LinkedList<String> makeBaggage(String country, String flight, int num)
{
     LinkedList baggagex = new LinkedList();
     baggagex.add(country + flight + num);

     return baggagex;
}