• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Guidance required on LinkedLists

 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hhmmmmmm
 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49364
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly, and Java conventions do not include underscores for "low level language functions". Always start a variable with a lowercase letter.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C, identifiers used in Assembly code have a leading underscore. This is probably what you're referring to.
 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic