Can you explain the algorithm you're trying to implement? That is, what are your steps, in English and/or pseudoode?
And what specific problems are you having with the code you posted? If it's giving a compile error or thrown an exception at runtime, copy/paste the exact, complete error message and indicate clearly which line is the cluprit. If it's just not doing what you want, explain clearly and precisely what you want it to do and what it's doing instead.
Raihan Jamal wrote:
Any suggestions will be appreciated with some code based on my solution..
Suggestions, sure, once you provide enough information. But be aware that this site is NotACodeMill.(⇐click)
Joined: Mar 23, 2010
The problem that I am having is that I am not able to maintain insertion order into lexicographical order. I have added the while loop in my code. But that while loop is not working means I am not getting the accurate result.
That looks a very short implementation of a doubly-linked list.
I think you need to get a piece of paper and a pencil. Draw a doubly-linked list in diagram form. Write down how you intend to add elements, at the ends and in the middle.
Find out about the Comparable<T> and Comparator<T> interfaces. Work out how you can create your list in sorted form with a Comparator<T>, and how you can use a Comparator<String> to sort in reverse order.
Find out why your List will run in quadratic time and you have a very inefficient application.
Not only should you do those things, but also you should do them in the order I wrote them.
In addition to what Campbell said, add print statements to your code so you can see what is happening at each step--which branches are being taken and what various values are. Compare that to how you expect it to behave based on your manual walk through. Where the two diverge, at least one of them is wrong.
Joined: Mar 23, 2010
I have updated the code. But somehow still I am having the same problem. Any suggestions will be appreciated..
Raihan Jamal wrote:Any suggestions will be appreciated..
I suggest you follow the suggestions you've already been given.
Joined: Oct 13, 2005
Raihan Jamal wrote:I have updated the code. . . .
An implementation of a doubly-linked list<E> will take about two pages to print out. You will have methods like add(E), add(int, E), addAtStart(E), addAtEnd(E), remove(E), remove(int), removeFromStart(), removeFromEnd(), get(int), indexOf(E), etc.
You can’t fit all those into the space you have taken.
Implement the plain linked list.
Enhance it will other List interface methods, eg size(), isEmpty().
Then enhance it (possibly as a subtype) with methods like nodeAt(int), insertBefore(Node, E) and insertAfter(Node, E).
Then enhance it with findInsertionPoint(E, Comparator<E>) and findInsertionPoint(E extends Comparable<? super E>). Now you have the insertion point, and you can use the methods you have got to insert there.
Now all you have to do is create a suitable Comparator<E> and you are home and dry.