我希望了解如何在插入时实现排序链表?下面是addItem
函数,可以选择在列表的开头或结尾添加。是否可以根据var.number;
中给出的值插入(从而排序)?
struct structName {
int number;
int number2;
char aString;
struct structName *next;
};
struct structName *head = NULL;
struct structName *curr = NULL;
struct structName *element = NULL;
struct Struct var;
struct structName* addItem(struct structName var, bool appendList) {
struct structName *pointer = (struct structName*)malloc(sizeof(struct structName));
pointer->someString = var.aString;
pointer->aNumber = var.number2;
pointer->anotherNumber = var.number;
pointer->next = NULL;
if(appendList) {
curr->next = pointer;
curr = pointer;
}
else {
pointer->next = head;
head = pointer;
}
return pointer;
}
答案 0 :(得分:0)