Results 1 to 2 of 2
  1. #1
    student2889 is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Sorted Doubly linked List

    I required to create a sorted doubly linked list for my homework. Inside the SortedStringList, I need to implement a method called insert(). I have some problem in doing so. I don't what is going wrong to my code below:



    XML Code:
    public class SortedStringList {
    
       private static class Node {
        public String value;
        public Node previous, next;
    
        public Node(String v, Node p, Node n) {
            value = v;
            previous = p;
            next = n;
        }
    }
    
    private Node first;
    private Node last;
    
    public SortedStringList(){
        this.first = null;
        this.last = null;
    }
    
    public void insert(String s){
        Node newnode = new Node(s,this.first,this.last);
        if(first == null){
            first = newnode;
        }
     else
         if(first.value.compareTo(s) >= 0){
             newnode.next = first;
             newnode.previous = null;
             first = newnode;
             last = first;
         }
     else
         {
            Node current = first;
            newnode.previous = first;
    
            while(current.value.compareTo(s)<=0 ){
                if(current.next == null){
                    current.next = newnode;
                    first = current;
                    last = newnode.next;
    
                }
                first.previous = current;
                current = current.next;
            }
                first.previous.next = newnode;
                newnode.next = current;
                current = first;
     }
    
    
    
    }
    }


    Can somebody please give me some idea?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

Similar Threads

  1. Replies: 12
    Last Post: 10-10-2010, 11:17 PM
  2. Doubly Linked List
    By matin1234 in forum New To Java
    Replies: 0
    Last Post: 06-02-2010, 05:58 AM
  3. doubly linked list insert
    By ineedhelpwithjava in forum Advanced Java
    Replies: 1
    Last Post: 03-20-2009, 02:05 PM
  4. Help with Doubly linked list
    By Dr Gonzo in forum New To Java
    Replies: 5
    Last Post: 12-06-2008, 07:45 AM
  5. Doubly-linked list with data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:30 PM

Posting Permissions

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