Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

 1 Answer(s)

  • Hi soulaimane123,

    A linked list is a linear collection of data elements, called nodes pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence. Here we can divide our node in two parts data part and address part. In data part we put our desired data and in address part we assign it the address of the next node. The last node's address part is assigned as NULL as their is no value to hold by it as there is no more nodes after that.
    The starting node is called head of linked list and last node is termed as tail. alt text

    Here is a simple program for linked list insertion and print

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    struct node {
            int data; // used to hold an integer value
            struct node *next; // used to hold the address of the next node
    typedef struct node node;
    void print(node *ptr)
            cout << "Inserted Linked list    : ";
            while (ptr->next) {
                    cout << ptr->data << "->";
                    ptr = ptr -> next;
            cout << ptr->data <<endl;
    int main()
            node *root = NULL, *tmp;
            int n;
            cout << "Enter the number of nodes in linked list : ";
            cin >> n; // here declareing the size of linked list
            if ( n > 0)
                    cout << "Enter the data elements : ";
            for ( int i = 0; i < n; i++) {
                    if (root == NULL) { 
                            root = (node *)malloc(sizeof(node)); 
                            cin >> root->data;
                            root->next = NULL;
                            tmp = root;
                    } else  {
                            node *ptr = (node *)malloc(sizeof(node));
                            cin >> ptr->data;
                            ptr->next = NULL;
                            tmp->next = ptr;
                            tmp = ptr;
            print(root); //print function call to print the data inside the linked list
            return 0;


    Enter the number of nodes in linked list : 7
    Enter the data elements : 2 3 5 1 2 3 4
    Inserted Linked list    : 2->3->5->1->2->3->4

    Hope may this help you, please feel free to ask questions and also go through this link

    Thanks :)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: