The circular linked list and the other linked list whether single linked list or double linked list only have the difference that in doubly linked list the next position of the last node is NULL while in the circular linked list the last position of the next pointer is the address of the first node not NULL.
Single Linked List as Circular

Doubly Linked List as Circular

Basic Operation
-
insert − insert an element in the start of the list.
-
delete − insert an element from the start of the list.
-
display − display the list.
Insertion in circular linked list
While insertion you need to set the both node to the next and previous node and for the last node set the node next position to first node address
//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data= data;
if (isEmpty()) {
head = link;
head->next = head;
}else {
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
}
Deletion in circular linked list
//delete first item
struct node * deleteFirst() {
//save reference to first link
struct node *tempLink = head;
if(head->next == head){
head = NULL;
return tempLink;
}
//mark next to first link as first
head = head->next;
//return the deleted link
return tempLink;
}
0 Comment(s)