# C program to create and traverse Circular linked list

Write a C program to create a circular linked list of n nodes and traverse the list. How to create a circular linked list of n nodes and display all elements of the list in C. Algorithm to create and traverse a Circular linked list.

## Required knowledge

Basic C programming, Function, Dynamic memory allocation, Circular linked list

## Algorithm to create Circular linked list

Algorithm to create circular linked list
%%Input : N {Total number of nodes to be created}
Being:
For count ← 2 to N do
alloc (newNode)
newNode.nextNULL;
prevNode.nextnewNode;
prevNodenewNode;
End for
End

## Algorithm to traverse Circular linked list

Algorithm to traverse or display a circular linked list
%%Input : head {Pointer to the first node of the list}
Begin:
write ('List is empty')
Else then
Do
write ('Data =', current.data)
currentcurrent.next;
End if
End

## Steps to create circular linked list

The creation steps of a circular linked list is almost similar as of singly linked list. Circular linked list only differs only differs in the last stage of its creation as of singly linked list.

1. Create a head node and assign some data to its data field.

2. Make sure that the next pointer field of head node should point to NULL.

3. Now head node has been created that points to the first node of the list. Lets take another pointer that also points to the first node say prevNode pointer.

4. Create a newNode and assign some more value to its data field and also do sure that next pointer field of newNode should point to NULL.

5. Now connect the previous node with newly created node i.e. connect the next pointer field of prevNode with the newNode. So that next pointer field of prevNode points to newNode.

6. Move the prevNode ahead i.e. prevNode should point to newNode which can be done as prevNode = prevNode.next.

7. Repeat step 4-6 till N (where N is the total number of nodes to be created).

8. After all nodes have been created. Now at final stage we have to connect the last node with the first node in order to make it circular. Therefore, now connect the next pointer field of prevNode with the head node i.e. prevNode.next = head (Since prevNode points to last node of the list) and you are done.

## Program to create and traverse Circular linked list

/**
* C program to create and traverse Circular Linked List
*/

#include <stdio.h>
#include <stdlib.h>

/*
* Basic structure of Node
*/
struct node {
int data;
struct node * next;

/*
* Functions used in this program
*/
void createList(int n);
void displayList();

int main()
{
int n, data, choice=1;

/*
* Run forever until user chooses 0
*/
while(choice != 0)
{
printf("============================================\n");
printf("============================================\n");
printf("1. Create List\n");
printf("2. Display list\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");

scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter the total number of nodes in list: ");
scanf("%d", &n);
createList(n);
break;
case 2:
displayList();
break;
case 0:
break;
default:
printf("Error! Invalid choice. Please choose between 0-2");
}

printf("\n\n\n\n\n");
}

return 0;
}

/**
* Creates a circular linked list of n nodes.
* @n Number of nodes to be created
*/
void createList(int n)
{
int i, data;
struct node *prevNode, *newNode;

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

printf("Enter data of 1 node: ");
scanf("%d", &data);

/*
* Creates and links rest of the n-1 nodes
*/
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
prevNode->next = newNode;

// Move the previous node ahead
prevNode = newNode;
}

// Link the last node with first node

}
}

/**
* Display the content of the list
*/
void displayList()
{
struct node *current;
int n = 1;

{
printf("List is empty.\n");
}
else
{
printf("DATA IN THE LIST:\n");

do {
printf("Data %d = %d\n", n, current->data);

current = current->next;
n++;
}
}

Output

============================================
============================================
1. Create List
2. Display list
0. Exit
--------------------------------------------
Enter the total number of nodes in list: 5
Enter data of 1 node: 10
Enter data of 2 node: 20
Enter data of 3 node: 30
Enter data of 4 node: 40
Enter data of 5 node: 50

============================================
============================================
1. Create List
2. Display list
0. Exit
--------------------------------------------
DATA IN THE LIST:
Data 1 = 10
Data 2 = 20
Data 3 = 30
Data 4 = 40
Data 5 = 50

============================================