How do I construct nodes with values in a generic Java LinkedList?

  • Context: Comp Sci 
  • Thread starter Thread starter Live4eva_2
  • Start date Start date
  • Tags Tags
    Class Java
Click For Summary

Discussion Overview

The discussion revolves around constructing nodes with values in a generic Java LinkedList. Participants explore the structure of a linked list, the role of generics, and the process of inserting nodes into the list.

Discussion Character

  • Exploratory, Technical explanation, Homework-related

Main Points Raised

  • One participant expresses confusion about the Node constructor parameters, specifically the use of a LinkedList reference in the context of inserting nodes.
  • Another participant confirms the accuracy of the initial assessment of a linked list and emphasizes the importance of a "root" pointer for the first element and using a temporary variable for subsequent nodes.
  • A later reply indicates a potential understanding of the topic, suggesting progress in comprehension.

Areas of Agreement / Disagreement

Participants do not reach a consensus, as the initial poster seeks clarification while others provide insights based on their experiences. The discussion remains somewhat unresolved regarding the specific implementation details.

Contextual Notes

There is uncertainty regarding the context in which the Node constructor is used, particularly concerning the reference variable for LinkedList. The discussion does not resolve the specifics of node insertion.

Who May Find This Useful

Readers interested in Java programming, particularly those learning about data structures and generics, may find this discussion relevant.

Live4eva_2
Messages
28
Reaction score
0
[SOLVED] Java LinkedListNode class

Hi..

I'm trying to understand a generic linked list...in Java.

I understand that I should have a LinkedList class containing a Node class.
The Node class should have an info variable and a link variable.These variables should be accesessed through the .dot operator.

Now my book has a huge chapter on it,but unfortunately introducing generics and Linked Lists with a million different interfaces isn't much use to a noob.

My biggest problem seems to be in understanding how a node is inserted into the list.
For example the book shows a Node constructor having the following parameters:
(T elem,LinkedList<T> ptr)...I understand that T can be anything except primitive type parameters.But what I don't understand is in what context will this method be used??

Most likely we will be required to squash an array of Integer or String values into a linked list...So assuming that I have this : Integer[] anArray={1},{2},{3},{4}
How would I go about constructing nodes with these values...The constructor has a parameter,LinkedList<T> ptr.Where am I intended to get this reference variable from.
 

Attachments

  • infolink.JPG
    infolink.JPG
    23.3 KB · Views: 504
Last edited:
Physics news on Phys.org
I don't know a thing about Java because I prefer C++'s transparency so I'll just say that your assessment of a linked list is accurate. When you implement a linked list, it's important to create a "root" pointer that points to the first element of your list, then use a temp variable to create subsequent nodes by repeated use of the "new" command.
 
Last edited:
I think I just might get it now...
 
Yay!I do...Marked as solved!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
12K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K