• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ordered Linked List add and remove with type Comparable  RSS feed

 
Karen Byk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an assignment that requires me to implement 2 methods (add() and remove()) and create an inner class (OrderedListNode). I must use data items of type Comparable. The items should be sorted.

I understand what needs to be done, but I am having a difficult time actually writing the code. I added the main method to check to see if my code works, and it doesn't seem like that is even being read.
It compiles without error - it only gives a warning of unchecked or unsafe operations.

Thanks for any input!

Code:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Byk wrote:I am working on an assignment that requires me to implement 2 methods (add() and remove()) and create an inner class (OrderedListNode). I must use data items of type Comparable. The items should be sorted.

That much is obvious from your code - very comprehensive (and nicely written), I might add; in fact, possibly overly so, because it includes things like clear(), which have no bearing on your problem.

I understand what needs to be done, but I am having a difficult time actually writing the code.

Which makes me wonder whether you've written down what needs to be done.
Specifically:
1. What are the steps required for an add() to your OrderedLinkedList? Describe them in English, not Java, and in detail.
2. What are the steps required for a remove()?
3. What are the common steps to both those actions?

Programming is NOT code. It is understanding the problem before you, and then translating THAT into code. And it often involves lots of paper, and sleepless nights, and bruised foreheads, before you're ready to code. Coding (once you've got past the syntax; which, I suspect, is what you're actually struggling with) is easy; it's solutions and - when you get more advanced - optimal solutions, that are difficult.

I added the main method to check to see if my code works

My advice: Don't do that. Write another class that tests your code. You might find the MainIsAPain page worth reading.

However, in answer to your original question, I see one major problem with your main() method:
The while loop is not required, since listOne cannot possibly be null.

HIH

Winston
 
Karen Byk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't write all of the code - just add() remove() and OrderedListNode class.
My problem is translating it from English.

I tried it as another class, but it didn't work so I threw it in with the program I'm working with. It doesn't even print the
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Byk wrote:I didn't write all of the code - just add() remove() and OrderedListNode class.
My problem is translating it from English.

I tried it as another class, but it didn't work so I threw it in with the program I'm working with. It doesn't even print the


How did you compile your code, and how are you trying to run it?

What happens when you run it?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Byk wrote:My problem is translating it from English.

A common problem when you're starting out. Do you mind showing us the "English" that you're trying to translate?

Just the add() method will do for starters.

Winston
 
Karen Byk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was told that the head and tail nodes are null, so everything should be between them. So if I wanted to add ‘b’ to my linked list and nothing was in it, it would be head -> b -> tail
obj = object to add
currentNode = node currently looking at

If the linked list is empty, add the obj to the spot after the head (head.next)
Otherwise, compare obj to the currentNode, if obj is greater than the currenNode, compare it to the next node – keep checking until you reach the end.
If you reach the end and it is still greater than the last node, add it to the spot before the tail.
If obj is less than the currentNode and greater than currentNode.prev then add it in between the two
 
Karen Byk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:

How did you compile your code, and how are you trying to run it?

What happens when you run it?


I use JGrasp - most people have never heard of it, it is somewhat similar to eclipse I guess.
Nothing happens when I run it.

Compiling shows:
----jGRASP exec: javac -g OrderedLinkedList.java
Note: OrderedLinkedList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
----jGRASP: operation complete.

but that shouldn't cause any issues since it is just a warning.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Byk wrote:I was told that the head and tail nodes are null, so everything should be between them. So if I wanted to add ‘b’ to my linked list and nothing was in it, it would be head -> b -> tail

Yes, but it also has to be head ← b ← tail; and right now you don't appear to be doing that. You need to update both sets of pointers, not just the forward ones.

One thing that might help you: Rather than putting all your add and remove logic in the list, why not put it in your Node class? Then you only have to write logic to deal with "this" Node. For example:Do you see how that might help you?

Your OrderedLinkedList.add() method then only needs to find the right position and call your Node method to actually do the add. And similar for OrderedLinkedList.remove(): the method simply finds the Node to be removed and calls its method to do it. I suspect you might then also discover that the "find" logic for both those methods is almost identical.

HIH

Winston

PS: Happy Easter.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Byk wrote:
I use JGrasp - most people have never heard of it, it is somewhat similar to eclipse I guess.
Nothing happens when I run it.

Compiling shows:
----jGRASP exec: javac -g OrderedLinkedList.java
Note: OrderedLinkedList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
----jGRASP: operation complete.

but that shouldn't cause any issues since it is just a warning.


Well it seems you have 2 problems here. First is implementing your linked list (which Winston is giving very good advice about). Second is compiling and running the code. I'd treat them separately, so just produce a very simple Hello World application and get that to compile and run:

.

Most of my applications at work start out something like that. Once you have that you can add in your Linked List code (after following Winston's advice of coming up with a clear plan of what you need to implement etc).

Alternatively you might want to compile and run using javac and java on the commandline to eliminate JGrasp from the equation until you become more familiar with it.
 
Karen Byk
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Yes, but it also has to be head ← b ← tail; and right now you don't appear to be doing that. You need to update both sets of pointers, not just the forward ones.
One thing that might help you: Rather than putting all your add and remove logic in the list, why not put it in your Node class?


I sure did forget the backwards pointers.
I do see the benefit of using more methods. If I was writing this from scratch I probably would have separated the add and the find the correct position. However, I was told not to add any additional methods.

Mike. J. Thompson wrote: Alternatively you might want to compile and run using javac and java on the commandline to eliminate JGrasp from the equation until you become more familiar with it.


I'm very familiar with JGrasp; it has been the only thing I've used since writing HelloWorld
I learned that for some reason it runs if I put it above the inner class. It didn't work at the beginning - after the constants, and it didn't work at the very end. However, if I put it right before the OrderedListNode it is fine. Why would that be?

Something else I'm wondering is how could I get it to print the list? Since add is a boolean (have to have it that way), that only returns if it did the criteria I set or not.

Happy Easter to you as well! And to anyone that celebrates it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!