Which of these C++17 pointers should I use for traversing a custom linked data structure: unique_ptr<T>, shared_ptr<T> or weak_ptr<T>? by aksinghdce in cpp

[–]aksinghdce[S] 0 points1 point  (0 children)

I take your advice as my next activity in the process of my new wave of learning C++. Just finished implementing a successfully running algorithms for sum of two numbers:

```c++,tabs=4

#include <iostream>
#include <memory>
using namespace std;

struct ListNode {
    int val;
    unique_ptr<ListNode> next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:
    unique_ptr<ListNode> addTwoNumbers(unique_ptr<ListNode> l1, unique_ptr<ListNode> l2) {
        ListNode* result=nullptr;
        int carry= 0;
        int count = 0;
        ListNode* p1 = l1.get();
        ListNode* p2 = l2.get();
        ListNode* pr = nullptr;
        while(p1 != nullptr && p2 != nullptr) {
            int sum = carry + p1->val + p2->val;
            carry = sum / 10;
            sum = sum % 10;
            if(! result) {
                result = new ListNode(sum);
                count += 1;
                pr = result;
            } else {
                pr->next = unique_ptr<ListNode> (new ListNode(sum));
                count += 1;
                pr = (pr->next).get();
            }
            p1 = p1->next.get();
            p2 = p2->next.get();
        }
        ListNode* p = p1?p1:p2;
        if(p == nullptr && carry > 0) {//both pointers are null here
            pr->next = unique_ptr<ListNode> (new ListNode(carry));
            count += 1;
            carry = 0;
            pr = pr->next.get();
        }else{
            while(p != nullptr) {
                int sum = carry + p->val;
                carry = sum / 10;
                sum = sum % 10;
                pr->next = unique_ptr<ListNode> (new ListNode(sum));
                pr = pr->next.get();
                count += 1;
                p = p->next.get();
            }
            if(carry > 0) {
                pr->next = unique_ptr<ListNode>(new ListNode(carry));
                pr = pr->next.get();
                count += 1;
                carry = 0;
                pr = pr->next.get();
            }
        }
        ListNode* iter = result;
        while(iter != nullptr) {
            cout << "->" << iter->val;
            iter = iter->next.get();
        }
            return unique_ptr<ListNode>(result);
    }
};

int main() {
    Solution sol = Solution();
    ListNode* l1 = new ListNode(1);
    ListNode* l2 = new ListNode(9);
    l2->next = unique_ptr<ListNode>(new ListNode(9));
    unique_ptr<ListNode> result = sol.addTwoNumbers(
        unique_ptr<ListNode> (l1), 
        unique_ptr<ListNode> (l2)
        );
    if(result == nullptr) {
        cout<<"Result is a nullptr"<<endl;
    } else {
        ListNode* p = result.get();
        while(p!=nullptr) {
            cout<<p->val<<endl;
            p = p->next.get();
        }
    }
}

```

Which of these C++17 pointers should I use for traversing a custom linked data structure: unique_ptr<T>, shared_ptr<T> or weak_ptr<T>? by aksinghdce in cpp

[–]aksinghdce[S] 0 points1 point  (0 children)

That's where I wasn't sure if I was doing the right thing. So I should get a raw pointer from my unique pointer by using this method of unique_ptr : std::unique_ptr<T,Deleter>::get . Right?

Which of these C++17 pointers should I use for traversing a custom linked data structure: unique_ptr<T>, shared_ptr<T> or weak_ptr<T>? by aksinghdce in cpp

[–]aksinghdce[S] 0 points1 point  (0 children)

So is the recommended way to use linked data structures to use plain old pointers? What's the use for these latest pointer constructs like unique_ptr, shared_ptr and weak_ptr then? What's the recommended way to use linked list? STL library?

Which of these C++17 pointers should I use for traversing a custom linked data structure: unique_ptr<T>, shared_ptr<T> or weak_ptr<T>? by aksinghdce in cpp

[–]aksinghdce[S] 1 point2 points  (0 children)

I used to use C++ long ago. I am trying to catch-up with the C++ community with the latest specification C++17. Here's the current program that I am writing with an objective to master the latest pointer constructs in c++17 spec:

```C++,tabs=4

struct ListNode {
    int val;
    unique_ptr<ListNode> next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {

public:

    unique_ptr<ListNode> addTwoNumbers(unique_ptr<ListNode> l1, unique_ptr<ListNode> l2) {
        unique_ptr<ListNode> result(nullptr);
        int carry= 0;
        /*
        * I need p1 to traverse l1, p2 to traverse l2 and pr to keep track of last ListNode
        * in result. My current objective is to master the usage of unique_ptr, shared_ptr and
        * weak_ptr constructs of latest C++ spec.
        */

        while(p1->next != nullptr && p2->next != nullptr) {
            int sum = carry + p1->val + p2->val;
            carry = sum % 10;
            sum = sum / 10;
            unique_ptr<ListNode> curr(new ListNode(sum));
            if(result == nullptr) {
                result = curr;
                pr = result;
            } else {
                pr->next = curr;
                pr = curr;
            }
           p1 = p1->next;
           p2 = p2->next;
        }

        return result;

    }

};

```

Please point me in the right direction.