• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Insert into Doubly LinkedList

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have updated the code. But somehow still I am having the same problem. Any suggestions will be appreciated..

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raihan Jamal wrote:Any suggestions will be appreciated..



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


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic