Junilu Lacar wrote:If I were your instructor, I would give you double extra credit if you successfully implemented an insertBefore(Node other) method and an insertAfter(Node other) method, with the proper error checking and handling.
Dawn Goxhaj wrote:This is the code the teacher gave us to work with. ... I don't even care about the extra credit but I hate not understanding how to do it :/
Dawn Goxhaj wrote:this stuff already makes me want to cry. doing all that would probably cause me to have a full blown meltdown.
Junilu Lacar wrote:So this is the (more expressive) code you were given as a starting point and you want to understand what
[code=java]
Lines 1 & 2 simply set up your "cursors" or placemarkers for where you are in the process of going through your list. You start at the head of the list so your current and your last "cursors" are going to be set to that initially.
Line 4 just does what the comment says: process the entire list until the curr variable doesn't reference any Node, which means you've reached the end of the list at that point.
Line 7 checks to see if the newData variable's value is less than the current Node's data value. The "< 0" part is what says "is less than" -- see the documentation for compareTo()
Lines 8 & 9 - inserts newNode before the Node referenced by curr.
Lines 13 & 14 - advances your curr and [/tt]last[/tt] references forward, with last referencing the Node that curr was just referencing, then moving curr to the next Node in the List.
In summary, this code you were given to start with has "Insert Before" semantics with the additional condition that you insert newNode before the first Node in the list it finds where the newNode data is less than the other Node's data.
Dawn Goxhaj wrote:... in his code he has an accessor getData, I don't have that bc I'm avoiding it by keeping the inner class private so I don't need to use accessors or mutators. I mean I could rewrite my whole code to add those and I could probably easily insert in the middle, but I know there's got to be a way without using any accessors.
Junilu Lacar wrote:Although I should add that if you're to maintain the list as a doubly linked list, you need to add a couple of lines of code to the section where you're actually inserting the new node. Other than that, it's very straightforward. Once you understand what needs to be done, writing the code itself takes less than 1 minute. Understanding what needs to be done first and the rest is easy-peasy.
Junilu Lacar wrote:
Dawn Goxhaj wrote:... in his code he has an accessor getData, I don't have that bc I'm avoiding it by keeping the inner class private so I don't need to use accessors or mutators. I mean I could rewrite my whole code to add those and I could probably easily insert in the middle, but I know there's got to be a way without using any accessors.
You don't have to rewrite your code. You just have to declare T extends Comparable<T> -- see this: http://stackoverflow.com/questions/8537500/java-the-meaning-of-t-extends-comparablet
This only requires the DoublyLinkedList to be used with types that support the Comparable interface, which defines the compareTo() method. When you do this, you don't need a getData() method, you just use the data values directly with compareTo(). That would actually be a better way than what your instructor gave you as an example:
Dawn Goxhaj wrote:I guess saying it's easy triggers the inner b*tch in me lol
...
What am I missing?
Dawn Goxhaj wrote:so again, I understand how my teacher did it. Like I said I could rewrite the whole code the way my teacher did and probably do it easy, but I want to know how I can implement the same idea w/o adding an accessor such as "getData"
What am I missing?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:it would appear to me that what you're "missing" is how to use that wonderful
public Node(T newData, Node<T> previousValue, Node<T> nextValue)
constructor that you've already written.
Dawn Goxhaj wrote:Thank you for dummying it down for me!
Junilu Lacar wrote:If you're still inclined to do so, please post your final code. It will be very interesting to see the solution you end up going with and maybe we can still point out something you may have missed.
Dawn Goxhaj wrote:Sorry, didn't check this after I got what I needed and was occupied with holiday cooking for last couple days. My bad lol
How I did it:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Dawn Goxhaj wrote:Sorry, didn't check this after I got what I needed and was occupied with holiday cooking for last couple days.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
It would appear to use a variable called 'position', but doesn't actually set it (or "find" it) anywhere; which suggests to me that it's in the body of your class somewhere.
Now if this method is in your DllIterator class, that's probably fine, but if it's in your DoublyLinkedList one, it's almost certainly wrong; because that would mean that your list has a permanent "current element" pointer, which doesn't make much sense.
Dawn Goxhaj wrote:that was an easy fix (I think) by just adding this before my if statement:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |