I'm trying to make an update or replace method in my linked list.. I've done add, search and delete methods but now I don't know how to make a method that will replace specific data in a node. I want the user to search for a code and then be able to edit the information of that specific node.
Rebecca Jonson wrote:I'm trying to make an update or replace method in my linked list.. I've done add, search and delete methods but now I don't know how to make a method that will replace specific data in a node. I want the user to search for a code and then be able to edit the information of that specific node.
Well, there are a couple of ways to do it:
1. Remove the old Node, and add a new one with the new values. That will satisfy the "replacement" requirement if order isn't critical (I notice you only have addToHead() and addToTail() methods).
2. Find the Node and replace its contents.
Here is my program...
Tip for you. Either:
1. Forget about all those other fields, and just deal with 'code', because it's the only one that's critical to the behaviour of your list.
2. (and probably much better) Create a class that encapsulates
String name,String department,String code,Object date,Object time,int room (Student? Teacher?)
and create those objects outside your list.
Then your Node only needs to contain two fields: a Student (if that's what it is), and a 'next' pointer - and that will allow you to concentrate on what the LIST is supposed to do, not on setting all those fields.
You'll have to implement a method for the class that compares by it's 'code' field; but that shouldn't be too complicated.
PS: And in answer to your last post: if you implement a separate Student (or whatever) class, you'll be able to write a toString() method for it that displays the contents of one instance any way you like. Do you see how that might help when displaying the contents of the list?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Rebecca Jonson wrote:
I've done this so far but it's not working.. it only prints the last added data
The code is doing exactly what you told it to do. Try to understand why it does what it's doing instead of what you intended for it to do. The bug is mainly because of what you instructed it to do on line 8.
Where does a nanny get ground to air missles? Protect this tiny ad: