CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: linked lists

  1. #1
    Join Date
    Apr 2009
    Posts
    26

    linked lists

    Give the code to add data to the end of a linked list using the above classes and function declaration as given. Make sure you take care of the special case of an empty list.

    Code:
    class Node{
        int data;
        Node next;
    }
    class LinkedList{
        Node head;
        public void addToEnd(int d){
    // add stuff here
        }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: linked lists

    And your question is?

    Judge a man by his questions, rather than his answers...
    Voltaire
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2009
    Posts
    26

    Re: linked lists

    Quote Originally Posted by dlorde View Post
    And your question is?

    Judge a man by his questions, rather than his answers...
    Voltaire
    "Give the code to add data to the end of a linked list using the above classes and function declaration as given. Make sure you take care of the special case of an empty list."

    That's the question word for word from a practice exam.

  4. #4
    Join Date
    May 2007
    Posts
    811

    Re: linked lists

    And what do you want us to do?

  5. #5
    Join Date
    Apr 2009
    Posts
    26

    Re: linked lists

    Quote Originally Posted by STLDude View Post
    And what do you want us to do?
    ADD DATA TO THE END OF A LINKED LIST using the classes and function declarations given.

    I thought it would to be a simple task for you guys.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: linked lists

    Quote Originally Posted by Backslash View Post
    I thought it would to be a simple task for you guys.
    Why should we do this simple task? what's your point?

    Judge a man by his questions, rather than his answers...
    Voltaire
    Last edited by dlorde; April 19th, 2009 at 03:51 PM.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Apr 2009
    Posts
    26

    Re: linked lists

    Quote Originally Posted by dlorde View Post
    So what's your point?

    Judge a man by his questions, rather than his answers...
    Voltaire
    I'm saying you guys have more knowledge in Java than me because I am learning the language and I just wanted some help.

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: linked lists

    If you want someone to write your exercises or assignments for you, go to a pay site.

    We can help you to do this for yourself, but we're not going to do it for you - what would be the point? The point of the exercise is for you to figure out how to achieve the task.

    So what part of it are you having trouble with, what don't you understand?

    It is better to have an approximate answer to the right question than an exact answer to the wrong one...
    J. Tukey
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  9. #9
    Join Date
    Apr 2009
    Posts
    26

    Re: linked lists

    I've tried it but I don't know if it's correct. Here's what I did:
    Code:
    class Node
    {
        int data;
        Node next;
    }
    class LinkedList
    {
        Node head;
        public void addToEnd(int d)
        {
            Node newNode = newNode();
                 if(head == null)
                 {
                      newNode.add();
                      if(data.next == null)
                      {
                       newNode.add();
                       }
                  }
        }
    }

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: linked lists

    Before asking us, you should make a minimal effort to find out for yourself, because, after all, that is the whole point of the exercise.

    First, make sure you understand exactly what you are being asked to do and how you can achieve it - this should be quite independent of any code. Preferably, write down the task and your solution on paper, in whatever way is clearest to you (diagrams, instruction steps in your own language, etc). When you are quite clear how the solution will work and why, translate it back into Java code. You may feel you're at this point already, but the code you wrote suggests not.

    Once you've written the code, look over it carefully and correct any obvious errors. Next try compiling it and running it. The compiler will tell you about any syntax errors you need to fix. If it works when you run it, you're done. If it doesn't work, look at any error messages and try to figure out what they're telling you (Java error messages usually tell you exactly what is wrong and where).

    If you are able to compile and run the code and it still doesn't do what you want, try stepping through it by hand, with pencil and paper, playing the role of the computer. With small pieces of code like this, you can usually spot logic errors very quickly (a few minutes or less).

    If you still have a problem and you really can't figure it out, ask a question here.

    You may think this is all a waste of time, in which case I have wasted both my time and your time. So be it, but the code you have written suggests you need to do it.

    If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
    Anonymous
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  11. #11
    Join Date
    Apr 2009
    Posts
    26

    Re: linked lists

    Quote Originally Posted by dlorde View Post
    Before asking us, you should make a minimal effort to find out for yourself, because, after all, that is the whole point of the exercise.

    First, make sure you understand exactly what you are being asked to do and how you can achieve it - this should be quite independent of any code. Preferably, write down the task and your solution on paper, in whatever way is clearest to you (diagrams, instruction steps in your own language, etc). When you are quite clear how the solution will work and why, translate it back into Java code. You may feel you're at this point already, but the code you wrote suggests not.

    Once you've written the code, look over it carefully and correct any obvious errors. Next try compiling it and running it. The compiler will tell you about any syntax errors you need to fix. If it works when you run it, you're done. If it doesn't work, look at any error messages and try to figure out what they're telling you (Java error messages usually tell you exactly what is wrong and where).

    If you are able to compile and run the code and it still doesn't do what you want, try stepping through it by hand, with pencil and paper, playing the role of the computer. With small pieces of code like this, you can usually spot logic errors very quickly (a few minutes or less).

    If you still have a problem and you really can't figure it out, ask a question here.

    You may think this is all a waste of time, in which case I have wasted both my time and your time. So be it, but the code you have written suggests you need to do it.

    If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
    Anonymous
    Am I even on the right track or way off?

  12. #12
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: linked lists

    The code you posted is clearly not correct. I don't know what you had in mind, so I can't say if you're "way off", but making an inspired guess is not going to work. At the least, you'll need to write legal Java syntax. However, you can't do this task unless you understand what a linked list is and how it works, so that's where you should start. Then solve the (fairly easy) problem of adding an item to a linked list on paper, before you write any Java code. Once you understand the problem and how to solve it, you should find it much easier to do it in Java.

    If we really understand the problem, the answer will come out of it, because the answer is not separate from the problem...
    J. Krishnamurti
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured