Ads by ProfitSence

Linked list with deep copy constructor C++

Last Updated on Wednesday 5th Oct 2022

Copy Constructor Linked List C++

Prefer nullptr over NULL.

  • Unless you really need to support older compilers, you're generally better off using nullptr in new code.
  • If you do use nullptr, you can still support older compilers if really necessary.
  • It's basically just a class that provides conversions for pointer to T and pointer to member.

Linked List Copy Constructor

  • When you copy a container such as a linked list, you probably want a deep copy, so new nodes need to be created and only the data copied over.
  • The next and prior pointers in the nodes of the new list should refer to new nodes you create specifically for that list and not the nodes from the original list.
  • These new nodes would have copies of the corresponding data from the original list, so that the new list can be considered a by value, or deep copy.
					node = head              # start at the head.
while node != null:      # traverse entire list.
    temp = node          # save node pointer.
    node =     # advance to next.
    free temp            # free the saved one.
head = null              # finally, mark as empty list.


Copy Constructor for Linked List

The basic idea is to remember the node to free in a separate variable then advance to the next before freeing it.

You only need to remember one node at a time, not the entire list as y

					Node(T val, Node *nxt=NULL) {
    value = val;
    next = nxt;


Copy Constructor C++ Linked List

					#include <iostream>
#include <memory>

template <class T>
class LinkedList {
    struct Node {
        T value;
        std::unique_ptr<Node> next;

        Node(const T& val, std::unique_ptr<Node>&& nxt=nullptr) {
            value = val;
            next = std::move(nxt);

        Node(T&& val, std::unique_ptr<Node>&& nxt=nullptr) {
            value = std::move(val);
            next = std::move(nxt);

std::unique_ptr<Node> head;
std::unique_ptr<Node>* tail = &head;

    LinkedList() = default;
    LinkedList(LinkedList& obj);
    LinkedList(LinkedList&& ) = default;
    ~LinkedList() = default;

    void add(const T& element);
    void add(T&& element);

    void displayList();

template<class T>
void LinkedList<T>::add(const T& element){
   *tail = std::make_unique<Node>(element);
   tail = &((**tail).next);

template<class T>
void LinkedList<T>::add(T&& element){
   *tail = std::make_unique<Node>(std::move(element));
   tail = &((**tail).next);

template<class T>
void LinkedList<T>::displayList(){
   Node* node = head.get();
       std::cout << node->value;
       if (node->next) std::cout << ", ";
       node = node->next.get();
   std::cout << std::endl;

template<class T>
LinkedList<T>::LinkedList(LinkedList& obj){
   Node* node = obj.head.get();
       *tail = std::make_unique<Node>(node->value);
       node = node->next.get();
       tail = &((**tail).next);



linked list copy constructor c++
copy constructor for linked list c++
how to make a copy constructor for a linked list c++
copy constructor linked list
linkedlist copy constructor
deep copy constructor linked list c++
c++ copy constructor linked list
copy constructor for a linked list
linked list deep copy c++