aspose file tools*
The moose likes Java in General and the fly likes Insert into Doubly LinkedList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Insert into Doubly LinkedList" Watch "Insert into Doubly LinkedList" New topic
Author

Insert into Doubly LinkedList

Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
I am tring to insert string into doubly linked list in reverse lexicographical order. But I am not sure how can I maintain the insertion order in reverse lexicographical order.

Meaning- **You can add elements to a linked list. When you add a new string to the list it is maintained in reverse lexicographical order without using any library.**

This is my below code.



The problem that I am having with my code is that it is not working with that while loop that I have added in my code.. Any suggestions will be appreciated with some code based on my solution..


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You gotta TellTheDetails.(⇐click)

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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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)
Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Raihan Jamal
Ranch Hand

Joined: Mar 23, 2010
Posts: 86
I have updated the code. But somehow still I am having the same problem. Any suggestions will be appreciated..

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raihan Jamal wrote:Any suggestions will be appreciated..


I suggest you follow the suggestions you've already been given.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Insert into Doubly LinkedList
 
Similar Threads
Linked list problem
oldskool linked list
Making a quadtree from a Matrix using recursion
Liked List help
Linked List in Java