Program to calculate length of a Linked List

Linked List is a data structure used for storing collection of data where successive elements are connected through pointers and the size of linked list can grow or shrink dynamically.
In short, Linked list can be thought as similar to array in which traversal can happen through pointers instead of indexes. Before going ahead have a look into Linked List Basics and Linked List Implementation.

Let’s have a look on basic class definition for Linked List.

class LinkedList
{
    public:
        int data;
        LinkedList* next;
        LinkedList (int data);
        ~LinkedList ();
};

/* Below function will print all nodes in a Linked List */
void print_linked_list (LinkedList* start)
{
    if (!start)
    {
        cout << "Linked list is null, nothing to print" << endl;
        return;
    }

    LinkedList* temp = start;
    while (temp)
    {
        cout << " " << temp -> data;
        temp = temp -> next;
    }
    cout << endl;
}

Length of a Linked List

Length of a Linked List is the total number of nodes present in the Linked List.
In order to calculate length of a Linked List we need to traverse the Linked list and keep on counting the node until we reach the end of the Linked List.

Using Iteration

Let’s look into the sample code.

int length_of_linked_list_using_iteration (LinkedList* start)
{
    LinkedList* temp = start;
    int count = 0;

    while (temp)
    {
        count++;
        temp = temp -> next;
    }
    return count;
}

Using Recursion

Let’s look into the sample code.

int length_of_linked_list_using_recursion (LinkedList* start)
{
    if (!start)
        return 0;
    return 1 + length_of_linked_list_using_recursion (start -> next);
}

Let’s use these functions in an example main functions to explain the usage of these functions.
Few of the functions used below are explained in Linked List Insert operation, Linked List Delete Operation and Linked List Traverse and Search article . Refer those before moving ahead.

int main()
{
    LinkedList* start = NULL;
    print_linked_list (start);
    insert_at_begining (&start, 3);
    insert_after_certain_element (start, 5, 6);
    insert_after_certain_element (start, 7, 5);
    insert_after_certain_element (start, 9, 8);
    insert_after_certain_element (start, 11, 10);
    insert_after_certain_element (start, 16, 11);
    insert_after_certain_element (start, 2, 9);
    insert_after_certain_element (start, 3, 2);
    cout << "Length of linked list: " << length_of_linked_list_using_iteration (start) << endl;
    cout << "Length of linked list: " << length_of_linked_list_using_recursion (start) << endl;
    int searched_data = 5;
    cout << "Searching element in linked list: " << searched_data << endl;
    search_element (start, searched_data);
    print_linked_list (start);
    cout << "Deleting Head Node " << endl;
    delete_start_node (&start);
    print_linked_list (start);
    cout << "Deleting Node 11" << endl;
    delete_searched_node (&start, 11);
    print_linked_list (start);
    cout << "Deleting all Nodes" << endl;
    delete_all_node (&start);
    print_linked_list (start);
    cout << "Length of linked list: " << length_of_linked_list_using_iteration (start) << endl;
    cout << "Length of linked list: " << length_of_linked_list_using_recursion (start) << endl;
}

Let’s analyze the output of this main function.

Linked list is null, nothing to print
Inserting node 5
Searched data not found, hence putting new node at the end
Inserting node 7
Inserting node 9
Searched data not found, hence putting new node at the end
Inserting node 11
Searched data not found, hence putting new node at the end
Inserting node 16
Inserting node 2
Inserting node 3
Length of linked list: 8
Length of linked list: 8
Searching element in linked list: 5
Found the searched data: 5
 3 5 7 9 2 3 11 16
Deleting Head Node 
 5 7 9 2 3 11 16
Deleting Node 11
Node found, going to delete
 5 7 9 2 3 16
Deleting all Nodes
Linked list is null, nothing to print
Length of linked list: 0
Length of linked list: 0

Leave a Reply

Your email address will not be published. Required fields are marked *