Creating and Displaying Circular Linked List with Code

In this post, I am providing the code for creating and displaying circular linked list along with the complete explanation.

Code for creating and displaying Circular Singly Linked List

#include <stdio.h>

#include <stdlib.h>

struct node

{

int data;

struct node *next;

};

struct node *startList;

void createCircularLinkedList();

void displayCircularLinkedList();

int  main()

{

startList=NULL;

createCircularLinkedList();

displayCircularLinkedList();

return 0;

}

void createCircularLinkedList()

{

struct node *newNode,*temp;

char k=’y’;

while(k==’y’ || k==’Y’)

{

if(startList==NULL)

{

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

printf(“Enter an integer value:”);

scanf(“%d”,&newNode->data);

startList = newNode;

newNode->next=startList;

temp=startList;

}

else

{

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

printf(“Enter an integer value:”);

scanf(“%d”,&newNode->data);

temp->next = newNode;

newNode->next=startList;

temp=newNode;

}

printf(“Want to add more y/n?”);

scanf(” %c”,&k);

}

printf(“Linked list is created\n”);

}

void displayCircularLinkedList()

{

struct node *temp;

temp=startList;

if(temp !=NULL)

{

printf(“\nThe content in the circular linked list are :\n”);

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

temp=temp->next;

while(temp!=startList)

{

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

temp=temp->next;

/* pointer temp is set to point at the node where its next

pointer is pointing. That is, pointer temp is set to point at

its next successive node */

}

}

else

printf(“Circular Linked list is empty\n”);

}