How can I use smart pointer here?
#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;
}
How can I change first line of code of add_note
function to use smart pointer and not have to worry about new
/delete
later. What changes are required? Should I have to change multiple lines of code? If so,then where?
Aucun commentaire:
Enregistrer un commentaire