Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to make an update method in singly linked list?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone;

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.

Here is my program



----------------------------------------------------------------------------------------------------------------



 
Marshal
Posts: 74085
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like everything else on singly‑linked lists, it is easier to visualise if you draw a diagram of what you want to do.
 
Rebecca Jonson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the reply ! I've managed to do it.

However, I'm having another problem.. how can I print all the list using a return type method?

I've done this so far but it's not working.. it only prints the last added data

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
or:
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.

HIH

Winston

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?
 
Marshal
Posts: 16601
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic