仅将新数据添加到C中的链接列表

时间:2016-12-04 04:02:54

标签: c linked-list nodes

if (!head->next) {
    head->next = newNode;   /* if only dummy node, add node to end of list */
} else {
    /* iterates through linked list until a node is found that is greater than the new node */
    while (head->next && strcmpa((head->next)->data, newNode->data) < 0) 
        head = head->next;
    if (!head->next) {
        head->next = newNode;   /* adds new node to end list if no nodes are greater in value */    
    } else {
        newNode->next = head->next;     /* points new node to the next node */  
        head->next = newNode;   /* points current node to new node */
    }
}

如何编辑此代码以使其拒绝任何其数据字段等于列表中已有节点的新节点?

1 个答案:

答案 0 :(得分:2)

将最后else {更改为

else if(strcmp(head->next->data, newNode->data)) {