按升序将节点插入链接列表

时间:2015-12-10 17:01:44

标签: c insert linked-list

我有一个升序的链表,在某些代码中,我将生成一个新节点并取出前两个节点,并需要将其插入到链表中的正确位置。我的下面代码是尝试这样做的,所以我应该收到打印语句list is now 5 nodes long list is now 4 nodes long list is now 3 nodes long等。

在开始时,temp指向列表中的第一个节点。我的逻辑是当它找到temp - >下一个 - >频率要大于/等于,它将指向parent_node - >接着在temp - > next(等于/大于1)和point temp - >接下来在父节点处,从而将节点插入列表中。然而,这似乎不起作用。

while (ind == 0)
{
    temp = temp -> next;
    if (temp -> next -> frequency >= parent_node -> frequency && temp != NULL)
    {
        parent_node -> next = temp -> next; /* parent points at bigger value */
        temp -> next = parent_node; /* temp points at parent */
        ind = 1;
    }
}

temp =  list -> start; /* point list back at start */
while (temp != NULL)
{
    temp = temp -> next; /* run through list */
    counter++;
    printf("The linked list is now: %d nodes long\n", counter);
}

它将打印列表现在为1 - > 10长,现在1 - > 9长,现在1 - > 8长。

0 个答案:

没有答案
相关问题