Creation and Traversal of Linked list

Question -
To crate a linked list of length 4 using dynamic memory allocation and print all the element of list.

Implementations - 

/**********************************************
We create a linked list of length 4 with the
help of dynamic memory allocations.
print the all element of that Lined list
**********************************************/


#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int data;
    struct Node *next;
};

void Print_Linkedlist(struct Node* head)
{
    struct Node* p=head;
    while(p!=NULL){
        printf("%d ",p->data);
        p=p->next;
    }
    printf("\n");
}
int main()
{
    struct Node* head=(struct Node*)malloc(sizeof(struct Node));
    struct Node* second=(struct Node *)malloc(sizeof(struct Node));
    struct Node* third=(struct Node*)malloc(sizeof(struct Node));
    struct Node* fourth=(struct Node *)malloc(sizeof(struct Node));


    head->data=5;
    head->next=second;

    second->data=12;
    second->next=third;

    third->data=18;
    third->next=fourth;

    fourth->data=25;
    fourth->next=NULL;

    printf("Linked list elements are : ");
    Print_Linkedlist(head);


}

Comments