If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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).

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:

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).

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.

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?

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

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.