堆中的类中的结构变量和智能指针的用法

问题描述

如何在此处使用智能指针?

#include <iostream>
using namespace std;
struct node
{
    char data;
    node *next;
};

class linked_list
{
private:
    node *head,*tail,*tmp;
public:
    linked_list()
    {
        head = nullptr;
        tail = nullptr;
    }

    void add_node()
    {
        tmp = new node();
        cin>> tmp->data;
        tmp->next = nullptr;

        if(head == nullptr)
        {
            head = tmp;
            tail = tmp;
        }
        else
        {
            tail->next = tmp;
            tail = tail->next;
        }
    }
    void print()
    {
        tmp=head;
        while(tmp!=nullptr)
        {
            cout<<tmp->data;
            tmp=tmp->next;
            cout<<endl;
        }       
    }
};
int main()
{
    linked_list a;
    a.add_node();
    a.add_node();
    a.add_node();
    a.add_node();
    a.print();
    return 0;
}

如何更改add_note函数的第一行代码以使用智能指针,而不必稍后担心new / delete。需要哪些更改?我是否需要更改多行代码?如果是这样,那又在哪里?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)