• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

linked-list of objects

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

how can i make a linked-list of objects ?

for example i have a class called with variables like color and model
and i want to do a linked-list of multiple cars , how ?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has its own built in LinkedList class. You could use that.
Or, if you are doing this as an exercise in data structures, do you know what the general structure of a linked list is ? If not that is probably a good place to start - there are plenty of references on the web - just google structure of linked list.
Once you know the structure you should be able to design one in Java. Have a go and if you have any problems come back with what you've got and details what the problems are.
 
nizar malaikah
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Java has its own built in LinkedList class. You could use that.
Or, if you are doing this as an exercise in data structures, do you know what the general structure of a linked list is ? If not that is probably a good place to start - there are plenty of references on the web - just google structure of linked list.
Once you know the structure you should be able to design one in Java. Have a go and if you have any problems come back with what you've got and details what the problems are.



i do .

I'm a little confused in how can i make a nodes with objects as data for example ..



now the class Car has all the variables and set\get methods

what if the user wants to do a thousand car, how to write it in main ??



 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nizar malaikah wrote:I'm a little confused in how can i make a nodes with objects as data for example ..



now the class Car has all the variables and set\get methods

what if the user wants to do a thousand car, how to write it in main ??


Lets just start off with one.
What you posted isn't a linked list - its a linked list node i.e. its what your linked list is going to contain. Your linked list class will have a variable that points to the first node in the list.
So why don't you have a go at writing the basic linked list class
 
nizar malaikah
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:

nizar malaikah wrote:I'm a little confused in how can i make a nodes with objects as data for example ..



now the class Car has all the variables and set\get methods

what if the user wants to do a thousand car, how to write it in main ??


Lets just start off with one.
What you posted isn't a linked list - its a linked list node i.e. its what your linked list is going to contain. Your linked list class will have a variable that points to the first node in the list.
So why don't you have a go at writing the basic linked list class




you mean like this



??
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
Your linked list object does not have any references to its elements. It has references to the nodes which contain the references. Yoiu may prefer to create the nodes as instances of a private inner class in the linked list.
 
nizar malaikah
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No.
Your linked list object does not have any references to its elements. It has references to the nodes which contain the references. Yoiu may prefer to create the nodes as instances of a private inner class in the linked list.



I'm sorry i don't understand , can you explain some more please
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that was a simple explanation. Maybe somebody else can explain it better. Have you searched for articles about linked lists?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key to linked lists is they are normal Classes with an extra field, usually called next, that is a reference of the same type as the class. If next is null, you're at the end of the list. If next has a reference, that is the next "link" in the list.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is a good description of a linked list node. A linked list is made of any number of nodes. A singly‑linked list has a reference to the first node (or null if it is empty).
 
And inside of my fortune cookie was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic