aspose file tools*
The moose likes Beginning Java and the fly likes Guidance required on LinkedLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Guidance required on LinkedLists" Watch "Guidance required on LinkedLists" New topic
Author

Guidance required on LinkedLists

Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17
The program I'm required to do is to make a LinkedList. Manually.

The easiest way to do this is just to make a node since they can be referenced to each other.

The problem is that I have no idea how to start the head or even instantiate a node (I think). Yes, I've stared at the java API like a boss. I'm stuck...

Another issue is that after declaring it, I'm unable to stick an object into the node.... Help is greatly appreciated.
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 294
    
    2

Tim Chaung wrote:The program I'm required to do is to make a LinkedList. Manually.

The easiest way to do this is just to make a node since they can be referenced to each other.

The problem is that I have no idea how to start the head or even instantiate a node (I think). Yes, I've stared at the java API like a boss. I'm stuck...

Another issue is that after declaring it, I'm unable to stick an object into the node.... Help is greatly appreciated.
I wonder how to give you hint without doing the work for you.

Let's say you one a one-way linked list.
How about a class called Node that looks something like



the head is
Node head;

Hope that helps.

Joe


It's not what your program can do, it's what your users do with the program.
Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17
Thanks, I'll make some progress with this, and I don't like it when other people do the work for me since it makes me feel bad, and it also doesn't let me learn anything.
Satyen Singh
Greenhorn

Joined: Oct 05, 2011
Posts: 21

hhmmmmmm
Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17
I feel like I'm doing something stupid.... but I'm unable to assign a Node an object..... what am I doing wrong?

I could use a MapSet but.... isn't that not the point of assignment? There isn't any rules against it.... or is there another way around this?
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Based on the outline already provided, assigning a data Object to a Node is done when the Node instance is created:


Show us what you've tried to do, and we may be able to give hints.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36494
    
  16
You may find similar examples in the books; for example the Deitel and Deitel book has an example of how to create a lniked list. To convert that to a linked list, you simply have to alter the spelling a little
Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17


Would casting it like



be a valid? It would keep it as whatever the object was, except now it's a node... so it can be referenced to another?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36494
    
  16
Don’t start variable names with _
Where are you getting that error from? Are you using an IDE or similar? The Node constructor takes an Object, which you are applying to the data field. In the set method you would need to pass a Node, which replaces the node, probably the next reference. You do have to get the next.next reference (as long as next is not null), and apply that to the new Node’s next reference.
If you are only passing the data parameter to the set method, you can use that to create a new Node and insert that into the linked list. That cast would almost certainly fail and cause an Exception.
Why are you declaring that method as returning Object? I would probably have made it void myself. An alternative is boolean; you return true or false depending on whether you inserted successfully. Returning null from anything is hazardous, because you can release incorrect null references into your application.

Well, I know what I mean, but I suspect I am not explaining it well. Anybody else?
Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17
Not really related but _ shouldn't matter. It's a character right? o.O

And I'm not really allowed to change the method... It was an interface I was forced to implement.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7056
    
  16

Tim Chaung wrote:And I'm not really allowed to change the method... It was an interface I was forced to implement.

Could you supply that (if it's not too huge)? It might help.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17
I realized why Node didn't work.... I also have to make a Node class. Manually. My brain is fried....

My current attempt at making a Node class involves making a constructor, giving it an object.... However I'm unsure if this is a right path.... Trial and Error! The way of most programmers!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36494
    
  16
Tim Chaung wrote:. . . . I also have to make a Node class. Manually. . . .
But you supplied a Node class, so I thought you already had made it.
Trial and Error! The way of most programmers!
No, no, no, no, no. You must think what you are doing and plan and design it. If you work by “trial and error”, you will achieve “error”.
Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17
@ Ritchie

What I meant as in, I plan it out, it works in theory. You write it, but you never really know if it works until you test it.

Well, at least from my view, I'm sure with practice I'll pass the "never really know" part...

On a side note, I've made progress on making my nodes class. I'm now stuck on writing my own "Next()" method to reference objects/nodes to each other.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Tim Chaung wrote:Not really related but _ shouldn't matter. It's a character right? o.O

And I'm not really allowed to change the method... It was an interface I was forced to implement.


It's difficult to read. So you have an interface you're implementing, how about sharing that information?
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 294
    
    2

Dennis Deems wrote:It's difficult to read. So you have an interface you're implementing, how about sharing that information?


The leading underscore is by convention reserved for lower level language functions.

Following capitalization and naming conventions makes code easier to read and more consistent.

Not following them (...well I deleted the not so nice comments...) is kind of a refusal to play nice with others.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3371
    
    9
Exactly, and Java conventions do not include underscores for "low level language functions". Always start a variable with a lowercase letter.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7056
    
  16

Joe Areeda wrote:The leading underscore is by convention reserved for lower level language functions...

Never heard of that one, but it seems like a reasonable use (if documented).

As far as OP's use is concerned, I've actually seen it in quite a lot of Perl scripts and Python programs, where I believe (but definitely not sure, since I've never used it myself) that it's used to indicate a local variable.

Winston
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 294
    
    2

Winston Gutkowski wrote:
Joe Areeda wrote:The leading underscore is by convention reserved for lower level language functions...

Never heard of that one, but it seems like a reasonable use (if documented).

As far as OP's use is concerned, I've actually seen it in quite a lot of Perl scripts and Python programs, where I believe (but definitely not sure, since I've never used it myself) that it's used to indicate a local variable.

Winston
I went looking for guidance on leading underscores and couldn't find it. I may be thinking about C++ libraries or I may have just made that up out of thin air. Sorry.

Here is the Oracle document on variable naming conventions http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

Joe
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3371
    
    9
In C, identifiers used in Assembly code have a leading underscore. This is probably what you're referring to.
Tim Chaung
Greenhorn

Joined: Sep 19, 2011
Posts: 17
I know it is in C/C++... and this naming style was recommended to me by my prof....

I'm chugging along nicely now.... I'll drop the interface here after the due date though.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Guidance required on LinkedLists
 
Similar Threads
dynamic instantiation of objects
Appending one node element from first xml file to the second xml file using ant
Parsing a TreeModel structure
New node in JTree not visible
Insert numbers in a binary search tree