在另一个列表中添加列表的新节点

时间:2018-12-09 13:37:09

标签: c list

我有一个包含订阅者的项目,每个订阅者都有许多合同。问题是添加节点时必须直接对节点进行排序。 到目前为止,我将程序附加在程序下方。添加订户可以很好地工作,但是当我尝试向订户添加另一个合同时却行不通。有什么建议吗?

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;

https://pastebin.com/R9LEMb57

0 个答案:

没有答案