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?
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/
Re: Alphabetizing a Linked List using Pointers?
.....Well then. My professor handed me misinformation there. Thanks for the resource!
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.
Re: Alphabetizing a Linked List using Pointers?
What, exactly, is:
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
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;
}
}
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.
Re: Alphabetizing a Linked List using Pointers?
Quote:
Originally Posted by
T-Fox
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.