Alphabetizing a Linked List using Pointers?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Alphabetizing a Linked List using Pointers?

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    Alphabetizing a Linked List using Pointers?

    I've been working on this project for a while, and I'm down to the last couple of bits; one of which is sorting as items are created. Basically all the project requires is a linkedlist of "Music Records", each of which contain the Title, Album, Artist, Genre, and Year of a song. Now, the requirment is that when a new Music Record is added, it finds where it goes alphabetically, changes the pointer of the one before it to point to it, and makes it point to the next one instead.

    The way I want to go about this is via an iterator, which I have ready to go. It's a pointer that initially points to the first record in the list, and each time I insert a record, it goes through each one to check. Howver, I'm having some major syntax issues (bolded below).

    void MusicList::insert_song (string title, string artist, string album, string genre, int year)
    {
    current = first;
    if (first == NULL)
    {
    first = new MusicRecord(title, artist, album, genre, year);
    }
    else
    {
    while (current->NextRecord != NULL)
    {
    if (current->title {??} title)
    current = current->NextRecord;
    else
    }
    current->NextRecord = new MusicRecord(title, artist, album, genre, year);
    }

    }

    I know I can't use the >, <, and == tags for letters. So, how would I go about what I'm trying to do with this syntax-wise?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Alphabetizing a Linked List using Pointers?

    What makes you think you can't use the <, >, etc operators? Assuming those "string" parameters are std::strings and not some homegrown variety, the operators should work fine.

    http://www.cplusplus.com/reference/string/operators/

  3. #3
    Join Date
    Oct 2010
    Posts
    3

    Re: Alphabetizing a Linked List using Pointers?

    .....Well then. My professor handed me misinformation there. Thanks for the resource!

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Alphabetizing a Linked List using Pointers?

    Note that the operators will not work for comparing two char arrays, AKA "C-style strings". In that case you'd need to use the functions in <cstring>. C++ string classes such as std::string exist primarily to solve the problems that dealing with C-style strings present, so giving them things like comparison operators made sense.

  5. #5
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Alphabetizing a Linked List using Pointers?

    What, exactly, is:
    Code:
    current->title
    I'd guess it's a std::string, based on the parameter passed into the function. If it is, Lindley is correct, and your professor is incorrect (unless he wants you not to use those operators, which makes no sense to me).

    Viggy

  6. #6
    Join Date
    Oct 2010
    Posts
    3

    Re: Alphabetizing a Linked List using Pointers?

    current->title is just a std::string, yeah. I don't know what was up when she told me that those operators were unusable. Either way, I just implemented the entire sorting method and it works as intended with the modifications below. Thanks again guys. I was going out of my mind trying to figure out *** to use in place of those.

    Code:
    void MusicList::insert_song (string title, string artist, string album, string genre, int year)
    {
    	current = first;
    	if (first == NULL)
    	{
    		first = new MusicRecord(title, artist, album, genre, year);
    	}
    	else
    	{
    		while (current->NextRecord != NULL)
    		{
    			if (current->title <= title && current->title <= current->NextRecord->title)
    			{
    				temp = current->NextRecord;
    				current->NextRecord = new MusicRecord(title, artist, album, genre, year);
    				current = current->NextRecord;
    				current->NextRecord = temp;
    			}
    			else
    			{
    				current = current->NextRecord;
    			}
    		}
    		iterator_reset();
    		temp = NULL;
    	}
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center