| Author |
delete () in linkedlist in Java without using API
|
Surya Bavirti
Greenhorn
Joined: Sep 08, 2008
Posts: 13
|
|
Given the following classes, wite the delete() method, which will delete all nodes equal() to Node n. delete() should return true if at least one node could be removed, and return false otherwise. Use Java syntax.Don't use Java API. Here is my code for delete().But i'm unable to get the correct result.What my problem is, if the Node 'n' is existing multiple times,my delete() will not remove all 'n's.It removes only one 'n'.Pla help me. My code for delete() -------------------- ----------------------------// end of delete() Actual Program --------------- [RP]Added code tags, modified indentation[/RP] [ September 25, 2008: Message edited by: Rob Prime ]
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
|
Forget the Java code for a moment. Try to explain the delete algorithm you want to implement in English.
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Surya Bavirti
Greenhorn
Joined: Sep 08, 2008
Posts: 13
|
|
|
The delete() method, which will delete all nodes equal() to Node n. delete() should return true if at least one node could be removed, and return false otherwise. Use Java syntax.Don�t use java API
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
|
I understand what you're supposed to do. How do you propose to do it?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Some comments: LinkedList? As in java.util.LinkedList? You're not supposed to be using that. In fact, you're not. You create it, then ignore it. Just remove this line. You create a new DS instance, then call getStart on that. You probably want to call getStart on the current instance; either use "this.getStart()" or simply "getStart()". You could turn that in a while loop. What if the first three nodes are equal to the node to remove? Again, new DS(). That will 100% surely throw a NullPointerException. You've set n1 to be null, then never reassign it. You need to set it to something somewhere, and also check if it is null - it probably will during the first iteration. You can also just return (var > 0) - that's a boolean all by itself You basically have the right idea, it just needs some tweaking. And one final note: getStart() is surely to return null if the list is empty. What do you do then? Oh, and in the future, please Use Code Tags. I've added them for you this time. [ September 25, 2008: Message edited by: Rob Prime ]
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Surya Bavirti
Greenhorn
Joined: Sep 08, 2008
Posts: 13
|
|
|
Thanks RobPrime.Good Suggestions
|
 |
 |
|
|
subject: delete () in linkedlist in Java without using API
|
|
|