aspose file tools*
The moose likes Beginning Java and the fly likes LinkedList /Workin with a Node class as well Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "LinkedList /Workin with a Node class as well" Watch "LinkedList /Workin with a Node class as well" New topic
Author

LinkedList /Workin with a Node class as well

Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
So I have some project which I guess I am to create a class called BasicLinkedList and another called SortedLinkedList. I need to make a method called getSize() and the method just returns the value of the instance variable you use to keep track of size. But here's the catch....I'm not suppose to traverse the list. Here's a doc of all the methods I need to make if someone wanted to see the actual instructions themselve. http://www.cs.umd.edu/class/spring2013/cmsc132/projects/LinkedListsProject/doc/index.html But also for the addToFront method it says it should return a reference of the current object? This confuses me because its not letting me return head unless the head should be defined within the Node class. Here's my current code



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
When it means current object, it probably does not mean the head node. It probably means its value, your data field. That has a type of T.
Your getSize method will nto do what you want it to.
Your addToTail method does not appear to add the new node to the previous tail node.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Tarrell Fletcher wrote:Here's my current code...

Well, quite apart from anything else, I suspect you're going to have problems testing that:Do you see the problem?

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Tarrell Fletcher wrote:I need to make a method called getSize() and the method just returns the value of the instance variable you use to keep track of size. But here's the catch....I'm not suppose to traverse the list...

OK, so how else do you think you might do it? (Tip: the answer is in your instructions, which as far as I can see you haven't followed - at least not yet).

Winston
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Winston Gutkowski wrote:
Tarrell Fletcher wrote:I need to make a method called getSize() and the method just returns the value of the instance variable you use to keep track of size. But here's the catch....I'm not suppose to traverse the list...

OK, so how else do you think you might do it? (Tip: the answer is in your instructions, which as far as I can see you haven't followed - at least not yet).

Ok so I just reread it, it seems like it is telling me to make an global counter and then I guess increment it whenever something is added to it. And Im just calling that variable to see where the counter is at. And I guess if I do a remove method to make sure I decrement whenever something is deleted?

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Tarrell Fletcher wrote:Ok so I just reread it, it seems like it is telling me to make an global counter and then I guess increment it whenever something is added to it. And Im just calling that variable to see where the counter is at. And I guess if I do a remove method to make sure I decrement whenever something is deleted?

Certainly sounds like a possibility to me.

Winston
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Ok so I did some revisions to the code. I am to use the comparator in my remove method. But so far does this seem correct? I just returned this when for the addToFront and addToEnd methods. I feel as if the hasNext method in the Iterator should be asking if the position != null then return true else false.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
Tarrell Fletcher wrote: . . . I am to use the comparator in my remove method. But so far does this seem correct? . . .
That sounds very peculiar
Apart from your getLast and retrieveLast methods having confusing names, there is some poor style there. Look for a style guide which tells you what to use instead. (Hint: try here.)
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Campbell Ritchie wrote:
Tarrell Fletcher wrote: . . . I am to use the comparator in my remove method. But so far does this seem correct? . . .
That sounds very peculiar
Apart from your getLast and retrieveLast methods having confusing names, there is some poor style there. Look for a style guide which tells you what to use instead. (Hint: try here.)

I know the names are a bit unusual but these are the method names I have to use for this assignment. The get methods just show the data but does not remove it and the retrieve methods show and remove.
I'm just trying to see if the remove method seems correct because I am trying to test the methods but I do not know how to pass a comparator as an argument. Ive tried doing like
Comparator<String> comp = new Comparator() and such but nothing I do seems to be working unless you have to assign it a certain way.

And I also changed my iterator methods up as well.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Tarrell Fletcher wrote:I'm just trying to see if the remove method seems correct because I am trying to test the methods but I do not know how to pass a comparator as an argument. Ive tried doing like...

And that's most of your problem, I think: You're concentrating too much on coding, when what you need to do is take a step back and understand the problem.

Linked Lists are tricky and fiddly, and can be very hard to visualise when you're just looking at code.

My advice: Turn off your computer and draw some diagrams. A node can be drawn with a simple circle or box and an arrow for the "next" pointer(*). So maybe start out with two of those named 'head' and 'tail' and draw out all the stages needed for an "add first" and "add last", along with written instructions in English. Once you're happy with that, do the same thing for your 'peek' (get()) and 'pop' (retrieve()) methods (those are the "official" names for those functions).

The main thing is to make sure you really know the mechanics before you write another line of Java code; otherwise you're going to spend a lot of time "trying things out".

Winston

(*) - I actually often use a dot and an arrow: a dot on the edge of the box or circle to denote the pointer itself, and an arrow to show when it's actually pointing to something (because it can be null, which means "pointing to nothing").
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
No.
That is not how you write those methods. The simplest example is your hasNext() method, which should readYour next method is flaky; if you call it once too often you will suffer a NullPointerException. Read the style guide and work out how to alter that method to avoid that Exception. You should, however, throw a NoSuchElementException if you haven’t got a next node.
I don’t think you should use head in those methods, because head means something specific, the first node. Use current or currentNode or similar.
Tarrell Fletcher
Ranch Hand

Joined: Oct 07, 2011
Posts: 60
Winston Gutkowski wrote:
Tarrell Fletcher wrote:I'm just trying to see if the remove method seems correct because I am trying to test the methods but I do not know how to pass a comparator as an argument. Ive tried doing like...

And that's most of your problem, I think: You're concentrating too much on coding, when what you need to do is take a step back and understand the problem.

Linked Lists are tricky and fiddly, and can be very hard to visualise when you're just looking at code.

My advice: Turn off your computer and draw some diagrams. A node can be drawn with a simple circle or box and an arrow for the "next" pointer(*). So maybe start out with two of those named 'head' and 'tail' and draw out all the stages needed for an "add first" and "add last", along with written instructions in English. Once you're happy with that, do the same thing for your 'peek' (get()) and 'pop' (retrieve()) methods (those are the "official" names for those functions).

The main thing is to make sure you really know the mechanics before you write another line of Java code; otherwise you're going to spend a lot of time "trying things out".

Winston

(*) - I actually often use a dot and an arrow: a dot on the edge of the box or circle to denote the pointer itself, and an arrow to show when it's actually pointing to something (because it can be null, which means "pointing to nothing").


That's just it, I have actually drawn it before to see what happens. Its kinda how I got through some of the other methods and also I seen some videos and the guys suggested it. I'm just more concerned about the comparator thing. I have never even stated a comparator as an parameter which means I have never had to pass one as an argument. It just kinda messes with me and whenever I've tried doing some searches to see how to exactly do it, I find nothing. I even checked the Java Api for the comparator class and it only has equal and compare methods. There's no type of constructor so its just messing with me right now.
 
wood burning stoves
 
subject: LinkedList /Workin with a Node class as well