sorting char* arrays
Posted
by skazhy
on Stack Overflow
See other posts from Stack Overflow
or by skazhy
Published on 2010-05-25T16:24:14Z
Indexed on
2010/05/25
16:31 UTC
Read the original article
Hit count: 135
Hi! I have a datastructure
struct record {
char cont[bufferSize];
record *next;
};
When I add new records to this structure, I want them to be sorted alphabetically. I made this function, that adds record in the right place (by alphabet) in the linked list:
record *start=NULL, *p, *x;
void recAdd(char*temp) {
p = new record;
temp[strlen(temp)] = '\0';
for (int j=0;j<bufferSize;j++) p->cont[j] = temp[j];
if (start==NULL) start=p;
else {
x=start;
int c=0;
while (recComp(x->cont,p->cont) <= 0 && x->next != NULL) {
x=x->next;
c++;
}
if (c == 0) {
p->next=start;
start=p;
}
else {
x=start;
for (int i=0;i<c;i++) x=x->next;
p->next=x->next;
x->next=p;
}
}
for (int j=0;j<bufferSize;j++) temp[j] = NULL;
};
But somehow it doesn't sort things right. What is wrong with my function?
© Stack Overflow or respective owner