• Post Reply Bookmark Topic Watch Topic
  • New Topic

MyLinkedList class  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've designed a SinglyLinked list class that implements MySinglyLinkedList, would someone mind commenting on or recommending any improvements I could make to the code. I've been referencing my text and a few other sites to assist in building this class...and I'm still uncertain of the purpose of a few of the methds: next(), hasNext().

Also, I have not found a clear explanation of the following code example: tail.next = tail; There are several instances of this in the code below....I'm just not sure exactly how this assigns the value to the next object in the other class..?? ??



 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:I've designed a SinglyLinked list class that implements MySinglyLinkedList

Actually, I think that's the wrong way around.

would someone mind commenting on or recommending any improvements I could make to the code.

Well, I could, but some of my issues may have been forced on you by the SinglyLinkedList interface you're implementing (which I presume is part of your "text"). Unfortunately, you haven't provided it for us to see.

However:
1. Your class seems to encompass both the business of "being a List" and that of "being an iterator" - ie, it not only stores values, it traverses them as well (and to answer your other question, that's what next() and hasNext() are for).
Most Java collections just do the first, and they return an Iterator (java.util.Iterator) to do the second.

2. Your class implements SinglyLinkedList, yet it uses a NodeList class that defines a 'previous' reference. There's nothing actually wrong with that if it's never used, but it does seem odd.

3. NodeList also seems like an odd name. Node is more usual.

4. Every place you've put NodeList, they should be NodeList<T>'s.

5. Your search() method contains several mistakes, but mainly:
  • 'if (searchHead == key)' won't compile because searchHead is a NodeList, not a T.
  • Even if you correct that, the expression is wrong. Have a look at the AvoidTheEqualityOperator (←click) page to find out why.

  • And there are several other problems as well. Linked Lists aren't easy - especially if you're tackling them for the first time - so my advice would be to StopCoding (←click) and write down everything you think you need to do.

    Take one action at a time - eg, addFirst() - and write down all the steps needed to achieve it. Then translate it to code and compile it. Then test it. And don't even think about writing another method until you know it works...every time.

    Sorry if it sounds blunt, but you did ask...

    Winston
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, silly mistake -- been working long days and I'm beginning to lose brain function. haha You are right, the LinkedList interface seems easy to visualize, just connect a bunch of nodes together that contain objects.....

    I will try to keep my responses short and to the point.

    1. I understand what you are saying and I just checked my textbook(Intro to Java, Y. Daniel Liang) to verify. You are right, but our professor has us creating our own interfaces and defining them another class(SinglyLinkedList interface and MyLinkedList) and his template includes traverse() in the interface...? He is a wiz at this stuff and a great teacher, but from what I can tell he's included traverse, hasNext, and next, all within MyLinkedList and not used an interator. Now, we're not permitted to use any of the java.util imports, so that might explain the different approach.

    2. lol Would "public Node search" be better? Again, just following the professors template. Should I remove search from the interface altogether?
    Before reading your post, I actually modified the Node class to be a private inner class, since a separate Node class will return a reference to the node and therefore lacks any security measure (mostly trying to for good habits).

    3. Gotcha! Just thought it was another Java SE name, similar to ArrayList, so I was trying to avoid confusion! ha

    5. Thank you for the source, I will take a look at it.

    For this program, we add a string to a String class that will hold the character string as a singly linked list of character. Each node contains a single character as its data. How this is going to happen, I'm not quite sure yet. Which leads me to a new question...if I create a String object and send that to a String class...how on earth are MyArrayList and Node supposed to be implemented? I know String class will extend MyArrayList...but how will the String be dispersed into character nodes??


    I can handle constructive criticisms...you are doing me favor! Thank you.
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'd actually like to ask you guys an "off topic" question....what is the best way to develop my understanding of Java and retain all the information? I just started reading a book, "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin which I think will be a great read and provide a lot of insight. There are so many different approaches (a few better than most) so how does one get there? I know they say it's practice..practice...practice, but how long does it take before it makes sense and you can whip up a nice program in a couple hours, instead of days!!!

    I enjoy programming, but I'm absolutely terrible at it!
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Derek Nickerson wrote:
    2. lol Would "public Node search" be better? Again, just following the professors template. Should I remove search from the interface altogether?


    If the interface was provided by your professor, then probably not. Remember that an interface is a contract -- your professor may decide to exercise your linked list with a tester application -- and if you change the interface, it won't compile.

    Henry
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Winston, thanks a lot for the link. After reading it and WhatNotHow, I realize I've formed some bad habits and need to take a step back every once in a while. Great stuff and very well done! I have a friend that's always saying, I'm a bull in a china closet....safe to say that's applicable in my approach to programming.

    I know you have a few other blogs posts, do you know of any other good sources that approach Java in a similar manner?
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought I had figured this out and properly formatted my code, however, when I compile the code -- the contents of the node returns false (null). I created a string object and added it to the front of the list.



    (For the class Node, I don't believe I'm going to need my set/get methods, but I'm not ready to deal with that yet. )
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Derek Nickerson wrote:I thought I had figured this out and properly formatted my code, however, when I compile the code -- the contents of the node returns false (null). I created a string object and added it to the front of the list.


    Your constructor sets current to null.
    Your addToFront method doesn't alter the value of current.
    Your hasNext method returns false if current is null
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I see what you are saying, but prior to my post I tried to set current = head within the hasNext() method --- but then it returned PointerOutOfBoundsException.
    I also tried to assign current = head after reading your post, but I get the same result. Which tells me two things: 1. The object is not being added to the list. 2.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Derek Nickerson wrote:Yes, I see what you are saying, but prior to my post I tried to set current = head within the hasNext() method --- but then it returned PointerOutOfBoundsException.
    I also tried to assign current = head after reading your post, but I get the same result. Which tells me two things: 1. The object is not being added to the list. 2.

    You shouldn't be setting anything in the hasNext method. That method should only be telling you the current state of the List.
    The state should only change in methods that add, remove or rearrange the elements in your list or move the list iterator.
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I didn't think so, but I thought it was worth a try since I wasn't getting a result the other way. Why is my object not being stored in the nodelist? Everything I have tried keeps giving me a NullPointer error.

    I think I have a fairly good understanding of the singly linked list, in that it is sort of like playing the kids game, telephone. Each node only holds the address of the next node. By taking away a nodes address to its neighbor, you remove the node from the linear sequence. I know there is more, but what has me stumped right now is why the node is null and does not contain its object??
    ----------------------------------------------------------------------------------------------------------------
    I started with a clean slate and I'm trying to construct one method (addToFirst). Hopefully the condensed version will merit a response. I'm still getting the OutOfBoundsException. I tried to design a couple toString methods to return details about the list.







     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I've narrowed it down to two major problems.

    1. When I add the first node, or second node (in addtoFirst) -- I'm not retaining the value of head. When I try to use it within my getNext and hasNext methods, its using the null version of head. Could someone give me a straight answer to remedy this...my mind is racked!

    2. Do I need to setup a linkedlist array? The reason I ask is otherwise the nodes are overwritten each time I run the code -- therefore it seems reasonable that they be stored somewhere.
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can anybody help me before I have to repost my code to a new thread?
     
    Campbell Ritchie
    Marshal
    Posts: 56598
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please read this.
    How have you implemented Joanne's suggestions from last night?
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I've tried to define current several different ways, but its still returning null. I understand what she was saying and tried to make the adjustments in my code -- clearly I'm just not implementing it correctly. The code will print the element in the node briefly, but then it disappears and the null pointer error displays. I've tried to set the value explicitly within the add method, I've used a getter/setter, and I've simply used the head variable, since it will always be assigned to the first node in the list. I'm really at a loss here and I've checked a few other resources and they always seem very straightforward. Am I trying to do too much? I know I've created an object firstNode within the method..but I do assign the value to head... and then set current = head?

    ==========UPDATE==========
    Variable assignment!! aaahh Too focused on the new List concepts.
    Wow.....is there a rock, so I can go crawl under it.

    And I apologize for the consecutive posts...no excuse, other than summer classes are a pain in the butt.

     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Derek Nickerson wrote:@Winston, thanks a lot for the link. After reading it and WhatNotHow, I realize I've formed some bad habits and need to take a step back every once in a while. Great stuff and very well done!

    Thanks, and very glad that it helped.

    I have a friend that's always saying, I'm a bull in a china closet....safe to say that's applicable in my approach to programming...

    It's understandable, and I was exactly the same. And the reason (I think) is that you're just dying to see the results of your code. In class, where you have guidance, the process is almost mystical - you're given some instructions, and hey presto, a couple of hours later you have a program with a ball that bounces around your screen, pong-style, and changes colour. And what you remember is the effect, not the instructions that showed you what you need to do to do it. You may remember some of the more interesting stuff, like how to get it to "bounce"; but you've missed a lot of the steps that got you there to begin with.

    And one of the reasons that we old wrinklies can spot the symptoms of "what-not-how purgatory" is that all your questions are about HOW:
  • I've tried [something], and it doesn't work.
  • Why is [whatever] null?
  • Why does [so-and-so] happen when x is 3 and it's a Thursday?

  • And what that means, very often, is that you've implemented (ie, written code), before you understand why you're doing it that way.

    The sorts of questions you should be asking before you write a single line of Java are:
  • What is a Node, and why do I need it for a "linked" list?
  • What are the 'head/tail' of a list?
  • What do I need to do if I add a new value to the start/end of my list (and how do I maintain its head/tail so that they're still correct after it's done)?
  • What do I need to do to traverse my list? Does it affect the head or tail?

  • Note: mostly "what" questions, not "how" ones , and none of them presumes that we're talking about a Java list.

    One of the things that you may find useful when thinking about linked lists are diagrams. The Wiki page about them shows you one after four lines. It isn't needed for every type of problem, but linked lists are particularly tough to "visualise" without some help. I suspect that a quick look at that first picture alone will give a better idea of what the "list" is, what its "nodes" represent, and where the "values" that you add and remove fit in.

    I know you have a few other blogs posts, do you know of any other good sources that approach Java in a similar manner?

    Simple answer: No. But that's not to say there isn't anything like it around.

    My "stuff" is basically accidental, and probably because I'm a lazy git. It just seems easier to write a quick FAQ page, rather than answer tons of similar questions the same way. I'm 57, and I need to keep me carpels in shape.

    You can find a link to all of my articles in my signature (at the bottom of each post) if you're interested. However:
    Caveat lector ("let the reader beware").
    They are my slant on things, and their quality varies. You may also find people that totally disagree with my approach.

    HIH

    Winston
     
    Derek Smiths
    Ranch Hand
    Posts: 119
    Eclipse IDE Mac VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, I suppose my approach has been a little too similar to my engineering courses (circuits, physics, and math(s?)) in the idea that if I chisel away at a few of the rocks, I will have a better understanding of the material. With programming, I'm using a jackhammer on boulders, and it ain't pretty! In addition to that, I'd say that in our computer science 1 course, the importance of 'preparation before programming' was never a guided process and as a result very easy to form bad habits. Often times in class, we're given very generic, almost too easy, programs that professors will walk us through -- and this is where I get my false sense of security! By the time I get home and start to program -- ... I will say, my computer science 2 prof is much better, albeit, a little more demanding. Unfortunately, holes in my understanding from cs1 are bleeding over into cs2.

    I guess what I mean to say is that this is where I'd prefer to see a more practical application, so we can really explore the material and topics. Personally, I think it would be great to pick a single, very large program that will be the focus of an entire semester. Break down the program by the topics/chapters covered in the book -- and by the end of the semester students will have a better understanding of the building/planning process, the links between topics...etc..etc... While professors reviewing particular examples in the book and clicking through powerpoint slides can be helpful, I often just find it redundant.... ... (Lesson: get into a great university)

    My ¢.02 not that anybody asked. I am american! haha

    I'll just try to remember and focus on the What.

    Thanks again.

     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Derek Nickerson wrote:Personally, I think it would be great to pick a single, very large program that will be the focus of an entire semester. Break down the program by the topics/chapters covered in the book -- and by the end of the semester students will have a better understanding of the building/planning process, the links between topics...etc..etc...

    Actually, I think that would be a great approach, BUT there are several possible roadblocks:
    1. Java programs - or classes (at least the best ones) - usually aren't very large. They're a bunch of small classes that together make a "big" program (or application).
    2. It'd probably be quite tough to find an existing application that fills all the needs.
    3. Ideally, you want an app where some mistakes were made as well, so you can discuss them and possibly try and work out why they were made. It's one of the reasons why this book is so good. The author (who was one of Java's foundation designers) is very honest about some of the mistakes he made, and in several cases explains why.

    Winston
     
    J. Kevin Robbins
    Bartender
    Posts: 1801
    28
    Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Derek Nickerson wrote:I'd actually like to ask you guys an "off topic" question....what is the best way to develop my understanding of Java and retain all the information? I just started reading a book, "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin which I think will be a great read and provide a lot of insight. There are so many different approaches (a few better than most) so how does one get there? I know they say it's practice..practice...practice, but how long does it take before it makes sense and you can whip up a nice program in a couple hours, instead of days!!!

    I enjoy programming, but I'm absolutely terrible at it!

    Short answer, years. But it's worth it. I suggest you read this. Learning programming is a lot like learning a foreign language or a musical instrument. It takes years of practice to get really good at it, but you will get a little better every day and with every program. Sometimes it's hard to not get discouraged, especially in the early stages. I can't tell you how many times I said "I'm just not smart enough to learn this", yet somehow I did after many, many months of study.

    The fact that you enjoy it is the key to success. I tell people, if you are getting into programming for the money, you'll never be very successful at it, just mediocre at best. But if you are doing it because you love it and you go home from work and write more code in your spare time, you will be a success.

    Hang in there. It's a marathon, not a sprint.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!