Circular linked list program in C
A linked list is a term where two-node or Element similarly connected with each other that is called Linked List, but when the Linked list repeated this task as Ring (Singly Linked List and Doubly Linked List) that is known as Circular linked list.
In other words when a Linked list connects one time that is a Singly Linked list, and when repeat twice at a time that is Doubly Linked List. But when Singly and Doubly Meet each other then he Made a Circular Linked list.
Circular linked list Algorithm
Before learning or Understanding how actually work Circular linked list in C Program. First, you need to learn the given code, Here you will learn how the Circular linked list works for printing the Data.
What is circular linked list with example
#include <stdio.h> struct node { int data; struct node *next; }; int main() { struct node n1,n2,n3, *head; n1.data=10; n1.next=&n2; n2.data=20; n2.next=&n3; n3.data=30; n3.next=&n1; head=n3.next; do { printf("%d-->",head -> data); head = head -> next; } while(head != n3.next); return 0; }
The output of Circular Linked List Program
After seeing the output of this program I hope you understand. Let’s Learn how a circular linked list takes input in C using Struct.
Circular linked list in C
#include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 2 typedef struct node { int data; struct node* next; }node; node* get_node(){ node* newNode = (node*)malloc(sizeof(node)); newNode->next = NULL; return newNode; } node* create(node *head){ node *temp , *New; int val, flag = TRUE; char ans = 'y'; do{ printf("\nEnter the element : \n"); scanf("%d", &val); if(head==NULL) { // Creating a node dynamically. temp =get_node(); // Assigning the data. temp -> data = val; head = temp; // Creating the link. head -> next = head; } else{ temp =get_node(); temp -> data = val; temp -> next = head -> next; head -> next = temp; } printf("You want to add more element(y/n) : "); scanf("%s", &ans); }while(ans == 'y' || ans == 'Y'); return head; } void display(node* head){ struct node *p; // If list is empty, return. if (head == NULL) { printf("List is empty.\n"); return; } // Pointing to first Node of the list. p = head-> next; // Traversing the list. do { printf("%d ", p -> data); p = p -> next; } while(p != head->next); } int main(){ node *head=NULL; head = create(head); display(head); return 0; }
Enter the element : 5
You want to add more element(y/n) : y
Enter the element : 6
You want to add more element(y/n) : n
6 5
Note:- ♥ We input in Orange Color. and get ♥ output in Green color.
Now we learn how to delete the Circular linked list. We also understand via the C program. clickHere.