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 8 of 8

Thread: trouble with linked lists

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default trouble with linked lists

    I'm trying to make a dictionary program using linked lists. I need four classes, one that holds the word and the meaning. One that creates the node of information and its link field. Another class that creates and maintains a linked list of words and their meanings. And then a test class.

    so far I have
    public class WordMeaning 
    {
        String theword;
        String def;
        WordMeaning(String t, String d)
        {
            theword = t;
            def = d;
        }
        String getword()
        {
            return theword;
        }
        String getdef()
        {
            return def;
        }
     
     
    }

    public class WordMeaningNode 
    {
     
        WordMeaning word;
        WordMeaning next;
        WordMeaningNode(WordMeaning w)
        {
     
            word = w;
            next = null;
        }
    }

    public class WordList 
    {
        WordMeaningNode list;
        WordList()
        {
            list = null;
        }
     
        void add(WordMeaning a)
        {
            WordMeaningNode temp = new WordMeaningNode(a);
            temp.next = list;
            list = temp;
        }
     
        void add2(WordMeaning a2) //appending to a list
        {
            WordMeaningNode temp = new WordMeaningNode(a2);
            if (listIsEmpty())
            {
                list = temp;
            }
            else
            {
                WordMeaningNode curr = list;
                while(curr.next != null)
                {
                    curr = curr.next;
                }
                curr.next = temp;
            }
        }
     
        boolean listIsEmpty()
        {
            boolean empty;
            if(list == null)
            {
                empty = true;
            }       
            else
            {
                empty = false;
            }
     
     
            return empty;
        }
     
     
     
     
     
    }

    Not sure why i'm getting errors in the list class in the add methods.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trouble with linked lists

    getting errors
    Please copy the full text of the error messages and post it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trouble with linked lists

    temp.next = list;
    curr = curr.next;
    curr.next = temp;
    those lines in the wordlist class gives me errors.

    says incompatible types
    required: WordMeaning
    found: WordMeaningNode

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trouble with linked lists

    says incompatible types
    required: WordMeaning
    found: WordMeaningNode
    The code is using a variable of type: WordMeaningNode where it should use one of type: WordMeaning
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trouble with linked lists

    okay. So, how would be the correct way to go about this. Because i'm using the same code that my professor gave us.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trouble with linked lists

    Did you copy and paste the source, or did you type it in from notes?

    Can you post the full text of the error messages? You've left out some of the information.
    The message should show the source with a ^ under the location of the error.
    Here is a sample from the javac compiler:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trouble with linked lists

    I got it. next should have been declared as WordMeaningNode instead of WordMeaning in wordMeaningNode class. thank you.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trouble with linked lists

    You're welcome.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Linked Lists and Nodes
    By tomlisi92 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 22nd, 2013, 05:07 AM
  2. Need help to understanding with Doubly-linked circular Lists
    By mrdeath9x in forum Java Theory & Questions
    Replies: 3
    Last Post: May 5th, 2011, 03:51 AM
  3. [SOLVED] Linked Lists
    By lieles in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 08:39 AM
  4. Linked Lists
    By Jnoobs in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2010, 04:09 PM
  5. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM

Tags for this Thread