As in previous tutorial we learned the basic of singly linked list as what is linked list and how it created now we will see singly circular linked list.
The singly linked list is the node which contain the data and address.

As linked list we seen that the address field container address of a next node, But at last (last node) instead of pointing NULL the last node point to the first node which form a circular linked list as shown below.

The last node points to first to form circular list.
Program singly circular linked list
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 | #include<stdio.h>#include<conio.h>struct node{int data;struct node *next;}struct node *start;void insert_beg(void);void insert_end(void);void insert_before(void);void display(void);void deletion(void);void destroy(void);void main(void){int ch;start=NULL;do{printf(“\n 1. insert_beg\n2. insert_end\n3. insert_before\n4. Display\n5. Deletion\n6.Destroy\n Choice: ”);scanf(“%d”,&ch);switch(ch){case 1:insert_beg();break;case 2:insert_end();break;case 3:insert_before();break;case 4:display();break;case 5:deletion();break;case 6:destroy();break;case 7:printf(“Program ends”)break;default:printf(“Wrong Choice:”)break;}}while(ch!=7);}void insert_beg(void){struct node *nn,*temp;nn=(strcut node *)malloc(sizeof(struct node));printf(“Enter data”);scanf(“%d”,&nn->data);if(start==NULL){nn->next=nn;start=nn;}else{temp=start;while(temp->next!=start){temp=temp->next;}temp->next=nn;nn->next=start;start=nn;}}void insert_end(void){struct node *nn ,*temp;nn=(strcut node *)malloc(sizeof(struct node));printf(“Enter data”);scanf(“%d”,&nn->data);if(start==NULL){nn->next=nn;start=nn;}else{temp=start;while(temp->next!=start){temp=temp->next;}temp->next=nn;nn->next=start;}}void display(void){struct node *temp;if(start==NULL){printf(“\n SCLL is empty”);return;}tempt=start;do{printf(“%d”,temp->data);temp=temp->next;}while(temp!=start);}void destroy(void){struct node *temp,*dp;if(start==NULL){printf(“\n SCLL is empty”);return;}temp=start;do{dp=temp;temp=temp->next;free(dp);}while(temp!=start)start=NULL;}void insert_before(void){struct node *nn ,*temp,*ptemp;int x;if(start==NULL){printf(“SCLL is empty”);return;}printf(“Enter data which you want to insert the node”);scanf(“%d”,&x);if(x==start->data){insert_beg();return;}temp=start;while(temp->data!=x){ptemp=temp;temp=temp->next;if(temp==start){printf(“%d does not exist:”,x);return;}}nn=(strcut node *)malloc(sizeof(struct node));printf(“Enter data”);scanf(“%d”,&nn->data);ptemp->next=nn;nn->next=temp;}void deletion(void){struct node *ptemp,*temp,*lp;int x;if(start==NULL){printf(“\n SCLL is empty”);return;}printf(“Enter data to delete”);sf(“%d”,&x);if(x==start->data){if(start==start->next){temp=start;start=NULL;free(temp);}else{lp=start;while(lp->next!=start){lp=lp->next;}temp=start;start=start->next;free(temp);lp->next=start;}return;}temp=start;while(temp->data!=x){ptemp=temp;temp=temp->next;if(temp==start){printf(“%d is does not exist”,x);return;}}ptemp->next=temp->next;free(temp);} |