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 348651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:23:05+00:00 2026-05-12T11:23:05+00:00

Trying to do it through a loop that traverses through the list. In the

  • 0

Trying to do it through a loop that traverses through the list.

In the loop I’m feeding the head node into a sorting function that I have defined and then I’m using strcmp to find out if which name in the node should come first.

It is not working because writing the names too early.

Im comparing them all linearly by going down the list one node at a time and not going back to see if the first should come before the last. That part explained would be helpful.

The two functions that are most important to me now are defined as follows:
I have tried my best to do what I think is right for the sorting function.

void list::displayByName(ostream& out) const
{
    list *ListPtr = NULL;
    node *current_node = headByName;
    winery *wine_t = new winery(); 
    // winery is another class object type
    // im allocating it to prevent a crash when I call it.

    while ( current_node != NULL )
    {
        *(wine_t) = current_node->item;
        wine_t = ListPtr->sort( current_node );
        out << wine_t << endl;
        current_node = current_node->nextByName;
    }
    delete wine_t;
}

winery * const list::sort( node * current_node ) const
{
    // current_node is the first node.
    const char *SecondName = NULL, *FirstName  = NULL;
    winery *wine_t   = new winery();

    if ( current_node != NULL )
    {
        SecondName   = current_node->item.getName();            
        current_node = current_node->nextByName;
        FirstName    = current_node->item.getName();
    }

    if ( strcmp( FirstName, SecondName ) == -1 )
    {
        *(wine_t)  = current_node->item;
        FirstName  = NULL;
        SecondName = NULL;
        return wine_t;
    }
    else if ( strcmp( FirstName, SecondName ) == 1 )
    {
        *(wine_t) = current_node->item;
        FirstName = NULL;
        SecondName = NULL;
        return wine_t;
    }
    else return wine_t;// then the strings are equal

    FirstName  = NULL;
    SecondName = NULL;
    return wine_t;
}

And I started to develop my nodes here:

void list::insert(const winery& winery)
{
    node *current_node = new node( winery );

    if ( headByName == NULL )
    {
        headByName   = current_node;
        headByRating = current_node;
        tail         = headByName;
        current_node->prev = current_node;
    }
    else
    {
        current_node->prev = tail;
        tail->nextByName   = current_node;
    }

    tail = current_node;
    current_node = NULL;
}

I think its correct in that function above. Could I possibly get away with sorting it there?

Below are my varaibles that I am working with:

public list
{
         ...
    void insert(const winery& winery);
    void displayByName(ostream& out) const;
}
private:
    struct node
    {
        node(const winery& winery);     // constructor
        winery item;
        node * prev;
        node * nextByName;
        node * nextByRating;
    };

    winery * const sort(node*) const;
    node * headByName;
    node * headByRating;
    node * tail;
};

Any help is appreciated.
Thanks very much =)

  • 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-05-12T11:23:05+00:00Added an answer on May 12, 2026 at 11:23 am

    From what I understand, you want list::sort to find the least node in the list which is greater than the input.

    To do this, you need to iterate through all the elements and keep the current least-but-greater node found.

    Something like this:

    node * const list::sort( node * given_node ) const
    {
        if ( given_node == NULL )
        {
            return NULL;
        }
    
        // Smallest node found which is greater than given_node.
        node * least_found_node = NULL;
    
        // Node we are looking at right now.
        node * current_node = given_node->nextByName;
    
        // Go through all nodes.
        while ( current_node && current_node != given_node )
        {
            // Is this node bigger than the given node?
            if ( strcmp( current_node->item.getName(), given_node->item.getName() ) < 0 )
            {
                // Is this node smaller than the smallest node we know of?
                if ( least_found_node == NULL ||
                   ((strcmp( current_node->item.getName(), least_found_node->item.getName() ) > 0) )
                {
                    // We found a better node.
                    least_found_node = current_node;
                }
            }
    
            current_node = current_node->nextByName;
        }
    
        return least_found_node;
    }
    

    Now change your display function to use it like this:

    void list::displayByName(ostream& out) const
    {
        // Find first node initially.
        node * current_node = sort( NULL );
    
        while ( current_node != NULL )
        {
            // Print node.
            out << current_node->item.getName();
    
            // Find next node in sorted output.
            current_node = sort( current_node );
        }
    }
    

    This part keeps calling sort until sort returns NULL. The first call to sort is with NULL so the lowest item is found (that is, the first in the sorted list). sort returns NULL if there are no more nodes larger than current_node, thus terminating the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 205k
  • Answers 205k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What are the drawbacks of drupal? This is really a… May 12, 2026 at 9:00 pm
  • Editorial Team
    Editorial Team added an answer Short answer: basically the answer is no. But can't you… May 12, 2026 at 9:00 pm
  • Editorial Team
    Editorial Team added an answer No, Java has nothing similar at the moment. Heck, properties… May 12, 2026 at 9:00 pm

Related Questions

I doubt this is possible, but I was curious if you could have more
I am moving from VB to C#. I am trying to loop through a
I'm trying to normalize some data which I have in a data frame. I
I'm trying to write some STL data out of matlab and I'm trying to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.