Skip to main content

Circular Doubly Linked List

Circular Doubly Linked List


CODE

👇

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node
  4. {
  5. int info;
  6. struct node *next;
  7. struct node *prev;
  8. }NODE;

  9. void createlist(NODE *list);

  10. void display(NODE *list);

  11. void search(NODE *list);

  12. void insert(NODE *list, int num, int pos);

  13. void delpos(NODE *list, int pos);

  14. void delvalue(NODE *list, int num);

  15. void main()
  16. {
  17. int pos, num, n, no;
  18. NODE *list=(NODE*)malloc(sizeof(NODE));
  19. list->next=list;

  20. createlist(list);
  21. display(list);
  22. printf("\n");

  23. do
  24. {
  25. printf("\nOperation on Doubly Linked List:");
  26. printf("\n1. Insert:");
  27. printf("\n2. Display");
  28. printf("\n3. Search");
  29. printf("\n4. Delete by position");
  30. printf("\n5. Delete by value");
  31. printf("\n6. Exit\n");
  32. printf("\n Enter your choice:");
  33. scanf("%d", &no);

  34. switch(no)
  35. {
  36. case 1:  printf(" Enter the node data:");
  37. scanf("%d", &num);
  38. printf("Enter the node position to insert node:");
  39. scanf("%d", &pos);
  40. insert(list, num, pos);
  41. display(list);
  42. printf("\n");
  43. break;

  44. case 2:  printf(">>Circular Doubly Linked List<<\n");
  45. display(list);
  46. printf("\n");
  47. break;

  48. case 3:  search(list);
  49. display(list);
  50. printf("\n");
  51. break;

  52. case 4:  printf("Enter node position to be delete that node:");
  53. scanf("%d", &pos);
  54. delpos(list, pos);
  55. display(list);
  56. printf("\n");
  57. break;

  58. case 5:  printf("Enter node data to be delete :");
  59. scanf("%d", &num);
  60. delvalue(list, num);
  61. display(list);
  62. printf("\n");
  63. break;

  64. case 6:  printf("End\n");
  65. break;
  66. default:
  67.         printf("\n>>Enter Valid Choice<<\n");
  68. }
  69. }
  70. while(no!=6);
  71. }

  72. void createlist(NODE *list)
  73. {
  74. int n, i;
  75. NODE *temp=list, *nn;
  76. printf("How Many Nodes you want to enter ?\n");
  77. scanf("%d", &n);
  78. for(i=1; i<=n; i++)
  79. {
  80. nn=(NODE*)malloc(sizeof(NODE));
  81. printf("\n Enter the node data:\t");
  82. scanf("%d", &nn->info);
  83. nn->next=list;
  84. temp->next=nn;
  85. temp->prev=temp;
  86. list->prev=nn;
  87. temp=nn;
  88. }
  89. }

  90. void display(NODE *list)
  91. {
  92. NODE *temp;
  93. for(temp=list->next; temp!=list; temp=temp->next)
  94. {
  95. printf("%d", temp->info);
  96. printf("--->");
  97. }
  98. }

  99. void search(NODE *list)
  100. {
  101. int num, flag=0;
  102. NODE *temp; 
  103. printf("Enter Element to search:\t");
  104. scanf("%d", &num);
  105. for(temp=list->next; temp!=list; temp=temp->next)
  106. {
  107. if(temp->info==num)
  108. {
  109. printf(">>%d is found<<\n", num);
  110. flag=1;
  111. exit;
  112. }
  113. }
  114. if(flag==0)
  115. printf(">>%d is not found<< \n", num);
  116. }

  117. void insert(NODE *list, int num, int pos)
  118. {
  119. NODE *nn, *temp=list, *temp1;
  120. int i;
  121. nn=(NODE*)malloc(sizeof(NODE));
  122. nn->info=num;

  123. for(i=1, temp=list; temp->next!=list && i<pos; i++)
  124. temp=temp->next;
  125. if(i<pos)
  126. {
  127. printf("Position is out range\n");
  128. return;
  129. }
  130. temp1=temp->next;
  131. nn->next=temp1;
  132. temp1->prev=nn;
  133. temp->next=nn;
  134. nn->prev=temp;
  135. }

  136. void delpos(NODE *list, int pos)
  137. {
  138. NODE *temp=list, *temp1, *temp2;
  139. int i;
  140. for(i=1,temp=list; temp->next!=list && i<pos; i++)
  141. temp=temp->next;
  142. if(i<pos || temp->next==list)
  143. {
  144. printf(">>Position is out of range<<\n");
  145. return;
  146. }
  147. temp1=temp->next;
  148. temp2=temp1->next;
  149. temp->next=temp2;
  150. temp2->prev=temp;
  151. free (temp1);
  152. }

  153. void delvalue(NODE *list, int num)
  154. {
  155. NODE *temp=list, *temp1, *temp2;
  156. if(list->info==num)
  157. {
  158. list=list->next;
  159. free (temp);
  160. return;
  161. exit;
  162. }
  163. for(temp=list; temp->next!=list; temp=temp->next)
  164. {
  165. if(temp->next->info==num)
  166. {
  167. temp1=temp->next;
  168. if(temp1->next==NULL)
  169. {
  170. temp->next=NULL;
  171. free (temp);
  172. return;
  173. exit;
  174. }
  175. else
  176. {
  177. temp2=temp1->next;
  178. temp->next=temp2;
  179. temp2->prev=temp;
  180. free (temp1);
  181. return;
  182. exit;
  183. }
  184. }
  185. }
  186. printf(">>Enter valid data<<\n");
  187. return;
  188. }


👉Execute👈


//Output

 /*

How Many Nodes you want to enter ?

5


 Enter the node data: 8


 Enter the node data: 1


 Enter the node data: 5


 Enter the node data: 6


 Enter the node data: 4

8--->1--->5--->6--->4--->


Operation on Doubly Linked List:

1. Insert:

2. Display

3. Search

4. Delete by position

5. Delete by value

6. Exit


 Enter your choice:1

 Enter the node data:9

Enter the node position to insert node:2

8--->9--->1--->5--->6--->4--->


Operation on Doubly Linked List:

1. Insert:

2. Display

3. Search

4. Delete by position

5. Delete by value

6. Exit


 Enter your choice:2

>>Circular Doubly Linked List<<

8--->9--->1--->5--->6--->4--->


Operation on Doubly Linked List:

1. Insert:

2. Display

3. Search

4. Delete by position

5. Delete by value

6. Exit


 Enter your choice:3

Enter Element to search: 1

1 is found 

8--->9--->1--->5--->6--->4--->


Operation on Doubly Linked List:

1. Insert:

2. Display

3. Search

4. Delete by position

5. Delete by value

6. Exit


 Enter your choice:4

Enter node position to be delete that node: 4

8--->9--->1--->6--->4--->


Operation on Doubly Linked List:

1. Insert:

2. Display

3. Search

4. Delete by position

5. Delete by value

6. Exit


 Enter your choice:5

Enter node data to be delete : 1

8--->9--->6--->4--->


Operation on Doubly Linked List:

1. Insert:

2. Display

3. Search

4. Delete by position

5. Delete by value

6. Exit


 Enter your choice:6

End

*/





                                                                                                                     //ThE ProFessoR

Comments