Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:52:33+00:00 2026-06-15T13:52:33+00:00

I am learning how to work with classes. I have made two classes and

  • 0

I am learning how to work with classes. I have made two classes and one is a list of cars. However, I need to modify the add function so that it will add cars sorted by price. The problem I am having is that it will send the cheapest car to the beginning but kill the rest of the list. Here is my code for the add…

public void add_car(the_cars new_car)
        {// Method to add cars to list
            if (count == 0)
            {// If this is the first car 
                first = new_car;
                last = new_car;
                count = 1;
            }
            else
            {// If it is not the first car
                if (new_car.getPrice() < first.getPrice())
                {// If price of new car is lower than first car
                    last = first;
                    first = new_car; // new car becomes first car
                }
                else
                {
                    while (new_car.getPrice() > last.getPrice() || last.next != null)
                    {
                        last.next = new_car; // Null value now equal to car
                        last = new_car;
                    }
                }


                count++;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:52:34+00:00Added an answer on June 15, 2026 at 1:52 pm

    You have identified the cases correctly:

    1. Is the list still empty?
    2. Should the item be added at the beginning?
    3. If not: after which item do we need to insert?

    You implemented the first case alright.

    The second case is wrong: Inserting in the beginning means to change the first element to new_car, but new_car.Next needs to point to the previous first element, otherwise you lose the link.

    The third case is also wrong: You need go towards the end of the list until either you’ve reached the last element (which is then the one you need to insert after) or until you find an element the successor of which has a greater price and insert after that.

    The reason why I can put the while condition like that is that if current != last I can be sure that there’s a current.Next, otherwise it would be last by definition. The reason I need a temporary iteration element is that if I modify first I lose the entry point to the list.

    If have not tested the following code, but it should give you a clue and if it doesn’t work, single-step debugging will help you.

    public void add_car(the_cars new_car)
    {// Method to add cars to list
        if (count == 0)
        {// If this is the first car 
            first = new_car;
            last = new_car;
            count = 1;
        }
        else
        {// If it is not the first car
            if (new_car.getPrice() < first.getPrice())
            {// If price of new car is lower than first car
                new_car.Next = first; // Insert the first car as the first element
                first = new_car;
            }
            else
            {
                // Create temporary iteration element
                the_cars current = first;
    
                // Find the car
                while (current != last && new_car.getPrice() >= current.Next.getPrice())
                    current = current.Next;
    
                // Insert after the given element
                new_car.Next = current.Next;
                current.Next = new_car;
    
                // Also you may need to update last to match the new end
                if (current == last)
                    last = new_car;
            }
    
            count++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, Test2 and Test3. Test2 has an attribute test3 that is
I'm learning about VB.Net and need to work with an SQLite database using the
I'm learning about interface properties and ran into something that I thought should work
1) I have some static classes in my project that allocate variables within their
I have been learning Objective-C as my first language and understand Classes, Objects, instances,
I'm learning how to work with threads in Java and I need some advice..
Recently I've been learning how to create methods within classes so that I only
I'm learning to use the mkreversegeocoder classes and have got it working using the
Ok I am learning php and trying out classes and functions. Currently I have
got a question regarding serializing classes that I've defined. I have some classes like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.