我有一个包含订阅者的项目,每个订阅者都有许多合同。问题是添加节点时必须直接对节点进行排序。 到目前为止,我将程序附加在程序下方。添加订户可以很好地工作,但是当我尝试向订户添加另一个合同时却行不通。有什么建议吗?
subscriber_t *addContract(subscriber_t *root, char name[20])
contract_t* newNode = malloc(sizeof(contract_t));
strcpy(newNode->name, name);
contract_t* aux = root->contractInfo;
contract_t** aux2 = &root->contractInfo;//aux 2 is used to retain the position where we want to put our new node
while(aux != NULL && (strcmp(aux->name, newNode->name) < 0)) //we go through the list until we reach the first element bigger than current element
{
aux2 = &aux->next;
aux = aux->next;
}
*aux2 = newNode;//we put on the position we wanted the initial data
newNode->next = aux;//then we move the root on the next position
return root;