Results 1 to 12 of 12
- 01-24-2012, 10:44 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 18
- Rep Power
- 0
Insert into Doubly LinkedList in reverse lexicographical order
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.
Java Code:theList.insertReverseLexicographicalOrder("B"); theList.insertReverseLexicographicalOrder("A"); theList.insertReverseLexicographicalOrder("H"); theList.insertReverseLexicographicalOrder("D"); theList.insertReverseLexicographicalOrder("E"); public void insertReverseLexicographicalOrder(String dd) { Link newLink = new Link(dd); if (isEmpty()){ last = newLink; }else{ Link focus = first; // This part is right or not? while(focus.next != null && dd.compareToIgnoreCase(focus.next.iData) > 0) { focus = focus.next; } //What to do now as I am not able to understand... Any help will be appreciated.. first.previous = newLink; } newLink.next = first; first = newLink; } class Link { public String iData; public Link next; public Link previous; public Link(String id) { iData = id; } public String toString() { return "{" + iData + "} "; } } private Link first; private Link last; public DoublyLinkedList() { first = null; last = null; }
- 01-24-2012, 11:04 PM #2
Re: Insert into Doubly LinkedList in reverse lexicographical order
problem that I am having with my code is that it is not working with that while loop
Try debugging the code by adding some printlns to show the values of the variables that are being used in the while loop. The print out should help you understand what the code is doing.
- 01-24-2012, 11:09 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 18
- Rep Power
- 0
Re: Insert into Doubly LinkedList in reverse lexicographical order
Thanks for replying back. As I am not sure whether my code is working fine. Since the logic that I am using with that it is not working fine. My questions is I want to insert node into doubly linked list such that-
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 question..
- 01-24-2012, 11:12 PM #4
Re: Insert into Doubly LinkedList in reverse lexicographical order
I think the answer to your question is: Yes it can be done.
What is a "library"?
- 01-24-2012, 11:15 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 18
- Rep Power
- 0
Re: Insert into Doubly LinkedList in reverse lexicographical order
By Library I mean without using any sort kind of thing.. Any solution with my code will really help me a lot.. Thanks
- 01-24-2012, 11:26 PM #6
Re: Insert into Doubly LinkedList in reverse lexicographical order
Have you used a piece of paper and pencil to work out the logic for traversing the list and finding the insert point and then doing the insert? I have always had to do it that way.
For debugging I use lots of printlns to show the values of the variables as the code executes. The output will show you what your code is doing.
- 01-24-2012, 11:29 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 18
- Rep Power
- 0
Re: Insert into Doubly LinkedList in reverse lexicographical order
Yeah I did that. But I am not able to know from where should I start. If you can help me in that. It will be of great help to me.. So by that way I will be able to understand whole thing where I am lacking.
- 01-24-2012, 11:31 PM #8
Re: Insert into Doubly LinkedList in reverse lexicographical order
where should I start.
Be sure to add an id String to each print out so you can tell where it was printed.
Execute the code and copy the print out here with your questions about why the code is doing what it is doing.
- 01-25-2012, 01:24 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 18
- Rep Power
- 0
Re: Insert into Doubly LinkedList in reverse lexicographical order
I have updated the whole code. But still I am having some problem. Any suggestions will be appreciated..
**Updated Code:-**
Java Code:class DoublyLinkedList { private Link root; private Link first; public DoublyLinkedList() { root = null; first = null; } public boolean isEmpty() { return root == null; } public void insertReverseLexicographicalOrder(String dd) { Link newLink = new Link(dd); Link focus = first; if (isEmpty()){ root = newLink; newLink.previous = null; newLink.next = null; }else{ while(focus.next != null && dd.compareToIgnoreCase(focus.next.iData) > 0) focus = focus.next; newLink.next = focus; focus.previous = newLink; } first = newLink; }
- 01-25-2012, 01:30 AM #10
Re: Insert into Doubly LinkedList in reverse lexicographical order
I don't see any printlns. How are you debugging the code?
- 01-25-2012, 01:41 AM #11
Member
- Join Date
- Mar 2010
- Posts
- 18
- Rep Power
- 0
Re: Insert into Doubly LinkedList in reverse lexicographical order
I am debugging the code using eclipse debugger. I just wanted to see why my logic is not working correctly..
- 01-25-2012, 01:43 AM #12
Similar Threads
-
Doubly-Linked list
By swp in forum New To JavaReplies: 3Last Post: 10-11-2011, 03:27 PM -
Doubly linked list, reverse()
By hamed in forum New To JavaReplies: 4Last Post: 10-29-2010, 09:53 AM -
Math.random in array and reverse order
By eugenechia in forum New To JavaReplies: 4Last Post: 02-17-2010, 03:33 AM -
doubly linked list insert
By ineedhelpwithjava in forum Advanced JavaReplies: 1Last Post: 03-20-2009, 02:05 PM -
How to insert large data into database using one insert query
By sandeepsai39 in forum New To JavaReplies: 3Last Post: 02-28-2009, 09:17 AM
Bookmarks