Results 1 to 18 of 18
  1. #1
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default Need help in linked list

    Hello guys

    I need help so quickly

    I need to know how to add a linked list under a node in a linked list

    such as


  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Shouldn't it be as simple as this:

    Java Code:
    List<Airplane> airplanes;
    ...
    public class Airplane {
       private List<Passenger> passengers;
       ...
    }
    ...
    public class Passenger {
       ...
    }
    kind regards,

    Jos

  3. #3
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default

    The problem is that I can insert the airplane list but I cannot insert the list of passengers to link it to an air plane node.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Hotzero View Post
    The problem is that I can insert the airplane list but I cannot insert the list of passengers to link it to an air plane node.
    All I can ask is: why not? in my scenario you'd add a Passenger to an Airplane and you'd add an Airplane (including its Passenger(s)) to your main list.

    kind regards,

    Jos

  5. #5
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default

    Have a look


    public class Airplane {
    public String name;
    public int number;
    public double time;
    public String caName;

    public Airplane next;
    public Passenger refPass;


    public Airplane(String nm, int num, double tm, String cName) {
    name=nm;
    number=num;
    time=tm;
    caName=cName;
    next=null;
    refPass=null;
    }
    public Airplane(String nm, int num, double tm, String cName, Airplane n, Passenger refP) {
    name=nm;
    number=num;
    time=tm;
    caName=cName;
    next=n;
    refPass=refP;
    }

    ********************

    public class Passenger {
    public String frName;
    public String laName;
    public int id;
    public int seatNumber;
    public int lagNumber;

    public Passenger next;

    public Passenger(String fNM, String laNM, int d, int sNum, int lgNum ) {
    frName=fNM;
    laName=laNM;
    id=d;
    seatNumber=sNum;
    lagNumber=lgNum;
    next=null;


    }

    public Passenger(String fNM, String laNM, int d, int sNum, int lgNum, Passenger n ) {
    frName=fNM;
    laName=laNM;
    id=d;
    seatNumber=sNum;
    lagNumber=lgNum;
    next=n;


    }




    }

    *******************

    import java.util.Scanner;


    public class List {

    public Airplane head, tail;
    public Passenger tmpPass;

    Scanner kk=new Scanner(System.in);

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

    public void addToTail(String nm, int num, double tm, String cName)/// Insertion for airplanes and flights.
    {
    // A pointer from an Airplane node to point and the first passenger node.
    if (head==null)
    {
    head=tail=new Airplane(nm, num, tm, cName);
    tmpPass=head.refPass;// Assigning the pointer to the passenger list pointer.
    this.addPass(tmpPass);// to junmb to the passngers list and start adding.
    }

    else
    {
    tail.next=new Airplane(nm, num, tm, cName);
    tail = tail.next;
    tmpPass=tail.refPass;// Assigning the pointer to the passenger list pointer.
    this.addPass(tmpPass);// to junmb to the passngers list and start adding.
    }
    }

    public void addPass(Passenger tmp)// the method that holds insertion for passengers lists
    {
    String firstName;
    String lastName;
    int iD;
    int seatNumber;
    int lagNumber;

    String op;
    do{


    System.out.println("***Passenger Info***");
    System.out.print("First Name:");
    firstName=kk.next();
    System.out.print("Last Name:");
    lastName=kk.next();
    System.out.print("ID:");
    iD=kk.nextInt();
    System.out.print("Seat Number:");
    seatNumber=kk.nextInt();
    System.out.print("Luggage Number:");
    lagNumber=kk.nextInt();
    tmp=new Passenger(firstName, lastName, iD, seatNumber, lagNumber, tmp);// Creating a passenger node.


    System.out.println("Do you want to add more passenger[Yes/No]");
    op=kk.next();
    }while(!op.equalsIgnoreCase("No"));

    }

    public boolean searchFlight(int nm)
    {

    Airplane tmp;

    for(tmp=head; tmp!=null &&tmp.number!=nm; tmp=tmp.next);
    this.printFlighrSInfo(tmp);




    return tmp!=null;
    }

    public void printFlighrSInfo(Airplane tmp)
    {
    System.out.println("Found");
    System.out.println("Flight Info");
    System.out.println("Airplane Type:"+tmp.name);

    System.out.println("Flight Number:"+tmp.number);

    System.out.println("Time:"+tmp.time);

    System.out.println("Captin Name:"+tmp.caName);

    int n=1;
    Passenger tmpPass;
    tmpPass=tmp.refPass;
    if(tmpPass==null)
    System.out.println("ha");
    else
    {


    for(; tmpPass!=null; tmpPass=tmpPass.next, n++)
    {



    System.out.println("Passenger"+n+" Info");
    System.out.println("First Name:"+tmpPass.frName);
    System.out.println("Last Name:"+tmpPass.laName);
    System.out.println("ID:"+tmpPass.id);
    System.out.println("Seat Number:"+tmpPass.seatNumber);
    System.out.println("Luggage Number:"+tmpPass.lagNumber);}


    }}}

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    You're trying to do it the hard way; why should a Passenger know about a 'next' passenger? And a Plane about a 'next' Plane? In my scenario a Plane simply has a List<Passenger> and most likely some methods addPassenger( ... ), removePassenger( ... ) etc. There's also a List<AirPlane> that keeps track of all the AirPlanes. Don't you think that's much easier? Above all Lists have already been implemented, so why not use them?

    kind regards,

    Jos

  7. #7
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default

    Dear JosAH

    thanks for your help I appreciate what you are doing alot, but you said
    You're trying to do it the hard way; why should a Passenger know about a 'next' passenger? And a Plane about a 'next' Plane?

    Because I need it in linked list so I can search about a plane or a passenger if exists or not. Also,
    that I can calculate the number of passengers and planes and finding the plane that has the highest load(Luggage).

    That's why my friend JosAH.

    kind regards,

    Hotzero

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Hotzero View Post
    Dear JosAH

    thanks for your help I appreciate what you are doing alot, but you said
    You're trying to do it the hard way; why should a Passenger know about a 'next' passenger? And a Plane about a 'next' Plane?

    Because I need it in linked list so I can search about a plane or a passenger if exists or not. Also,
    that I can calculate the number of passengers and planes and finding the plane that has the highest load(Luggage).
    Have a look again at the classes outlined in my reply #2: a plane has a list of passengers and there is another list that contains planes. In your scenario a passenger is a list node and a plane is a list node. Also read the API documentation for the List interface and its several implementations; they keep you from a lot of detailed work.

    kind regards,

    Jos

  9. #9
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default Your request are not accurate

    I'm having difficulty interpreting your request.
    Here is the code I am under the impression
    you must use.


    Java Code:
    public class Airplane{
      public String name;
      public int number;
      public double time; 
      public String caName;
    
      public Airplane next;
      public Passenger refPass;
    
      public Airplane(String nm, int num, double tm, String cName){
        name=nm;
        number=num;
        time=tm;
        caName=cName;
        next=null;
        refPass=null;
      }
    
      public Airplane(String nm, int num, double tm, String cName,
                      Airplane n, Passenger refP){
        name=nm;
        number=num;
        time=tm;
        caName=cName;
        next=n;
        refPass=refP;
      } 
    
    //********************
    
      public class Passenger{
        public String frName;
        public String laName;
        public int id;
        public int seatNumber;
        public int lagNumber;
    
        public Passenger next;
    
        public Passenger(String fNM, String laNM, int d, int sNum, int lgNum ) {
          frName=fNM;
          laName=laNM;
          id=d;
          seatNumber=sNum;
          lagNumber=lgNum;
          next=null;
        }
    
        public Passenger(String fNM, String laNM, int d, int sNum,
                       int lgNum, Passenger n ) {
          frName=fNM;
          laName=laNM;
          id=d;
          seatNumber=sNum;
          lagNumber=lgNum;
          next=n;
        }
      }
    
    //*******************
    
    import java.util.Scanner;
    
    public class List{
    
      public Airplane head, tail;
      public Passenger tmpPass;
    
      Scanner kk=new Scanner(System.in);
    
      public List(){
        head=tail=null;
      }
    
      public void addToTail(String nm, int num, double tm, String cName){
        // Insertion for airplanes and flights.
        // A pointer from an Airplane node to point and the first passenger node.
        if (head==null){
          head=tail=new Airplane(nm, num, tm, cName);
          tmpPass=head.refPass;// Assigning the pointer to the passenger list pointer.
          this.addPass(tmpPass);// to junmb to the passngers list and start adding.
        }
        else{
          tail.next=new Airplane(nm, num, tm, cName);
          tail = tail.next;
          tmpPass=tail.refPass;// Assigning the pointer to the passenger list pointer.
          this.addPass(tmpPass);// to junmb to the passngers list and start adding.
        } 
      }
    
      public void addPass(Passenger tmp){
        // the method that holds insertion for passengers lists 
        String firstName;
        String lastName;
        int iD;
        int seatNumber;
        int lagNumber;
    
        String op;
        do{
          System.out.println("***Passenger Info***");
          System.out.print("First Name:");
          firstName=kk.next();
          System.out.print("Last Name:"); 
          lastName=kk.next();
          System.out.print("ID:");
          iD=kk.nextInt();
          System.out.print("Seat Number:");
          seatNumber=kk.nextInt();
          System.out.print("Luggage Number:");
          lagNumber=kk.nextInt();
          tmp=new Passenger(firstName, lastName, iD, seatNumber, lagNumber, tmp);
    
          // Creating a passenger node.
          System.out.println("Do you want to add more passenger[Yes/No]");
          op=kk.next();
        }while(!op.equalsIgnoreCase("No"));
    
      }
    
      public boolean searchFlight(int nm){
    
        Airplane tmp;
    
        for(tmp=head; tmp!=null &&tmp.number!=nm; tmp=tmp.next);
          this.printFlighrSInfo(tmp);
     
        return tmp!=null;
      } 
    
      public void printFlighrSInfo(Airplane tmp){
        System.out.println("Found");
        System.out.println("Flight Info");
        System.out.println("Airplane Type:"+tmp.name);
    
        System.out.println("Flight Number:"+tmp.number);
    
        System.out.println("Time:"+tmp.time);
    
        System.out.println("Captin Name:"+tmp.caName);
    
        int n=1;
        Passenger tmpPass;
        tmpPass=tmp.refPass;
        if(tmpPass==null) System.out.println("ha");
        else{
          for(; tmpPass!=null; tmpPass=tmpPass.next, n++){
            System.out.println("Passenger"+n+" Info");
            System.out.println("First Name:"+tmpPass.frName);
            System.out.println("Last Name:"+tmpPass.laName);
            System.out.println("ID:"+tmpPass.id);
            System.out.println("Seat Number:"+tmpPass.seatNumber);
            System.out.println("Luggage Number:"+tmpPass.lagNumber);} 
          }
        }
      }

    Your first request was:
    I need to know how to add a linked list under a node in a linked list.

    Your most recent request are:
    "..so I can search about a plane or a passenger if exists or not."
    "Also, that I can calculate the number of passengers and planes
    and finding the plane that has the highest load(Luggage)."

    I'm assuming you must stick with the code
    you have provided.
    This code is missing some closing braces }},
    which makes it difficult for me to decide if
    some of these classes are inner classes or
    not.

    Please clarify this problem.

    After this is done, I think step 1 would be
    to create a main(String[] args) method,
    probably in another class, so you can
    implement this code to do as you would like.

  10. #10
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default

    I know paul pasciak what you mean

    but the problem of my code is that when I try to print the each airplane and its passengers the airplane info goes right but the passenger info does not appear wgich I think in some way they are lost in the memory and any thing is lost is in the garbage deleted.(I used main and main is nothing compared to the code that would do the function).

    My problem that passengers can not be inserted under the airplane, so any code you suggest whould be good.

    Note:-
    I do not have any provided code. In addition , the code I have posted was created by me and I do not have hints.
    Any code , hints, or directions would be appreciated.

    Thanks guys

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Hotzero View Post
    My problem that passengers can not be inserted under the airplane

    this is ok, because List<> are type safe and will not allow to insert different object types in the same list, unless you use generics.

    but what's about the code in post #2

    Java Code:
    List<Airplane> airplanes;
    ...
    public class Airplane {
       private List<Passenger> passengers;

    this is really an easy way to add Passengers to your Airplane.

  12. #12
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default

    Ok but the problem is the insertion method I can not make it work !!!!!!

  13. #13
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Hotzero View Post
    Ok but the problem is the insertion method I can not make it work !!!!!!

    strange, there is probably an error in the insertion method. post your code please.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Hotzero View Post
    Ok but the problem is the insertion method I can not make it work !!!!!!
    Strange how you insist using your own list implementation while the implementations of the standard List interface are all heavily optimized and clearly documented. Please reread my first reply.

    kind regards,

    Jos

  15. #15
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default

    I am sorry but I am compled to do it in my freaking stupid way

    At all thanks guys I reached the dead line

  16. #16
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Forget the deadline learn the programming involved... Write out all the requiremnents of the original program and post them here. Nobody ever made anything without knowing how to use the basics dont run before you can walk. post the requirements of the problem first.

  17. #17
    Hotzero is offline Member
    Join Date
    May 2010
    Location
    Where all your creativity is not appreciated !!
    Posts
    8
    Rep Power
    0

    Default

    Ok if I make it I will post for every one to see it and learn from but I still stuck.

  18. #18
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Do not ever talk about yourself like that, everybody started from someplace and everybody is only as good as the last thing he has learned, one third of programming is practice the second is researching what you need to know but the most important is asking questions of what you do not know. Don't ever be afraid to ask questions what you do not know, one day you will be answering them becaused you asked.

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-27-2010, 12:15 AM
  2. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  3. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •