• Post Reply Bookmark Topic Watch Topic
  • New Topic

linked-list of objects  RSS feed

 
nizar malaikah
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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 ?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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



??
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56595
172
  • Mark post as helpful
  • send pies
  • 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?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 56595
172
  • Mark post as helpful
  • send pies
  • 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).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!