我有一個數據結構排序的char *數組
struct record {
char cont[bufferSize];
record *next;
};
當我添加新記錄這種結構,我希望他們能夠按照字母順序排序。我做了這個功能,即在正確的位置(由字母)鏈接列表添加記錄:
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;
};
但不知何故,它不排序正確的事情。我的功能有什麼問題?
temp [strlen(temp)] ='\ 0'; - 這不是必要的 – KPexEA 2010-05-25 16:33:36
@anno:它也不是C--它是沒有庫的C++。 – 2010-05-25 16:55:05
recAdd()爲什麼修改'temp'的內容? – 2010-05-25 17:02:36