在C中只向链表添加新数据 if(!head->next){ head->next=newNode;/*如果只是虚拟节点,则将节点添加到列表的末尾*/ }否则{ /*在链表中迭代,直到找到大于新节点的节点*/ 而(head->next&&strcmpa((head->next)->data,newNode->data)下一步; 如果(!head->next){ head->next=newNode;/*如果没有节点的值大于此值,则将新节点添加到结束列表中*/ }否则{ newNode->next=head->next;/*将新节点指向下一个节点*/ head->next=newNode;/*将当前节点指向新节点*/ } }
如何编辑此代码以使其拒绝数据字段等于列表中已存在节点的任何新节点?将最后一个在C中只向链表添加新数据 if(!head->next){ head->next=newNode;/*如果只是虚拟节点,则将节点添加到列表的末尾*/ }否则{ /*在链表中迭代,直到找到大于新节点的节点*/ 而(head->next&&strcmpa((head->next)->data,newNode->data)下一步; 如果(!head->next){ head->next=newNode;/*如果没有节点的值大于此值,则将新节点添加到结束列表中*/ }否则{ newNode->next=head->next;/*将新节点指向下一个节点*/ head->next=newNode;/*将当前节点指向新节点*/ } },c,linked-list,nodes,C,Linked List,Nodes,如何编辑此代码以使其拒绝数据字段等于列表中已存在节点的任何新节点?将最后一个else{更改为 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
else{
更改为
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 */
}
}
将最后一个
else{
更改为
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 */
}
}
你爸爸。谢谢。看了这么久,我都不知道这段代码是什么意思了。@namarino很高兴帮助你:DYou da man。谢谢。看了这么久,我都不知道这段代码是什么意思了。@namarino很高兴帮助你:DYou需要遍历链接列表中当前存在的所有节点,并始终检查新节点的值等于当前节点的值。如果是,则停止迭代。如果列表中没有节点与新节点具有相同的值,则循环将仅在列表中的最后一个节点结束,其中“currentNode->next”等于NULL。此时,需要通过使列表中的最后一个节点指向新节点来添加新节点。但是,如果希望列表保持排序,则可以运行提供的插入排序的代码段。您需要迭代链接列表中当前存在的所有节点,并始终检查新节点的值是否等于当前节点的值。如果等于,则停止迭代。如果列表中没有节点具有相同的值,则停止迭代作为新节点,则循环将仅在列表中的最后一个节点结束,其中“currentNode->next”等于NULL。此时,您需要通过使列表中的最后一个节点指向新节点来添加新节点。但是,如果您希望保持列表的排序,则可以运行提供的代码片段以插入排序。