填写双向链表以进行双向遍历

时间:2015-12-31 07:26:32

标签: c++ linked-list

晚上好,大家好,我有另一个问题。和往常一样,如果这是重复的,请指出我正在复制的主题(如果我能找到它,我就不会发布)。我正在浏览并更新我的github上的一些古代代码,并将其重写为 less moronic,并且结束了理论和实现的问题。在双向链表中(头部和尾部需要跟踪),第一个成员应该如何初始化?从前面推动会留下未初始化(或未正确初始化)的尾部,反之亦然。

这就是我的意思

        $mail->ContentType   = "text/html";            //set character set

        $mail->SetFrom('mail_user1', 'Nathe Publication');  //Who is sending the email
        $mail->AddReplyTo("mail_user1","Nathe Publication");  //email address that receives the response

        $mail->Subject    = "Email subject";

        $mail->Body  = '<html><head>';
        $mail->Body  .= '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">';
        $mail->Body  .= '</head><body>';
        $mail->Body  .= '<p>';
        $mail->Body      .= "महाराष्ट्र";
        $mail->Body  .= '</p>';
        $mail->Body  .= '</body></html>';
        $mail->Body  = utf8_encode($mail->Body);
        $mail->Body  = utf8_decode($mail->Body);

        //$mail->AltBody    = "Plain text message";
        $destino = "mail_user2"; // Who is addressed the email to
        $mail->AddAddress($destino, "Ashik Lanjewar");

        //$mail->AddAttachment("images/phpmailer.gif");      // some attached files
        //$mail->AddAttachment("images/phpmailer_mini.gif"); // as many as you want
        if(!$mail->Send()) {
            $data["message"] = "Error: " . $mail->ErrorInfo;
        } else {
            $data["message"] = "Message sent correctly!";
            echo $mail->Body;
        }
        echo $data["message"];
        //$this->load->view('sent_mail',$data);
</code>

main.cpp:

template <typename T>
void double_list<T>::push_back(T data)
{
    if(tail)
    {
        node<T>* temp = new node<T>;
        temp->data = data;

        this->tail->next = temp;
        temp->prev = this->tail;
        this->tail = temp;
    }
    else
    {
        if(!head) //New list
        {
            this->head = new node<T>;
            this->tail = new node<T>;

            this->tail->data = data;

            this->tail->prev = this->head;
            this->head->next = this->tail;
        }
    }
}
template <typename T>
void double_list<T>::pop_back(T* out)
{
    if(this->tail->prev)
    {

            *out = this->tail->data;

            this->tail = this->tail->prev;
            this->tail->next = NULL;

            //numel -= 1;

    }
    else
        throw pop_empty();
}

当遍历风格匹配时(正面,正面和背面),没有问题,但我想让这更灵活一些,坦率地说,学习一些我教授在学校时从不打扰的东西。任何批评也是受欢迎的,因为我只是在这里发帖学习。提前谢谢!

1 个答案:

答案 0 :(得分:1)

您不需要额外的头部和尾部节点。如果列表由一个元素组成,则尾部指向同一元素。

template <typename T>
void double_list<T>::push_back(T data)
{
    if ( tail != nullptr )
    {
        node<T>* temp = new node<T>;
        temp->data = data;

        this->tail->next = temp;
        temp->prev = this->tail;
        this->tail = temp;
    }
    else
    {
        this->tail = this->head = new node<T>;
        this->tail->prev = this->head->next = nullptr;
        this->tail->data = data;
    }
}

template <typename T>
void double_list<T>::pop_back(T* out)
{
    if ( this->tail != nullptr )
    {
        node<T>* temp = this->tail;
        *out = this->tail->data;
        if ( this->tail->prev != nullptr )
        {
            this->tail = this->tail->prev;
            this->tail->next = NULL;
        }
        else
            this->tail = this->head = nullptr;
        delete temp;
    }
    else
        throw pop_empty();
}

参考上午的评论。 c ++的解决方案是std::list