• Register
100 points
5

Here, I will discuss circular linked list using C.A linked list is a data structure in which elements are not stored in contiguous memory location.Elemets or nodes are linked together by link .A node has two things :one is pointer which points next node and another one is data.Last node of linked list points to NULL.

Circular linked list is a type or variant of linked list in which last node points to first node and rest is same as single linked list.

Code

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

void linkedListTransversal(struct Node *head)
{   struct Node *ptr=head;
    do
    {
        printf("Element:%d\n", ptr->data);
        ptr = ptr->next;
    }while(ptr!=head);
}

int main()
{

    struct Node *head;
    struct Node *second;
    struct Node *third;
    struct Node *fourth;
    head = (struct Node *)malloc(sizeof(struct Node));
    second = (struct Node *)malloc(sizeof(struct Node));
    third = (struct Node *)malloc(sizeof(struct Node));
    fourth = (struct Node *)malloc(sizeof(struct Node));

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

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

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

    fourth->data = 40;
    fourth->next = head;

    linkedListTransversal(head);
    
    return 0;
}

 

100 points
5