Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Circular Linked List in C

Circular Linked list in C programming

What is circular linked list in C?

A circular linked list is a sequence of elements in which every element has a link to its next element in the sequence and the last element has a link to the first element this means that a circular linked list is a type of linked list in which last node does not point to null but it points to head i.e. first node of the list as it stores its  address in it. in this article we will learn more about circular linked list in C with a programming code showing the creation of circular linked list.

Why circular linked list?

As we know that singly linked list and doubly linked list are already present in there in data structure and algorithm then what is the need of circular linked list? So the answer to this question is very simple, we already know that last node of the list point the first node of the list this create a loop so circular linked list are need when we want to access the things in a loop.

Structure of circular linked list

Circular linked can be of both singly and doubly type. So they can be defined in a program by using following set of code.

  • Singly circular linked list is :-
    struct Node
    {
       int data;
       struct Node *next;
    }
  • Doubly circular linked list is:-
    struct node   
    {  
    struct node *prev;
    int data;
    struct node *next;  
    }   
Circular linked list in C

Advantages of circular linked list in C

  1. Any node of the list can act as head as list does not end.
  2. Access of first node from last node is easy and time saving.
  3. Queues can be easily implemented with circular linked list.
  4. We can traverse whole list from any node.

Operation on circular linked list

We can perform following operations on a circular linked list.

  • Insertion
    • Inserting at Beginning of the list.
    • Inserting at End of the list.
    • Inserting at Specific location in the list.
  • Deletion
    • Deleting from Beginning of the list.
    • Deleting from End of the list.
    • Deleting a Specific Node from the list
#include <stdio.h>
#include <stdlib.h>

struct node {
    int num;
    struct node * nextptr;
}*startnode;
 

void creation(int n);
void display();

int main() //main method
{
    int n;
    startnode = NULL;

    printf("Enter the number of nodes : ");
    scanf("%d", &n);
 
    creation(n); 
    display();
    return 0;
}

void creation(int n)//function to create list
{
    int i, num;
    struct node *preptr, *newnode;

    if(n >= 1)
    {
        startnode = (struct node *)malloc(sizeof(struct node));

        printf("Enter data for node 1 : ");
        scanf("%d", &num);
        startnode->num = num;
        startnode->nextptr = NULL;
        preptr = startnode;
        for(i=2; i<=n; i++)
        {
            newnode = (struct node *)malloc(sizeof(struct node));
            printf("Enter data for node %d : ", i);
            scanf("%d", &num);
            newnode->num = num;
            newnode->nextptr = NULL;	
            preptr->nextptr = newnode;	
            preptr = newnode;   		
        }
        preptr->nextptr = startnode; 		
    }
}

void display() //function to display list 
{
    struct node *tmp;
    int n,i;

    if(startnode == NULL)
    {
        printf("List is empty");
    }
    else
    {
        tmp = startnode;
        printf("\nData entered in the list are :\n");

      do {
            printf("Node %d : %d\n", n, tmp->num);

            tmp = tmp->nextptr;
            n++;
        }while(tmp != startnode);
    }
}
Output:
Enter the number of nodes : 5
Enter data for node 1 : 11
Enter data for node 2 : 22
Enter data for node 3 : 33
Enter data for node 4 : 44
Enter data for node 5 : 55

Data entered in the list are :
Node 0 : 11
Node 1 : 22
Node 2 : 33
Node 3 : 44
Node 4 : 55