i have an "add function" that adds names alphabetically to a sorted linked list.

the list is already made in this format :

Dani Web
Mark Markos
Michael David
Zebra Zebra

.
.
etc


if i add a node : adam ( it goes to the end of the list instead of getting attached to the head )

if i add node: Adam ( it gets attached to the head fine )

so as long as I am adding according to this format Xxx it works . if i add xxx it doesn't work .

p.s : i use strcmp to find the specific location for the node .

anyway to make the liked list take the name whether it starts with capital or lower letter and put it in the right place?

Edited 4 Years Ago by evanovan: n/a

>> p.s : i use strcmp to find the specific location for the node .

There's your problem. You are going to have to write your own comparison function. strcmp has its own idea of what the order is. If you have a different idea of what the order is (and you do), you'll need to write your own and use it instead.

what is data-type for your list?

char name [30];


cin.getline(obj->name,30);

>> p.s : i use strcmp to find the specific location for the node .

There's your problem. You are going to have to write your own comparison function. strcmp has its own idea of what the order is. If you have a different idea of what the order is (and you do), you'll need to write your own and use it instead.

hmmm, good point .

that would suck to re-write the strcmp function

>> that would suck to re-write the strcmp function

You aren't rewriting the strcmp function, you're writing your own compare function. There's not much to strcmp (or to your function). strcmp is actually easier.

  1. Is the first character of str1 and str2 both NULL? If so, they are equal.
  2. Is one of them NULL and the other non-NULL? The NULL is "smaller".
  3. Is the first character of str1 greater than the first character of str2? If so it's bigger.
  4. Is the first character of str1 smaller than the first character of str2? If so it's smaller.
  5. Repeat 1 to 4 for the next character in each string.

Yours is the same way except presumably capitalization is irrelevant. So you just add a call to tolower (or toupper) before all comparsisons.

All in all, probably no more than a ten line comparison function.

Edited 4 Years Ago by VernonDozier: n/a

>> Is there another way. Other than strcmp

??? I'm already suggesting that you NOT use strcmp. In fact, I don't think you CAN use strcmp. That was the whole point of my post, so I'm not sure what you mean with this post. In order to sort, one must compare. If no standard C++ comparison function is available (and I can think of none that match your needs), you must write your own.

That said, you can use whatever like as helper functions in your own comparison function, including strcmp. You can copy your strings to temporary strings, make them all lower or upper case, then use strcmp to compare those strings. That would work. But that will take at least as much code as writing the function the way I mentioned it before, so I don't see it as any easier.

sorry didnt see your earlier post. will implemnt my own functions as you suggested.

thanks

This article has been dead for over six months. Start a new discussion instead.