Sagewire Logo

Newbie question re linked list

17 Message(s) by 7 Author(s) originally posted in java help


From: shellriley Date:   Monday, October 22, 2007
I have created a few method s that will allow me to add a Student
object to a linked list of Student objects. It compiles OK and is
running, but when I enter more than one student record the new record
creates the additional node , but overwrites the data in all the
previous nodes, so I get multiple nodes of the same data. Is it
obvious where I have gone wrong?

Michelle

in my driver class :

public static void add_student() throws IOException{
String course_id = offerings.promptCourse();
int index = offerings.searchCourses(course_id);
Student newStudent = new Student();
newStudent.promptRecord();

offerings.course_array[index].courseEnrollment.add_Record(newStudent);

offerings.course_array[index].courseEnrollment.displayList();
}

in a class on its own:

public class Enrollment {
private Student head;
public Enrollment() {
head = null;
}

public void add_Record(Student newStudent) {
newStudent.next = head;
head = newStudent;
}

public void displayList() {
Student current = head;

while (current != null) {
current.display_student();
System.out.println("\n");
current = current.next;
}
}


From: Roedy Green Date:   Monday, October 22, 2007
wrote in message,
quoted or indirectly quoted someone who said :

in my driver class:



see http://mindprod.com/jgloss/sscce.html

Always post your complete code . Nearly always the problem is in the
part you do not post.
--
Roedy Green Canadian Mind Products
The JAVA Glossary
http://mindprod.com


From: Roedy Green Date:   Monday, October 22, 2007
wrote in message,
quoted or indirectly quoted someone who said :

I have created a few methods that will allow me to add a Student
object to a linked list of Student objects. It compiles OK and is
running, but when I enter more than one student record the new record
creates the additional node, but overwrites the data in all the
previous nodes, so I get multiple nodes of the same data. Is it
obvious where I have gone wrong?



A fellow student posted this exact same problem a short while ago. The
answer turned out to be she had only one Student object that she kept
modifying and re-adding. She needed to do a "new" for each Student.
--
Roedy Green Canadian Mind Products
The JAVA Glossary
http://mindprod.com


From: Patricia Shanahan Date:   Monday, October 22, 2007
wrote in message:
I have created a few methods that will allow me to add a Student
object to a linked list of Student objects. It compiles OK and is
running, but when I enter more than one student record the new record
creates the additional node, but overwrites the data in all the
previous nodes, so I get multiple nodes of the same data. Is it
obvious where I have gone wrong?
...



I could not work out exactly what is wrong from the snippets of code, but
almost always the symptom you describe is due to adding the same object
multiple times, rather than adding several different objects.

Patricia


From: shellriley Date:   Monday, October 22, 2007
On Oct 22, 3:02 pm, Roedy Green <see_webs...@xxxxxxxxxxx>
wrote in message:
wrote in message,
quoted or indirectly quoted someone who said :
> in my driver class:
seehttp://mindprod.com/jgloss/sscce.html
Always post your complete code. Nearly always the problem is in the
part you do not post.
--
Roedy Green Canadian Mind Products
The JAVA Glossaryhttp://mindprod.com



Thanks for that tip. Sorry, I did not include it all because there are
about 7 or 8 different class files.


From: Patricia Shanahan Date:   Monday, October 22, 2007
wrote in message:
On Oct 22, 3:02 pm, Roedy Green <see_webs...@xxxxxxxxxxx>
wrote in message:
wrote in message,
quoted or indirectly quoted someone who said :

in my driver class:
seehttp://mindprod.com/jgloss/sscce.html

Always post your complete code. Nearly always the problem is in the
part you do not post.
--
Roedy Green Canadian Mind Products
The JAVA Glossaryhttp://mindprod.com
Thanks for that tip. Sorry, I did not include it all because there are
about 7 or 8 different class files.



If the hints you've been given don't solve the problem, you should
simplify it down to one or two classes with everything irrelevant to the
problem deleted.

Patricia


From: shellriley Date:   Monday, October 22, 2007
On Oct 22, 3:04 pm, Roedy Green <see_webs...@xxxxxxxxxxx>
wrote in message:
wrote in message,
quoted or indirectly quoted someone who said :
> I have created a few methods that will allow me to add a Student
>object to a linked list of Student objects. It compiles OK and is
>running, but when I enter more than one student record the new record
>creates the additional node, but overwrites the data in all the
>previous nodes, so I get multiple nodes of the same data. Is it
>obvious where I have gone wrong?
A fellow student posted this exact same problem a short while ago. The
answer turned out to be she had only one Student object that she kept
modifying and re-adding. She needed to do a "new" for each Student.
--
Roedy Green Canadian Mind Products
The JAVA Glossaryhttp://mindprod.com



Thanks again. I did a search for linked list questions before I asked
the question but there were 791 to sort through! I must have missed
that one or used the wrong search criteria.

I do have a new Student being created everytime the add_student method
is called in the driver class. At the start of the program, the user
is given a menu with option s and the first option calls the
add_Student method. Also, I am actually getting new Student nodes, but
it is just the same data.

Michelle


From: Lew Date:   Monday, October 22, 2007
wrote in message:
I do have a new Student being created everytime the add_student method
is called in the driver class. At the start of the program, the user
is given a menu with options and the first option calls the
add_Student method. Also, I am actually getting new Student nodes, but
it is just the same data.



Until you post an SSCCE
<http://www.physci.org/codes/sscce.html>
we are unlikely to be able to help you. Textual paraphrases only vaguely
indicate where general problems can often reside; they do not point to what is
going wrong for you in this specific case.

People keep mentioning this because it is the only way to reasonably expect
results.

--
Lew
"If one person calls you a horse, they're a kook.
If two people call you a horse, it's a prank.
If three people call you a horse, it's time to buy a saddle."


From: Mark Space Date:   Monday, October 22, 2007
wrote in message:

public void add_Record(Student newStudent) {
newStudent.next = head;
head = newStudent;
}



I'm pretty sure this is buggered up.


From: Mark Space Date:   Monday, October 22, 2007
wrote in message:
wrote in message:
public void add_Record(Student newStudent) {
newStudent.next = head;
head = newStudent;
}
I'm pretty sure this is buggered up.Oops! My mistake, I read this wrong. You are fine here.





From: RedGrittyBrick Date:   Tuesday, October 23, 2007
wrote in message:
wrote in message:
wrote in message:

public void add_Record(Student newStudent) {
newStudent.next = head;
head = newStudent;
}


I'm pretty sure this is buggered up.
Oops! My mistake, I read this wrong. You are fine here.



Actually, the fact you were misled by the code suggests to me that all
isn't entirely fine and that the code could be clearer.

I must admit, when I think of linked lists (which I have not coded for
decades) I think of lists that grow at the "tail" end. So the
manipulation of the "head" end seemed a bit odd to me. The list coded
above can only be traversed in the reverse of the order in which the
elements were added. This is not necessarily a bad thing but I found it a
little counter intuitive. OTOH maybe thats the way linked lists are
taught nowadays? Maybe I was taught the "wrong" way?

This is, of course, a very minor point.[Later]

Having read http://en.wikipedia.org/wiki/Linked_list I see

function insertBeginning(List list, Node newNode) {
// insert node before current first node
newNode.next := list.firstNode
list.firstNode := newNode
}

The comment ought to be superfluous, I expect many consider it so.

I think the method name "insertBeginning" probably helps set the
reference frame within which the manipulation of the "head" end of the
list is easier to recognise as correct.

Perhaps it's just me?


From: shellriley Date:   Tuesday, October 23, 2007
wrote in message:
I have created a few methods that will allow me to add a Student
object to a linked list of Student objects. It compiles OK and is
running, but when I enter more than one student record the new record
creates the additional node, but overwrites the data in all the
previous nodes, so I get multiple nodes of the same data. Is it
obvious where I have gone wrong?
Michelle
in my driver class:
public static void add_student() throws IOException{
String course_id = offerings.promptCourse();
int index = offerings.searchCourses(course_id);
Student newStudent = new Student();
newStudent.promptRecord();
offerings.course_array[index].courseEnrollment.add_Record(newStudent);
offerings.course_array[index].courseEnrollment.displayList();
}
in a class on its own:
public class Enrollment {
private Student head;
public Enrollment() {
head = null;
}
public void add_Record(Student newStudent) {
newStudent.next = head;
head = newStudent;
}
public void displayList() {
Student current = head;
while (current != null) {
current.display_student();
System.out.println("\n");
current = current.next;
}
}



Thanks everyone for your input. I am going to work on other aspects of
my assignment and come back to this one. This is always a surefire way
of having an "aha" moment and solving my problem. Thanks again.

Michelle


From: Lew Date:   Tuesday, October 23, 2007
wrote in message:
public class Enrollment {
private Student head;
public Enrollment() {
head = null;



Since head is already null, this assignment has no net effect, except to
potentially waste processor cycles.

}



--
Lew


From: shellriley Date:   Tuesday, October 23, 2007
wrote in message:
wrote in message:
public class Enrollment {
private Student head;
public Enrollment() {
head = null;
Since head is already null, this assignment has no net effect, except to
potentially waste processor cycles.
}
--
Lew



Thank you for highlighting that. I'll get rid of the assignment. The
good news is that I figured out where I went wrong. In my Student
class, I had made all my variables static, which I understand makes
them available to all instances of the class--is that right? I am
still a bit fuzzy on what static is. I got rid of the static bit and
in my promptRecord method, I have now passed in a reference to a newly
created Student and have used the dot operator to specify which the
changes to the variables of said Student. It seems to be working fine
now. Thanks everybody for trying to help!

public void promptRecord(Student newStudent) throws IOException {
BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in));
Scanner keyboard = new Scanner(System.in);
System.out.println("\tStudent Details");
System.out.println("\t===============\n");
System.out.println("Student Name: ");
newStudent.student_name = stdin.readLine();
System.out.println("Address: ");
newStudent.address = stdin.readLine();
System.out.println("Age: ");
newStudent.age = keyboard.nextInt();
System.out.println("Outstanding Fees: ");
newStudent.fees_owed = keyboard.nextDouble();
System.out.println("Previous Student? True or False: ");
newStudent.prev_student = keyboard.nextBoolean();
System.out.println("Senior Student? True or False: ");
newStudent.sen_student = keyboard.nextBoolean();
System.out.println("=========================\n");
}


From: Andrew Thompson Date:   Tuesday, October 23, 2007
wrote in message:
..
Thank you for highlighting that. I'll get rid of the assignment.



What do you mean by 'get rid of'? 'Finish and submit',
or more 'throw away/discard'?

...The
good news is that I figured out where I went wrong. In my Student
class, I had made all my variables static, ..



I note the only mention of static in this thread, lies in the
method declarations. "Nearly always the problem is in the
part you do not post." Roedy, earlier.

..which I understand makes
them available to all instances of the class--is that right?



Not just available, it means there is *only ever one copy*
of the static attributes in the VM at any time.

A lot of new programmers use static to 'get access' to
class members, when that is very much *not* how to
use it. The static keyword is 'rarely'* required in
production code.

* insert argument about whether the word 'rarely' is
justified "If a static (constant) in an interface that is
implemented in 4 classes that are in turn used
by 100, is it's usage once, 4 times, or a hundred?"

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JAVAKB.com
http://www.JAVAkb.com/Uwe/Forums.aspx/JAVA-setup/200710/1


From: Lew Date:   Wednesday, October 24, 2007
public class Enrollment {
private Student head;

public Enrollment() {
head = null;
}




wrote in message:
Thank you for highlighting that. I'll get rid of the assignment.

wrote in message:
What do you mean by 'get rid of'? 'Finish and submit',
or more 'throw away/discard'?



It's worse than that, really. Since there is only the no-arg (no-throw)
constructor in that class, and it performs only default initialization, there
is not even a need for an explicit constructor.

wrote in message:
The good news is that I figured out where I went wrong.
In my Student class, I had made all my variables static, ..



Roedy actually pegged the problem when he said,
A fellow student posted this exact same problem a short while ago.
The answer turned out to be she had only one Student object ...

wrote in message:
I note the only mention of static in this thread, lies in the
method declarations. "Nearly always the problem is in the
part you do not post." Roedy, earlier.



OP, three times you were told to provide an SSCCE. Advice observed only in
the breach. What was that about?

It seems strange to ask for advice, have the same answer given multiple times,
and yet completely disregard it.

--
Lew


From: shellriley Date:   Wednesday, October 24, 2007
wrote in message:
public class Enrollment {
private Student head;
public Enrollment() {
head = null;
}
wrote in message:
Thank you for highlighting that. I'll get rid of the assignment.
wrote in message:
> What do you mean by 'get rid of'? 'Finish and submit',
> or more 'throw away/discard'?
It's worse than that, really. Since there is only the no-arg (no-throw)
constructor in that class, and it performs only default initialization, there
is not even a need for an explicit constructor.
wrote in message:
The good news is that I figured out where I went wrong.
In my Student class, I had made all my variables static, ..
Roedy actually pegged the problem when he said,
> A fellow student posted this exact same problem a short while ago.
> The answer turned out to be she had only one Student object ...
wrote in message:
> I note the only mention of static in this thread, lies in the
> method declarations. "Nearly always the problem is in the
> part you do not post." Roedy, earlier.
OP, three times you were told to provide an SSCCE. Advice observed only in
the breach. What was that about?
It seems strange to ask for advice, have the same answer given multiple times,
and yet completely disregard it.
--
Lew



The advice wasn'ted and will be adhered to in the future. Frankly,
the task of extracting all relevant data and then making it work in an
SSCCE was more cumbersome than to persevere with the problem myself.
It has been over 8 years since I did anything in JAVA and to say I'm
rusty is an understatement. Thanks again for everyone's help.



Next Message: Can I convert Weak references to "hard" references ?


Blogs related to Newbie question re linked list

i have a registration usb disk drive that I have formatted as ext2 ...
Having some kind of issue with “getent” — I’ve followed the directions at https://help.ubuntu.com/community/ActiveDirectoryWinbindHowto and can authenticate users from active directory; wbinfo -u provides a full list. ...

My computer is joomla acting strange My xorgconf is setup as it ...
lgc, Every time you install or remove a kernel the command “update-grub” is run, this is important to know as if you edit your menu.list incorrectly by hand your changes might be reverted by update-grub, but more importantly it is ...

line drawings coin database
linked list java code lipper financial services lex iulia ldrps conver lan web ligths of america lighting stores denver little deuce coupe midi life of jesus board game colina low price tvs legalization + prostitution for ...

25 new messages in 8 topics - digest
platform that OCI will use for future releases. So far, still remains >> linked to VC6 ones. > > This isn't really a core Ruby question. This should be asked on > ruby-talk or (if there is one) the One Click Installer mailing list. ...

hi i need website to connect two hosts using SSL just a cert on ...
i’d use a linked list to keep track of active threads, if one thread wants to close, change a node property and make the main thread free the memory. why? multiple concurrent threads? what problem do you think this would solve? ...

I asked this imagemagick question here a few hours ago and am ...
Ven0m`: better yet, what motherboard. biostar tforce 550 motherboard. #list. It wants you to run a filesystem check. Usually at prompt: sudo fsck If you want it to say yes to each question to fix/repair etc use sudo fsck -y ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional