3 Replies - 506 Views - Last Post: 24 April 2010 - 03:36 PM Rate Topic: -----

#1 midnightcowboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-April 10

Removing items from a LinkedList

Posted 23 April 2010 - 10:45 PM

I have a crash option on my menu where a race id is entered.(When a race is created 1 driver and 1 car are entered and an id is created. Each race only has 1 driver and 1 car. If there is a crash, the race id is entered, found and then removed from the list. I got this part to work but cannot figure out how to get the driver and car, that were registered to that particular race, removed from my LinkedList as well.

I think I need to look up the race id, then remove the car and driver that were assigned to that flight. Then I should remove the race. Any ideas?


 private void crash()
    {
        System.out.println("Crash");
        Race race = races.find(races.readId("Race"));
        races.removeRace(race);
        // this works up to this point and removes the race from the LinkedList
        // but I am not sure how to find the driver or car, that were registered into the race, removed.
        // Only the race id is entered and the race, driver and car are then all removed.
       Car car = cars.find("Car"));
       cars.removeCar(car);
       drivers.removeDriver(driver);
       menu();
    }




Is This A Good Question/Topic? 0
  • +

Replies To: Removing items from a LinkedList

#2 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: Removing items from a LinkedList

Posted 24 April 2010 - 05:33 AM

We'll need to see more code to see how you've implemented this. Are the cars/drivers data members of the Race class? If so, removing the Race object should delete them as well.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,651
  • Joined: 27-December 08

Re: Removing items from a LinkedList

Posted 24 April 2010 - 06:01 AM

It sounds like you are using parallel LinkedLists to associate Drivers and Racers. This is bad practice as it is easy to mismatch data. Instead, you should design a class to encapsulate a Driver and Racer, or more closely relate the two attributes for a single Object.

For more information on class design, you should check out my tutorial Moving Away From Parallel Arrays. While you are not using Arrays, the concepts I discuss in this tutorial are applicable to LinkedLists as well.
Was This Post Helpful? 1
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Removing items from a LinkedList

Posted 24 April 2010 - 03:36 PM

Was about to suggest the same as mac. You should do something more like (as an example):
class Racer
{
    String driver;
    String car;

    public Racer(String myDriver, String myCar)
    {
        driver = myDriver;
        car = myCar;
    }

    // getters
}


Then you could simply use getters to get the value of each to compare in the list or whatnot. I think this is more or less what mac was referring to.

This post has been edited by zim1985: 24 April 2010 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1