Results 1 to 18 of 18
Thread: Need help in linked list
- 05-27-2010, 02:13 PM #1
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
- 05-27-2010, 02:16 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Shouldn't it be as simple as this:
kind regards,Java Code:List<Airplane> airplanes; ... public class Airplane { private List<Passenger> passengers; ... } ... public class Passenger { ... }
Jos
- 05-27-2010, 09:15 PM #3
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
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.
- 05-27-2010, 09:28 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 05-29-2010, 09:13 AM #5
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
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);}
}}}
- 05-29-2010, 12:54 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-29-2010, 05:25 PM #7
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
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
- 05-29-2010, 05:30 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-29-2010, 06:25 PM #9
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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.
- 05-30-2010, 06:59 AM #10
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
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
- 05-30-2010, 09:41 AM #11
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.
- 06-01-2010, 11:01 AM #12
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
Ok but the problem is the insertion method I can not make it work !!!!!!
- 06-01-2010, 11:13 AM #13
- 06-01-2010, 11:27 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 06-05-2010, 07:45 AM #15
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
I am sorry but I am compled to do it in my freaking stupid way
At all thanks guys I reached the dead line
- 06-05-2010, 07:50 AM #16
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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.
- 06-05-2010, 08:05 AM #17
Member
- Join Date
- May 2010
- Location
- Where all your creativity is not appreciated !!
- Posts
- 8
- Rep Power
- 0
Ok if I make it I will post for every one to see it and learn from but I still stuck.
- 06-05-2010, 08:13 AM #18
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
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
-
Linked list inside a linked list
By viperlasson in forum New To JavaReplies: 5Last Post: 07-26-2010, 11:15 PM -
Linked list
By rosh72851 in forum New To JavaReplies: 1Last Post: 02-05-2009, 07:21 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks