Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problems in linked lists

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problems in linked lists

    Hello guys,
    I got a problem in my code which is suposed to work as a linked lists of airplanes and each node in the list(Airplane) has another list beneath it as a nother linked list of passengers, but the problem that the linked list of passengers for each airplane is each time in implemntation is lost

    ***Airplane class***

    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;
        } 
     
     
     
    }
    ***Passengers Calss***

    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;
     
     
        }
     
     
     
     
    }
    ***List and implemntation methods Class***
    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;
        		tmpPass=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=tail.refPass;// Assigning the pointer to the passenger list pointer.
        		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();
        		tmpPass=new Passenger(firstName, lastName, iD, seatNumber, lagNumber, tmpPass);// Creating a passenger node.
     
     
        		System.out.println("Do you want to add more passenger[Yes/No]");
        		op=kk.next();
        	}while(!op.equalsIgnoreCase("No"));
        	}
     
        	else
        	{
        		tail.next=new Airplane(nm,  num,  tm,  cName);
             	tail = tail.next;
             	tmpPass=tail.refPass;// Assigning the pointer to the passenger list pointer.
             	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();
        		tmpPass=new Passenger(firstName, lastName, iD, seatNumber, lagNumber, tmpPass);// 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);
        	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;
     
     
     
    	 		for(; tmpPass!=null; tmpPass=tmpPass.next)
    	 		{
     
     
     
     
        		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);}	
     
     
        	}
     
        	public void printAll()
        	{
        		Airplane tmp;
        		Passenger tmp2;
        		for(tmp=head; tmp!=null; tmp=tmp.next)
        		{
        		for(tmp2=tmp.refPass; tmp2!=null; tmp2=tmp2.next)
        		{
        			System.out.println("Name"+tmp2.frName);
        		}	
        		}
        	}
     
    }
    ***Main Class***
    import java.util.Scanner;
     
    public class Airport {
     
        public static void main(String[] args) {
            Scanner k=new Scanner(System.in);
            List listo=new List();
     
            int option;
     
         String name;
    	 int number;
    	 double time; 
    	 String caName;
    	 do{
    	 	System.out.print("option:");
    	 	option=k.nextInt();
    	 	switch(option)
    	 	{
    	 		case 1:
    	 			System.out.println("***Flight Info***");
    	 			System.out.print("Airplane Type:");
    	 			name=k.next();
    	 			System.out.print("Flight Number:");
    	 			number=k.nextInt();
    	 			System.out.print("Time:");
    	 			time=k.nextDouble();
    	 			System.out.print("Captin Name:");
    	 			caName=k.next();
    	 			listo.addToTail(name, number, time, caName);
    	 			break;
    	 			case 2:
    	 				System.out.print("Search Flight No:");
    	 				number=k.nextInt();
    	 				boolean b=listo.searchFlight(number);
    	 				if(b==true)
    	 				{
     
    	 					listo.searchFlight(number);
     
     
    	 					}
    	 					else
    	 						System.out.println("Not found");
    	 			break;
    	 			case 3:
    	 				listo.printAll();
     
    	 				break;
    	 	}
     
    	 }while(option!=10);
        }
     
     
    }

    Please help urgent

    Tahnks
    Last edited by helloworld922; June 5th, 2010 at 10:57 AM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problems in linked lists

    Please use code tags when posting code to preserve the formatting.

    the problem that the linked list of passengers for each airplane is each time in implemntation is lost
    Where in the code do you detect that the list is "lost"? What does "lost" mean? Does you mean that the code is changing a variable that should point to the list to null or some other value?

    Do some debugging by adding println() statements to show how the values of variables are changing, what the old value was and what the new value will be.

Similar Threads

  1. iterators & linked lists in java question
    By somewhat_confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2010, 10:59 AM
  2. Lists of Sets and Sets of Lists
    By Newoor in forum Collections and Generics
    Replies: 2
    Last Post: December 8th, 2009, 08:13 PM
  3. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM
  4. Simple linked list
    By Koren3 in forum Collections and Generics
    Replies: 10
    Last Post: November 2nd, 2009, 03:33 AM
  5. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM