나는 스마트 포인터에 대해 읽었으므로 실제 데모 예제를 원했기 때문에 아래의 DLL 코드를 만들었습니다. 문제는 노드가 적절하게 배치되었지만 노드 메모리는 아닙니다. 내가 무엇을 잘못하고 있는지 확신 할 수 없다. 내 이해까지 범위가 끝나면 노드를 자동으로 삭제해야합니다. 내가 틀렸다면 나를 바로 잡아주세요.스마트 포인터를 사용하는 이중 링크 목록
원본 코드 :
#include <iostream>
#include <memory>
using namespace std;
template <typename T>
class DLL {
class Node {
public:
T key;
std::shared_ptr<Node> next;
std::shared_ptr<Node> prev;
Node():key(),next(),prev(){}
Node(T data):key(data),next(),prev(){}
~Node(){
cout << "node deleted \n";
}
};
std::shared_ptr<Node> m_head;
std::shared_ptr<Node> m_tail;
std::size_t length;
public:
DLL() : m_head() ,m_tail() , length(0){
}
virtual ~DLL(){
}
void add_back(T data){
std::shared_ptr<Node> node = std::make_shared<Node>(data);
if(!m_tail){
m_tail = std::move(node);
m_head = m_tail;
}
else{
m_tail->next = std::move(node);
m_tail->next->prev = m_tail;
m_tail = m_tail->next;
}
length++;
}
void add_front(T data){
std::shared_ptr<Node> node = std::make_shared<Node>(data);
if(!m_head){
m_head = std::move(node);
m_tail = m_head;
}
else{
m_head->prev = std::move(node);
m_head->prev->next = m_head;
m_head = m_head->prev;
}
length++;
}
void printNodes(void){
for(std::shared_ptr<Node> temp = m_head; temp ; temp = temp->next) {
cout << temp->key << '\n';
}
}
void addAtPosition(T data , std::size_t pos){
if(pos < 0 || pos >= length) {
throw("Invalid position");
}
if(pos == 0){
add_front(data);
}
else if(pos == length - 1){
add_back(data);
}
else{
std::shared_ptr<Node> temp = m_head;
for(; temp && pos ; temp = temp->next) {
pos--;
}
std::shared_ptr<Node> node = std::make_shared<Node>(data);
std::shared_ptr<Node> prev = temp->prev;
temp->prev = std::move(node);
temp->prev->next = temp;
temp->prev->prev = prev;
prev->next = temp->prev;
length++;
}
}
};
int main(int argc , char** argv){
std::unique_ptr<DLL<int>> m_list = std::make_unique<DLL<int>>();
m_list->add_front(3);
m_list->add_front(2);
m_list->add_front(1);
m_list->add_back(4);
m_list->add_back(5);
m_list->add_back(6);
m_list->addAtPosition(7,0);
m_list->addAtPosition(7,4);
m_list->addAtPosition(7,7);
m_list->printNodes();
return 0;
}
수정 된 코드 :
#include <iostream>
#include <memory>
using namespace std;
template <typename T>
class DLL {
class Node {
public:
T key;
std::shared_ptr<Node> next;
std::weak_ptr<Node> prev;
Node():key(),next(),prev() {}
Node(T data):key(data),next(),prev() {}
~Node(){cout << "deleted \n";}
};
std::shared_ptr<Node> m_head;
std::weak_ptr<Node> m_tail;
std::size_t length;
public:
DLL():m_head(),m_tail(),length(0){}
void addFront(T data){
std::shared_ptr<Node> node = std::make_shared<Node>(data);
if(length == 0){
m_head = std::move(node);
m_tail = m_head;
}
else{
node->next = m_head;
m_head->prev = node;
m_head = std::move(node);
}
length++;
}
void addBack(T data){
std::shared_ptr<Node> node = std::make_shared<Node>(data);
if(length == 0){
m_head = std::move(node);
m_tail = m_head;
}
else{
node->prev = m_tail.lock();
node->prev.lock()->next = std::move(node);
m_tail = m_tail.lock()->next;
}
length++;
}
void addAtPosition(T data , std::size_t pos){
if(pos == 0){
addFront(data);
}
else if(pos == length){
addBack(data);
}
else if(pos < 0 || pos >= length) {
throw("Invalid position");
}
else{
std::shared_ptr<Node> node = std::make_shared<Node>(data);
std::weak_ptr<Node> temp = m_head;
for(int cnt = 0; cnt < pos ; cnt++){
temp = temp.lock()->next;
}
node->next = temp.lock();
node->prev = node->next->prev;
node->prev = std::move(node);
length++;
}
}
void printNodes(void){
std::weak_ptr<Node> wp = m_head;
for(int i = 0; i < length; i++) {
auto& sp = *(wp.lock());
cout << sp.key;
wp = sp.next;
}
}
};
int main(){
std::unique_ptr<DLL<int>> m_list = std::make_unique<DLL<int>>();
for(int i = 0; i < 10 ; i++)
{
try{
m_list->addAtPosition(i,i);
}
catch(const char* mess){
cout << i <<' '<<mess << '\n';
}
}
m_list->printNodes();
return 0;
}
PS : 입력을 기반으로 난 내 코드를 편집 한하고 지금 작업,하지만 여전히 내가 내 방법이 너무 많이하고있는 느낌 최적화의 범위가 있습니다. 누군가 스마트 포인터를 사용하여 내 코드를 최적화하는 데 도움을 줄 수 있습니까? 또한 DLL을 구현하려고하지 않고 새로운 스마트 포인터를 사용하여 실제 느낌을 얻기에 충분한 코드를 작성했습니다.
스마트 포인터를 사용하는 이중 연결 목록은 명백한 눈부신 근본 문제 인 순환 참조가 있습니다. Google에이 사실을 알리고 문제가 무엇인지 깨닫기 전에는 읽기 시작해야합니다. –
대개 스마트 포인터가 도움이되지만 [rule of 5] (https://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11)에도 문제가있을 수 있습니다. 그것으로. –
STL (Standard Template Library)과 어떤 관련이 있습니까? – curiousguy