-
October 26th, 2010, 10:37 AM
#1
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?
-
October 26th, 2010, 10:45 AM
#2
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/
-
October 26th, 2010, 10:57 AM
#3
Re: Alphabetizing a Linked List using Pointers?
.....Well then. My professor handed me misinformation there. Thanks for the resource!
-
October 26th, 2010, 11:00 AM
#4
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.
-
October 26th, 2010, 11:04 AM
#5
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
-
October 26th, 2010, 11:12 AM
#6
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;
}
}
-
October 26th, 2010, 11:19 AM
#7
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.
-
October 26th, 2010, 11:34 AM
#8
Re: Alphabetizing a Linked List using Pointers?
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|