Linked List and its basic implementation

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.

Linked List Implementation

Here we are going to talk about a simple implementation of Linked List data structures in C++ along with some basic Linked List operations implementation such as insertion/deletion etc.
Let’s have a look on basic C++ class definition for Linked List.

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

Similarly we can define basic structure definition for Linked List.

struct linkedList
{
	int data;
	struct LinkedList* next;
}LinkedList;

For all Linked List implementation we are going to follow C++ Class definition here on.
Same logic can be applied on Linked List data structure definition.

Linked List Class Constructor/Destructor implementation

LinkedList::LinkedList (int data): data (data),
                                   next (NULL)
{
}

LinkedList::~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;
}

Linked List Operations Implementation

Leave a Reply

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