C Program to Convert Singly Linked List to Circular List

This C Program converts a given singly linked list to a circular list.

Here is source code of the C Program to convert a given singly linked list to a circular list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Convert a given Singly Linked List to a Circular List 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int num;
  10.     struct node *next;
  11. };
  12.  
  13. void create(struct node **);
  14. void tocircular(struct node **);
  15. void release(struct node **);
  16. void display(struct node *);
  17.  
  18. int main()
  19. {
  20.     struct node *p = NULL;
  21.     int result, count;
  22.  
  23.     printf("Enter data into the list\n");
  24.     create(&p);
  25.     tocircular(&p);
  26.     printf("Circular list generated\n");
  27.     display(p);
  28.     release (&p);
  29.  
  30.     return 0;
  31. }
  32.  
  33. void tocircular(struct node **p)
  34. {
  35.     struct node *rear;
  36.  
  37.     rear = *p;
  38.     while (rear->next != NULL)
  39.     {
  40.         rear = rear->next;
  41.     }
  42.     rear->next = *p;
  43.     /*After this the singly linked list is now circular*/
  44. }
  45.  
  46. void create(struct node **head)
  47. {
  48.     int c, ch;
  49.     struct node *temp;
  50.  
  51.     do
  52.     {
  53.         printf("Enter number: ");
  54.         scanf("%d", &c);
  55.         temp = (struct node *)malloc(sizeof(struct node));
  56.         temp->num = c;
  57.         temp->next = *head;
  58.         *head = temp;
  59.         printf("Do you wish to continue [1/0]: ");
  60.         scanf("%d", &ch);
  61.     } while (ch != 0);
  62.     printf("\n");
  63. }
  64.  
  65. void display(struct node *head)
  66. {
  67.     struct node *temp = head;
  68.     printf("Displaying the list elements\n");
  69.     printf("%d\t", temp->num);
  70.     temp = temp->next;
  71.     while (head != temp)
  72.     {
  73.         printf("%d\t", temp->num);
  74.         temp = temp->next;
  75.     }
  76.     printf("and back to %d\t%d ..\n", temp->num, temp->next->num);
  77. }
  78.  
  79. void release(struct node **head)
  80. {
  81.     struct node *temp = *head;
  82.     temp = temp->next;
  83.     (*head)->next = NULL;
  84.     (*head) = temp->next;
  85.     while ((*head) != NULL)
  86.     {
  87.         free(temp);
  88.         temp = *head;
  89.         (*head) = (*head)->next;
  90.     }
  91. }

$ cc single2circular.c 
$ ./a.out
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 0
 
Circular list generated
Displaying the list elements
5	4	3	2	1	and back to 5	4 ..

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.