• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

LinkedList /Workin with a Node class as well

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80230
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic