Data Structure

Circular Linked List in Data Structure

Circular Linked List in Data Structure using C

  • In this tutorial we will learn about the basic concepts of Circular Linked List in Data Structure using C.
  • Content covered in this tutorial includes the introduction of Circular Linked List, Creating and displaying a Circular Linked List.
  • This tutorial will be useful for the computer science students.

Frequently Asked Questions

Some frequently asked questions from the basic concepts of circular linked list are given below –

  • What is difference between  circular linked list and linear linked list ?
  • Give the representation of circular linked list.
  • What is circular linked list creation and traversal in c ?.
  • Write the code or function to traverse a circular linked list.
  • Write a Program in C to create and display a circular linked list.

Let’s start with the introduction of circular Linked List.

What is Circular Linked List ?

  • Circular Linked List in Data Structure is a sequence of nodes in which every node has link to its next node in the sequence and the last node has link to the first node in the sequence or list.
  • That means Circular Linked List is similar to the single linked list except that the last node points to the first node in the list.

circular linked list in data structure

Difference Between Singly Linked List and Circular Linked List

  • Difference between Singly Linked List and Circular Linked List in data structure is that in single linked list, every node points to its next node in the sequence and the last node points NULL.
  • But in circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list.
  • Note that there are no NULL values in the NEXT part of any of the nodes of circular linked list.

How to create a Circular Linked List in Data Structure ?

To create a Circular Linked List we have to follow the steps given below –

Step 1 – Define a Structure node which consists of two members int type data to store data value of node and a node type next pointer to store the address of next node in linked list.

Syntax of node structure is given below

struct node

{

int data;

struct node * next;

};

Step 2 – Declare a head pointer of node type

struct node * head;

Step 3- Declare two functions to create and display.

void createList(int n);

void displayList();

Step 4- Define function createList(int n) to create circular linked list of n nodes. To create a circular linked list at first we will create the first node separately and initially assign lastnode as firstnode.

As soon as newnode will be created and added in the end of Circular linked list then reference of lastnode will be change.

To create remaining (n-1) node we used loop . Code for createList(int n) function is written in the program.

Step 5- We will define a function display in which we traverse the circular linked list from first node to last node and will print the data value of each node.

To traverse a circular linked list at first we will declare a node pointer temp and initialize it with head and in order to traverse the the list we will use do-while loop. .

do {

            printf(“%d\t”, temp ->data);

            temp = temp->next;

        }while(temp!= head);

The loop will execute until temp!=head is reached in the last because we have to reach from first node to last node and last node of circular linked list is connected to firstnode it means head.

Step 6- In order to execute the code defined in both functions we will call them inside the main() function as shown in the program.

Program – C Program to create and traverse the circular linked list in data structure is given below

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node * next;

};

struct node *head;

void createList(int n);

void displayList();

int main()

{

int numnodes;

printf(“enter  the number of nodes”);

scanf(“%d”, &numnodes);

createList(numnodes);

displayList();

return 0;

}

/** Creates a circular linked list of n nodes */

void createList(int n)

{

int i, data;

struct node *firstnode,*lastnode, *newNode;

firstnode = (struct node *)malloc(sizeof(struct node));

head=firstnode;

printf(“Enter data of 1 node: “);

scanf(“%d”, &data);

firstnode->data = data;

firstnode->next = NULL;

lastnode = firstnode;

/ * Creates and links rest of the n-1 node  */

for(i=2; i<=n; i++)

{

newNode = (struct node *)malloc(sizeof(struct node)) ;

printf(“Enter data of %d node: “, i);

scanf(“%d”, &data);

newNode->data = data;

newNode->next = NULL;

// Link the previous node with newly created node

lastnode->next = newNode;

// Move the previous node ahead

lastnode = newNode;

}

// when Linked List is created with n nodes then to make it circular Link // the last node with first node

lastnode->next=head;

printf(“\nCIRCULAR LINKED LIST CREATED SUCCESSFULLY\n”);

}

/**

* Display the content of the list

*/

void displayList()

{

struct node *temp;

if(head == NULL)

{

printf(“List is empty.\n”);

}

else

{

temp = head;

printf(“Linked List is\n”);

do {

            printf(“%d\t”, temp ->data);

            temp = temp->next;

        }while(temp!= head);

}

}

OUTPUT

enter  the number of nodes

4

Enter data of 1 node: 21

Enter data of 2 node: 22

Enter data of 3 node: 23

Enter data of 4 node: 24

CIRCULAR LINKED LIST CREATED SUCCESSFULLY

Linked List is

21      22      23      24

Conclusion and Summary

  • In this tutorial we have discussed the concept of Circular Linked List in Data Structure using C .
  • Circular Linked List program in C to create and define a circular linked list is also explained.

 

Next Tutorial – Insertion at Beginning of Circular Linked List 

Leave a Reply

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