CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  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
    Seattle, WA
    Posts
    10,895

    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
    Seattle, WA
    Posts
    10,895

    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;
    	}
    }

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Alphabetizing a Linked List using Pointers?

    Note that in "real" code, you'd just use std::list rather than coding up your own linked list.

    You should also verify that the behavior is correct with regards to capitalization. There are several ways in which it could make sense to order std::strings; the standard library chooses one of them, but you need to verify it's the one you want. Otherwise, you'll need to write your own comparison method after all.

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Alphabetizing a Linked List using Pointers?

    Quote Originally Posted by T-Fox View Post
    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.
    The only situation I can think of where you can't use the < operator to sort strings is when differences in case should be ignored or if non-ascii characters need to be considered.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

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
  •  





Click Here to Expand Forum to Full Width

Featured