# Circular Linked List Data Structure

## What is Circular Linked List?

Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.

### Singly Linked List as Circular

In singly linked list, the next pointer of the last node points to the first node.

### Doubly Linked List as Circular

In doubly linked list, the next pointer of the last node points to the first node and the previous pointer of the first node points to the last node making the circular in both directions.

As per the above illustration, following are the important points to be considered.

• The last link's next points to the first link of the list in both cases of singly as well as doubly linked list.

• The first link's previous points to the last of the list in case of doubly linked list.

## Basic Operations in Circular Linked List

Following are the important operations supported by a circular list.

• insert − Inserts an element at the start of the list.

• delete − Deletes an element from the start of the list.

• display − Displays the list.

## Circular Linked List - Insertion Operation

The insertion operation of a circular linked list only inserts the element at the start of the list. This differs from the usual singly and doubly linked lists as there is no particular starting and ending points in this list. The insertion is done either at the start or after a particular node (or a given position) in the list.

### Algorithm

1. START
2. Check if the list is empty
3. If the list is empty, add the node and point the head
to this node
4. If the list is not empty, link the existing head as
the next node to the new node.
5. Make the new node as the new head.
6. END


### Example

Following are the implementations of this operation in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {

//point it to old first node

//point first to new first node
}
}

//display the list
void printList(){
printf("\n[ ");

//start from the beginning
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}
printf(" ]");
}
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

//print list
printList();
}


### Output

Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}
//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {

//point it to old first node

//point first to new first node
}
}

//display the list
void printList(){
printf("\n[ ");

//start from the beginning
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}
printf(" ]");
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

//print list
printList();
return 0;
}


### Output

Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]

//Java program for circular link list
import java.util.*;
class Node {
int data;
int key;
Node next;
}
public class Main {
static Node current = null;
static boolean isEmpty() {
}
//insert link at the first location
static void insertFirst(int key, int data) {
if (isEmpty()) {
} else {
//point it to old first node
//point first to new first node
}
}
//display the list
static void printList() {
System.out.print("\n[ ");
//start from the beginning
while (ptr.next != ptr) {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.next;
}
}
System.out.print(" ]");
}
public static void main(String[] args) {
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
insertFirst(4, 1);
insertFirst(5, 40);
insertFirst(6, 56);
//print list
printList();
}
}


### Output

Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]

#python program for circular linked list
class Node:
def __init__(self, key, data):
self.key = key
self.data = data
self.next = None
current = None
def is_empty():
#insert link at the first location
def insert_first(key, data):
new_node = Node(key, data)
if is_empty():
else:
#point it to old first node
#point first to the new first node
#display the list
def print_list():
print("[", end=" ")
#start from the beginning
while ptr.next != ptr:
print("({}, {})".format(ptr.key, ptr.data), end=" ")
ptr = ptr.next
print("]")
insert_first(1, 10)
insert_first(2, 20)
insert_first(3, 30)
insert_first(4, 1)
insert_first(5, 40)
insert_first(6, 56)
#printlist
print_list()


### Output

Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]


## Circular Linked List - Deletion Operation

The Deletion operation in a Circular linked list removes a certain node from the list. The deletion operation in this type of lists can be done at the beginning, or a given position, or at the ending.

### Algorithm

1. START
2. If the list is empty, then the program is returned.
3. If the list is not empty, we traverse the list using a
current pointer that is set to the head pointer and create
another pointer previous that points to the last node.
4. Suppose the list has only one node, the node is deleted
by setting the head pointer to NULL.
5. If the list has more than one node and the first node is to
be deleted, the head is set to the next node and the previous
6. If the node to be deleted is the last node, link the preceding
node of the last node to head node.
7. If the node is neither first nor last, remove the node by
linking its preceding node to its succeeding node.
8. END


### Example

Following are the implementations of this operation in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {

//point it to old first node

//point first to new first node
}
}

//delete first item
struct node * deleteFirst(){

}

//mark next to first link as first

}

//display the list
void printList(){

//start from the beginning
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}
}
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

//print list
printList();
deleteFirst();
printf("\nList after deleting the first item: ");
printList();
}


### Output

Circular Linked List: (6,56) (5,40) (4,1) (3,30) (2,20)
List after deleting the first item: (5,40) (4,1) (3,30) (2,20)

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {

//point it to old first node

//point first to new first node
}
}

//delete first item
struct node * deleteFirst(){

}

//mark next to first link as first

}

//display the list
void printList(){

//start from the beginning
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

//print list
printList();
deleteFirst();
printf("\nList after deleting the first item: ");
printList();
return 0;
}


### Output

Circular Linked List: (6,56) (5,40) (4,1) (3,30) (2,20)
List after deleting the first item: (5,40) (4,1) (3,30) (2,20)

//Java program for circular linked list
import java.util.*;
public class Main {
static class Node {
int data;
int key;
Node next;
}
static Node current = null;

static boolean isEmpty() {
}
//insert link at the first location
static void insertFirst(int key, int data) {
if (isEmpty()) {
} else {
//point it to old first node
//point first to new first node
}
}
//delete first item
static Node deleteFirst() {
}
//mark next to first link as first
}
//display the list
static void printList() {
//start from the beginning
while (ptr.next != ptr) {
System.out.printf("(%d,%d) ", ptr.key, ptr.data);
ptr = ptr.next;
}
}
}
public static void main(String[] args) {
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
insertFirst(4, 1);
insertFirst(5, 40);
insertFirst(6, 56);
//print list
printList();
deleteFirst();
System.out.print("\nList after deleting the first item: ");
printList();
}
}


### Output

Circular Linked List: (6,56) (5,40) (4,1) (3,30) (2,20)
List after deleting the first item: (5,40) (4,1) (3,30) (2,20)

#python program for circular linked list
class Node:
def __init__(self, key, data):
self.key = key
self.data = data
self.next = None
current = None
def is_empty():
#insert link at the first location
def insert_first(key, data):
new_node = Node(key, data)
if is_empty():
else:
#point it to old first node
#point first to the new first node

def print_list():
print("[", end=" ")
#start from the beginning
while ptr.next != ptr:
print("({}, {})".format(ptr.key, ptr.data), end=" ")
ptr = ptr.next
print("]")
def delete_first():
insert_first(1, 10)
insert_first(2, 20)
insert_first(3, 30)
insert_first(4, 1)
insert_first(5, 40)
insert_first(6, 56)
#printlist
print_list()
delete_first()
print("\nList after deleting the first item: ")
print_list();


### Output

Circular Linked List:
[ (6, 56) (5, 40) (4, 1) (3, 30) (2, 20) ]

List after deleting the first item:
[ (5, 40) (4, 1) (3, 30) (2, 20) ]


## Circular Linked List - Displaying the List

The Display List operation visits every node in the list and prints them all in the output.

### Algorithm

1. START
2. Walk through all the nodes of the list and print them
3. END


### Example

Following are the implementations of this operation in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {

//point it to old first node

//point first to new first node
}
}

//display the list
void printList(){
printf("\n[ ");

//start from the beginning
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}
printf(" ]");
}
void main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

//print list
printList();
}


### Output

Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}

//insert link at the first location
void insertFirst(int key, int data){

struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {

//point it to old first node

//point first to new first node
}
}

//display the list
void printList(){
printf("\n[ ");

//start from the beginning
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}
printf(" ]");
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

//print list
printList();
return 0;
}


### Output

Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]

//Java program for circular link list
import java.util.*;
class Node {
int data;
int key;
Node next;
}
public class Main {
static Node current = null;
static boolean isEmpty() {
}
//insert link at the first location
static void insertFirst(int key, int data) {
if (isEmpty()) {
} else {
//point it to old first node
//point first to new first node
}
}
//display the list
static void printList() {
System.out.print("\n[ ");
//start from the beginning
while (ptr.next != ptr) {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.next;
}
}
System.out.print(" ]");
}
public static void main(String[] args) {
insertFirst(1, 10);
insertFirst(2, 20);
insertFirst(3, 30);
insertFirst(4, 1);
insertFirst(5, 40);
insertFirst(6, 56);
//print list
printList();
}
}


### Output

Circular Linked List:
[ (6,56) (5,40) (4,1) (3,30) (2,20) ]

#python program for circular linked list
class Node:
def __init__(self, key, data):
self.key = key
self.data = data
self.next = None
current = None
def is_empty():
#insert link at the first location
def insert_first(key, data):
new_node = Node(key, data)
if is_empty():
else:
#point it to old first node
#point first to the new first node
#display the list
def print_list():
print("[", end=" ")
#start from the beginning
while ptr.next != ptr:
print("({}, {})".format(ptr.key, ptr.data), end=" ")
ptr = ptr.next
print("]")
insert_first(1, 10)
insert_first(2, 20)
insert_first(3, 30)
insert_first(4, 1)
insert_first(5, 40)
insert_first(6, 56)
#printlist
print_list()


### Output

Circular Linked List: [ (6,56) (5,40) (4,1) (3,30) (2,20) ]


## Circular Linked List - Complete Implementation

Following are the complete implementations of Circular Linked List in various programming languages −

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}
int length(){
int length = 0;
//if list is empty
return 0;
}
length++;
current = current->next;
}
return length;
}
//insert link at the first location
void insertFirst(int key, int data){
struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {
//point it to old first node

//point first to new first node
}
}
//delete first item
struct node * deleteFirst(){

}
//mark next to first link as first

}
//display the list
void printList(){
printf("\n[ ");
//start from the beginning
while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}
printf(" ]");
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printf("Original List: ");
//print list
printList();
while(!isEmpty()) {
struct node *temp = deleteFirst();
printf("\nDeleted value:");
printf("(%d,%d) ",temp->key,temp->data);
}
printf("\nList after deleting all items: ");
printList();
}


### Output

Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
Deleted value:(6,56)
Deleted value:(5,40)
Deleted value:(4,1)
Deleted value:(3,30)
Deleted value:(2,20)
Deleted value:(1,10)
List after deleting all items:
[  ]

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
using namespace std;
struct node {
int data;
int key;
struct node *next;
};
struct node *current = NULL;
bool isEmpty(){
}
int length(){
int length = 0;

//if list is empty
return 0;
}
length++;
current = current->next;
}
return length;
}
//insert link at the first location
void insertFirst(int key, int data){
struct node *link = (struct node*) malloc(sizeof(struct node));
if (isEmpty()) {
} else {
//point it to old first node

//point first to new first node
}
}
//delete first item
struct node * deleteFirst(){
}
//mark next to first link as first

}
//display the list
void printList(){
cout << "\n[ ";
//start from the beginning
while(ptr->next != ptr) {
cout << "(" << ptr->key << "," << ptr->data << ") ";
ptr = ptr->next;
}
}
cout << " ]";
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
cout << "Original List: ";
//print list
printList();
while(!isEmpty()) {
struct node *temp = deleteFirst();
cout << "\n Deleted value:";
cout << "(" << temp->key << "," << temp->data << ") ";
}
cout << "\n List after deleting all items: ";
printList();
return 0;
}


### Output

Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
Deleted value:(6,56)
Deleted value:(5,40)
Deleted value:(4,1)
Deleted value:(3,30)
Deleted value:(2,20)
Deleted value:(1,10)
List after deleting all items:
[  ]

class Node {
int data;
int key;
Node next;
Node(int key, int data) {
this.key = key;
this.data = data;
this.next = null;
}
}
private Node current;
boolean isEmpty() {
}
int length() {
int length = 0;
//if list is empty
return 0;
}
length++;
current = current.next;
}
return length;
}
//insert link at the first location
void insertFirst(int key, int data) {
Node link = new Node(key, data);
if (isEmpty()) {
} else {
//point it to old first node
//point first to new first node
}
}
//delete first item
Node deleteFirst() {
}
//mark next to first link as first
}
//display the list
void printList() {
System.out.print("\n[ ");

//start from the beginning
while (ptr.next != ptr) {
System.out.print("(" + ptr.key + "," + ptr.data + ") ");
ptr = ptr.next;
}
}
System.out.print(" ]");
}
public static void main(String[] args) {
System.out.print("Original List: ");
//print list
System.out.println("\nDeleted value: (" + temp.key + "," + temp.data + ")");
}
System.out.print("\nList after deleting all items: ");
}
}


### Output

Original List:
[ (6,56) (5,40) (4,1) (3,30) (2,20)  ]
Deleted value: (6,56)

Deleted value: (5,40)

Deleted value: (4,1)

Deleted value: (3,30)

Deleted value: (2,20)

Deleted value: (1,10)

List after deleting all items:
[  ]

class Node:
def __init__(self, key, data):
self.key = key
self.data = data
self.next = None
def __init__(self):
self.current = None
def is_empty(self):
def length(self):
length = 0
# If list is empty
return 0
length += 1
self.current = self.current.next
return length
# insert link at the first location
def insert_first(self, key, data):
new_node = Node(key, data)
if self.is_empty():
else:
# point it to old first node
# point first to new first node
# delete first item
def delete_first(self):
# save reference to first link
# mark next to first link as first
# Diplay the list
def print_list(self):
print("[", end=" ")
# start from the beginning
while ptr.next != ptr:
print("({}, {})".format(ptr.key, ptr.data), end=" ")
ptr = ptr.next
print("]")
# Main function
if __name__ == '__main__':
print("Original List: ", end="")
print("\nDeleted value: ({}, {})".format(temp.key, temp.data))
# print list
print("List after deleting all items: ", end="")


### Output

Original List: [ (6, 56) (5, 40) (4, 1) (3, 30) (2, 20) ]

Deleted value: (6, 56)

Deleted value: (5, 40)

Deleted value: (4, 1)

Deleted value: (3, 30)

Deleted value: (2, 20)Deleted value: (1, 10)
List after deleting all items: [ ]