I wonder if someone answers my question, if somebody tried to use qsort to sort link-list?!!
I wonder if someone answers my question, if somebody tried to use qsort to sort link-list?!!
I suppose you could. Though I really don't see the point. Why not just insert in order when you create the list in the first place? I suppose you could have a list that simply [a][pre]ppends the data, and you want to sort it later. But yes, you could use qsort. Remember that with qsort, you're writing your own compare function for it to use. So yeah, you could get it to work. Again, I'm not really sure why you'd want to. I wouldn't. I'd just write a function to sort the list for me. Basicly just reconstruct the list prepending or appending as needed.
Quzah.
Hope is the first step on the road to disappointment.
... and don't forget the standard implementation of qsort is for arrays only.
Skip Lists are pretty neat, if you want an alternative.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
That's why you "sort it" into an array of pointers to your nodes.Originally Posted by Hammer
Quzah.
Hope is the first step on the road to disappointment.
Fair point Kinda defeats the point of having a dynamically sizable list though!
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Sure, but if you're really fond of qsort...Originally Posted by Hammer
Quzah.
Hope is the first step on the road to disappointment.
Thanks for all of you,
I tried to implement the approach on the following link ( for Kent):
http://www.experts-exchange.com/Prog..._20754585.html
but I am having problem on the last line(sorted_stocks[index ++] = n of this function
void CopyListToArray(List l)
({
Node *n;
int index;
for(index =0, n =l ; n ; n= n-> next)
sorted_stocks[index ++] = n; //////////here is the problem?!!!
})
compiler displays: [C++ Error] Main.c(518): E2096 Illegal structure operation
>E2096 Illegal structure operation
What is sorted_stocks declared as?
My best code is written with the delete key.
That is the declaration for n and sorted_stocks:
for sorted_stocks:
//global
typedef struct node
{
Item item;
struct node *next;
}Node;
Node sorted_stocks[MAX_NO_ITEM];
for n (inside CopyListToArry()):
Node *n;
You want to either change the statement:
Or the declaration of sorted_stocks:Code:sorted_stocks[index ++] = *n;
As it is, sorted_stocks[index] is a Node, but n is a pointer to Node. The types do not match.Code:Node *sorted_stocks[MAX_NO_ITEM];
My best code is written with the delete key.