I am looking to get some help understanding on how I would implement a sorted linked list at the point of insertion? Below is an addItem
function with the option to add at the beginning of the list or at the end. Is it possible to insert (thereby sorting) depending on the value given in var.number;
for example?
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;
}