Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么';对于循环双链接列表,在第一个节点之前/之后插入的算法是什么?_C++_Deque - Fatal编程技术网

C++ 什么';对于循环双链接列表,在第一个节点之前/之后插入的算法是什么?

C++ 什么';对于循环双链接列表,在第一个节点之前/之后插入的算法是什么?,c++,deque,C++,Deque,我一直在使用尾部指针构建deque,我只是想知道如果我使用循环指针,算法是否会相同。在我看来,我不必再跟踪尾部指针了,维护和更新列表会更容易。然而,它也让我感到震惊,第一个节点之前/之后的插入几乎是一样的,因为它是一个圆,所以第一个节点和之后节点之间没有区别。我的理解正确吗?如果您能在这里演示一个示例或显示这些函数的伪代码,那就太好了 下面是一些代码,用于在头部之前/之后插入节点。如果这就是你要找的 struct node { int val; struct node *prev

我一直在使用尾部指针构建deque,我只是想知道如果我使用循环指针,算法是否会相同。在我看来,我不必再跟踪尾部指针了,维护和更新列表会更容易。然而,它也让我感到震惊,第一个节点之前/之后的插入几乎是一样的,因为它是一个圆,所以第一个节点和之后节点之间没有区别。我的理解正确吗?如果您能在这里演示一个示例或显示这些函数的伪代码,那就太好了

下面是一些代码,用于在头部之前/之后插入节点。如果这就是你要找的

struct node
{
    int val;
    struct node *prev;
    struct node *next;
};

bool insert_after_head(struct node *head, int val)
{
    if(head == NULL)
        return false;

    struct node *temp = new node;
    if(temp == NULL)
        return false;

    temp->val = val;

    // build new links
    temp->prev = head;
    temp->next = head->next;

    // rebuild old links(watch the order)   
    head->next->prev = temp;
    head->next = temp;

    return true;
}

bool insert_before_head(struct node *head, int val)
{
    if(head == NULL)
        return false;

    struct node *temp = new node;
    if(temp == NULL)
        return false;

    temp->val = val;

    // build new links
    temp->next = head;
    temp->prev = head->prev;

    // rebuild old links(watch the order)
    head->prev->next = temp;
    head->prev = temp;

    return true;
}