Anyone know what's wrong with this? (Java linked list).?
-
So, it's a linked list, and this method is supposed to let you insert a new item in the list after another item already in the list of your choosing. Here is the list (before this method is called). Head-> [FLight 3]-> [London]-> [0.30.00]-> [Flight 2]-> [New York]-> [5:00:00]-> [Flight 1]-> [Malaga]-> [2:00:00]-> [Flight 4]-> [Shannon]-> [0.20.00]-> (Ignore the order in which they occur). When I call it and say I want to insert after Flight 1, it ignores it and the list comes out the same, ad if I choose something else it says I've run out of memory. Here is the code: public void insert_after_selected(String f,String de, String d)throws IOException { BufferedReader in; in = new BufferedReader(new InputStreamReader(System.in)); String find; Flight temp=head; System.out.println("Enter item to insert after: "); find=in.readLine(); while(temp!=null) { if(temp.Flight_id.equals(find)) { Flight temp1= new Flight(f,de,d); temp1.next=temp.next; temp.next=temp1; } else temp=temp.next; } } The call from main looks as follows: L1.insert_after_selected("Flight 5","Spain","0:20:00");
-
Answer:
Your while-loop doesn't end if it finds a match because it only advances temp when it doesn't match. So when it finds a match... it leaves temp pointing at the match, inserting a new element after it. Then it loops around, finds a match again, leaves temp pointing at it, and inserts a new element again. Repeat until you run out of memory. I would recommend adding a break statement. When you find the one entry and insert, you are done walking down the list. Something like this: while(temp!=null) { if(temp.Flight_id.equals(find)) { Flight temp1= new Flight(f,de,d); temp1.next=temp.next; temp.next=temp1; break; } temp=temp.next; } Then at the bottom of your loop, if temp is null it means you've fallen off the end of the list and never inserted anything. And if temp is non-null it means you found a place and did the insert. You can print out an error message saying that the specified insert point was not valid. @M
NullPoin... at Yahoo! Answers Visit the source
Related Q & A:
- What's wrong with this PHP Twitter API POST?Best solution by Stack Overflow
- What's wrong with my yahoo 360 page stat counter?Best solution by answers.yahoo.com
- What's wrong with msn hotmail?Best solution by Yahoo! Answers
- What's wrong with my digital camera?Best solution by Yahoo! Answers
- What's wrong with Nokia N97 Mini's wifi?Best solution by wiki.answers.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.