|
-
April 18th, 2009, 11:22 PM
#1
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
}
}
-
April 19th, 2009, 07:08 AM
#2
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.
-
April 19th, 2009, 01:37 PM
#3
Re: linked lists
 Originally Posted by dlorde
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.
-
April 19th, 2009, 02:31 PM
#4
Re: linked lists
And what do you want us to do?
-
April 19th, 2009, 03:27 PM
#5
Re: linked lists
 Originally Posted by STLDude
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.
-
April 19th, 2009, 03:48 PM
#6
Re: linked lists
 Originally Posted by Backslash
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.
-
April 19th, 2009, 03:51 PM
#7
Re: linked lists
 Originally Posted by dlorde
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.
-
April 19th, 2009, 03:55 PM
#8
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.
-
April 19th, 2009, 03:59 PM
#9
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();
}
}
}
}
-
April 19th, 2009, 04:39 PM
#10
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.
-
April 20th, 2009, 06:21 PM
#11
Re: linked lists
 Originally Posted by dlorde
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?
-
April 21st, 2009, 05:53 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|