反向链表的代码

时间:2017-06-05 18:28:29

标签: c data-structures linked-list

我已经制作了用于反转链表的代码。 如果我运行此代码,则会打印链接列表但不会反转。

我认为反向功能存在一些错误。

有人可以..告诉我代码中的错误。 我用三个指针完成了它

myView.executeJavaScript(`
    addEventListener("keyup", e=>{
        if(e.keyCode === 8) history.back();
    });
`);

1 个答案:

答案 0 :(得分:1)

这可能会对您有所帮助:

void reverse(struct node** headr)
{
    struct node* current=*headr;
    struct node* prev = NULL;
    struct node* next;
    while(current!=NULL)
    {
        next=current->next;
        current->next = prev;
        prev = current;
        current = next;
    }

    *headr=prev;
}

您的主要

int main()
{

    struct node* head = NULL;

    push(&head, 20);
    push(&head, 4);
    push(&head, 15);
    push(&head, 85);

    printf("Befor:\n");
    print(head);

    reverse(&head);

    printf("\nAfter:\n");
    print(head);

    getchar();
}

<强>输出:

Befor:
85  15  4  20  
After:
20  4  15  85