Results 1 to 12 of 12
  1. #1
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default 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;
        	}
    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..

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default 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
    Can you explain what happens and what the problem is?

    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.

  3. #3
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default 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..

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default 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"?

  5. #5
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default 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.

  7. #7
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default 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.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default Re: Insert into Doubly LinkedList in reverse lexicographical order

    where should I start.
    Add lots of printlns to show you the values of the variables as the while loop excutes etc
    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.

  9. #9
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default 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;
        	}

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default Re: Insert into Doubly LinkedList in reverse lexicographical order

    I don't see any printlns. How are you debugging the code?

  11. #11
    raihan26 is offline Member
    Join Date
    Mar 2010
    Posts
    18
    Rep Power
    0

    Default 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..

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default Re: Insert into Doubly LinkedList in reverse lexicographical order

    I can't help you when you use the debugger.
    Did you see where the problem is?

Similar Threads

  1. Doubly-Linked list
    By swp in forum New To Java
    Replies: 3
    Last Post: 10-11-2011, 03:27 PM
  2. Doubly linked list, reverse()
    By hamed in forum New To Java
    Replies: 4
    Last Post: 10-29-2010, 09:53 AM
  3. Math.random in array and reverse order
    By eugenechia in forum New To Java
    Replies: 4
    Last Post: 02-17-2010, 03:33 AM
  4. doubly linked list insert
    By ineedhelpwithjava in forum Advanced Java
    Replies: 1
    Last Post: 03-20-2009, 02:05 PM
  5. Replies: 3
    Last Post: 02-28-2009, 09:17 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •