C++ Copy Constructor for Singly Linked List

  1. C++ Linked List Data Structure
  2. Example Code of a Singly Linked List
  3. C++ Copy Constructor of a Singly Linked List

This article will first discuss the concept of a Linked List data structure and suitable scenarios for using it. Then, we will discuss a compact implementation for the singly list and the copy constructor of the singly linked list using C++.

Telephone Directory Project In C++ ...
Telephone Directory Project In C++ with Source code Free Download 2020 | C++ Projects

C++ Linked List Data Structure

A Linked List data structure’s main advantage is its ability to store data in non-contiguous memory locations. It’s a linear data structure consisting of nodes, where each node stores the data item (key and related satellite data) and a reference to the next node.

A head pointer points to the first node in this data structure; therefore, it is sometimes called a head node. Some classic implementations of the singly-linked lists also have a tail pointer pointing to the last node of the list.

For example, the following figure shows an example depiction of a linked-list concept without a tail pointer.

linked list concept

The Linked List has three types:

  1. Singly Linked List or Simple Linked List

    It is a simple Linked List, also called a Singly Linked List, in which we can only move forward. Each node store the data item and the reference of the next node.

  2. Doubly Linked List

    It is a Linked List in which we can move forward and backward. Each node of the Doubly Linked List consists of the data item, the next node’s reference, and the previous node’s reference.

    It suites batter in do/redo states scenarios, including web browsers’ next page and previous navigations.

  3. Circular Linked List

    It is nearly the same as a Singly Linked List, except that the next pointer in the last node of the list always points to the first node. This creates a circle; therefore, it is called a Circular Linked List.

    It is usually handy in circular queue management systems.

Example Code of a Singly Linked List

Let’s look at the following C++ implementation of a Singly Linked List:

#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node* next;
};
class LinkedList{
  public:
  Node * head;

  LinkedList()
  {
      head=nullptr;
  }
  void addNewItem(int data)
  {
      Node * temp=new Node();
      temp->data = data;
      temp->next = head;
      head = temp;

  }
  void display()
  {
      Node * temp=head;
      while (temp!=nullptr)
      {
          cout<<temp->data<<",";
          temp=temp->next;
      }
      cout<<endl;
  }
};
int main() {
    LinkedList list;
    list.addNewItem(5);
    list.addNewItem(10);
    list.addNewItem(15);
    list.display();
    return 0;
}

The above code consists of the following classes:

  1. A class Node with two member variables: the data and a Node class pointer next. The data variable is used to store the element’s value, and the next variable is used to store the reference of the next element.
  2. A class LinkedList has a member variable head of type Node used to store the first element of the Linked List, a constructor LinkedList(), and two member functions addNewItem and display(). The LinkedList() constructor initializes the head with the null value.

The addNewItem method is used to add a new element at the front of the Linked List. We can also name this function AddToFront or push.

The display() method displays the Linked List elements.

C++ Copy Constructor of a Singly Linked List

The following is the code of Copy constructor of a Singly Linked List.

LinkedList( const LinkedList &obj )
{
    if (obj.head!=nullptr)
    {
        this->head=new Node;
        this->head->data=obj.head->data;
        Node* temp= obj.head->next;
        Node* temp2=this->head;
        while(temp!=nullptr)
        {
            Node * newNode=new Node; ////
            newNode->data=temp->data;////    creating new node
            newNode->next=temp->next;////
            temp2->next=newNode;
            temp2=temp2->next;
            temp=temp->next;
        }
    }
    else
    {
        head=nullptr;
    }
}

The above code shows the Singly Linked List copy constructor, which accepts a linked list object const LinkedList &obj as a parameter and copies all the data from the received Linked List to the calling Linked List.

As the Linked List uses the Node type pointer, we must define the copy constructor for the deep copy. A shallow copy will be created if the implementation for copy constructor is not provided.

Calling Copy Constructor

Consider the following code of the main() function:

int main() {

    LinkedList list; // Declaring a List
    list.addNewItem(5);
    list.addNewItem(10);
    list.addNewItem(15);
    cout<<"The content of List 1 are: ";
    list.display();

    LinkedList list2 (list); //Declaring a list2 with the copy constructor
    cout<<"The content of List 2 are: ";
    list2.display();
    return 0;
}

In the above code, a LinkedList type object list is created using the LinkedList list; statement. After this, we inserted three values into the list using the list.addNewItem( ) method.

Another object of list2 of type LinkedList is created, and we passed the first list object as a parameter at the time of declaration to call the copy constructor. The display() method of both objects shows the same data items in the list.

Output:

Singly Linked List Copy Constructor - Output

Related Article - C++ Constructor

  • Struct Constructors in C++
  • Copy Constructor of Linked List in C++
  • Empty Constructors in C++