This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

item placement in LinkedList

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.)
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic