Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Help: OrderedLinkedList

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Help: OrderedLinkedList

    Here is my class program. It is an ordered linked list and I am having trouble with 1) the add and 2) find methods, as well as 3) how to use the String.compareToIgnoreCase method to put the nodes in the linked list in order of increasing key value. Please help.
    public class LinkedList<T>{
     
    protected Node<T> head;
    protected int size;
     
    public LinkedList(){
    head = null;
    size = 0;
    }
     
    private class Node<T>{
     
    private String key;
    private Node<T> next;
    private T value;
     
    private Node(String index){
    key = index;
    next = null;
    value = null;
    }
    private Node(String index, T value2) {
    key = index;
    next = null;
    value = value2;
    }
     
    private Node(String index, Node<T> reference, T value2) {
    key = index;
    next = reference;
    value = value2;
    }
    }
     
    //adds new node to the list. k stand for key, and v stands for value.
    //For some reason, it only adds the head and the tail of the linked list. What is in the middle returns null. What should be done so the middle of the list is included?
    public T add(String k, T v){
    if(head == null){
    head = new Node<T>(k, v);
    size++;
    return null;
    }
    Node<T> node;
    for(node = head; node.next != null && node.k.compareToIgnoreCase(k) < 0; node = node.next){
    node.next = new Node<T>(k, v);
    node = node.next;
    size++;
    return node.value;
    }
    while(node != null){
    node.next = new Node<T>(k, v);
    node = node.next;
    size++;
    return node.value;
    }
    return node.value;
    }
    //Method for finding certain nodes.
    //It only finds the head node. Anything else returns null.
    //What should be done so that it can find the rest of the list?
    public T find(String k){
    Node<T> node = new Node<T>(k);
     
    if(!k.equalsIgnoreCase(head.key) && !k.equalsIgnoreCase(node.key)){
    return null;
    }
    else{
    if(k.equalsIgnoreCase(head.key)){
    return head.value;
    }
    else{
    if(k.equalsIgnoreCase(node.key)){
    return node.value;
    }
    else{
    return node.value;
    } 
    }
    }
    }
    Last edited by copeg; February 7th, 2011 at 03:21 PM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Help: OrderedLinkedList

    For future reference, please use the code tags (see the announcements post at the top of every forum for instructions).

    What exactly are you having trouble with? Does this compile? Do you get exceptions? Is there any unexpected behavior?

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help: OrderedLinkedList

    It's supposed to add name and phone number from another class. yes, it compiles, but only registers the head and the tail. what is in the middle becomes null for some reason. according to an expert, the add method is still incomplete, but won't go any further than that. I also need the find method to register other nodes. it only recognizes the head node.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,264
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Help: OrderedLinkedList

    First focus on the add function, and step through it as though you were the computer adding nodes. Think about how the list works: when you insert elements, what pointers need to be set? A simple example: a list of 1 - 3 - 4 : where 1 points to 3, 3 to 4, and so on...you want to insert 2, what pointers need to be set to insert 2 between 1 and 3?. Draw it out if you have to.

  5. The Following User Says Thank You to copeg For This Useful Post:

    vk999 (February 7th, 2011)

  6. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help: OrderedLinkedList

    Would you mind showing me an example in code?

  7. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help: OrderedLinkedList

    I'm not very good at putting algorithm to code. Could anyone show an example of how the methods should look like, or closely resemble them?

Tags for this Thread