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?