• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

item placement in LinkedList

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the add method of the OrderedLinkedList class I am trying to place nodes with the same data items consecutively. As I have it now, the nodes are added in the order they are listed in the Test Harness.

Here is the output:

Sue, Bill, Michael, Someguy, Michael, Carl, Steve, Carl, Sue
Sue, Bill, Someguy, Michael, Steve, Carl, Sue
Sue, Bill, Someguy, Michael, Steve, Carl, Sue, Sue, Bill

Here is what I want:

Sue, Sue, Bill, Michael, Michael, Someguy, Carl, Carl, Steve.....etc.

My test case:



LinkedList



Add method:

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so what have you tried?

Have you thought about how you would do this "by hand"? That is, what are the precise steps you'd need to follow to accomplish the same task without regard to Java?
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I have come up with:
I know I need to compare the previous node with the newNode, and if they are the same, assign the new node to the next node.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ian Mcloud wrote:
I know I need to compare the previous node with the newNode,



Which previous node? Previous to what? It sounds like you're saying you'll make exactly one comparison, which in turn suggests you haven't thought through the "by hand" approach as thoroughly as you ought. You need to be able to describe your steps precisely before you jump into writing Java code.

Also, though it's not a big deal, is there any particular reason you're using Comparable here, instead of just using the equals() method? (There is actually a reason you might use it which could improve performance, but I want to know why you chose it.)
reply
    Bookmark Topic Watch Topic
  • New Topic