fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. //Declaration of Circular LL
  4. struct CLLNode{
  5. int data;
  6. struct CLLNode* next;
  7. };
  8.  
  9. //Counting Nodes in a CLL
  10. int circularListLength(struct CLLNode* head){
  11. struct CLLNode* current =head;
  12. int count = 0;
  13. if(head==NULL){
  14. return 0;
  15. }else{
  16. do{
  17. current = current->next;
  18. count++;
  19. }while (current!=head);
  20. }
  21. return count;
  22.  
  23. }
  24. void push(struct CLLNode** head,struct CLLNode** tail,int data){
  25. struct CLLNode* newNode = (struct CLLNode*)malloc(sizeof(struct CLLNode));
  26. newNode->data = data;
  27. if(*head == NULL){
  28. *head = newNode;
  29. *tail = newNode;
  30. newNode->next = *head;
  31. return;
  32. }else{
  33. (*tail)->next = newNode;
  34. *tail = newNode;
  35. (*tail)->next = *head;
  36. return;
  37. }
  38. }
  39. //Prints the contents of CLL
  40. void printCLLData(struct CLLNode* head){
  41. struct CLLNode* current = head;
  42. if(head==NULL){
  43. return;
  44. // printf("List is empty!!!\n");
  45. }else{
  46. do{
  47. printf("%d ",current->data);
  48. current = current->next;
  49. }while (current!=head);
  50.  
  51. }
  52.  
  53. }
  54. int main(){
  55. struct CLLNode* head = NULL;
  56. struct CLLNode* tail = NULL;
  57.  
  58. push(&head,&tail,1);
  59. push(&head,&tail,2);
  60. push(&head,&tail,3);
  61. push(&head,&tail,4);
  62. push(&head,&tail,5);
  63. push(&head,&tail,6);
  64. printf("Circular linked list created:\n");
  65. printCLLData(head);
  66. printf("\nLenght of list:\n");
  67. circularListLength(head);
  68. return 0;
  69. }
Success #stdin #stdout 0s 5528KB
stdin
Standard input is empty
stdout
Circular linked list created:
1 2 3 4 5 6 
Lenght of list: