fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /* Basic node structure. */
  5. struct cl_node {
  6. int INFO;
  7. struct cl_node * NEXT;
  8. };
  9.  
  10. struct cl_node *FIRST = NULL;
  11. struct cl_node *LAST = NULL;
  12.  
  13. /* Functions declarations */
  14. void insert(int);
  15. void delete(int);
  16. void display(void);
  17. struct cl_node * search(int);
  18.  
  19. int main()
  20. {
  21. int num1, num2, choice;
  22.  
  23. struct cl_node *LOC = NULL;
  24.  
  25. /* Run forever until user chooses 0 */
  26. while(1)
  27. {
  28. printf("--------------------------------------------\n");
  29. printf(" CIRCULAR LINKED LIST PROGRAM \n");
  30. printf("--------------------------------------------\n");
  31. printf("1. Insert \n");
  32. printf("2. Delete \n");
  33. printf("3. Search\n");
  34. printf("4. Display\n");
  35. printf("*. Exit\n");
  36. printf("--------------------------------------------\n");
  37. printf("Enter your choice : ");
  38. scanf("%d", &choice);
  39. switch(choice)
  40. {
  41. case 1:
  42. printf("Enter element to be inserted: ");
  43. scanf("%d", &num1);
  44. insert(num1);
  45. break;
  46. case 2:
  47. printf("Enter key to delete from list: ");
  48. scanf("%d", &num2);
  49. delete(num2);
  50. break;
  51. case 3: printf("Enter key to be searched from list: ");
  52. scanf("%d", &num2);
  53. LOC=search(num2);
  54. if(LOC == NULL)
  55. printf("\nElement not found...!\n");
  56. else
  57. printf("\n Element found \n");
  58. break;
  59. case 4: display();
  60. break;
  61. default: return 0;
  62. } // end case
  63. } // end while
  64. }
  65.  
  66. void insert(int data)
  67. {
  68. struct cl_node *PTR =
  69. (struct cl_node*)malloc(sizeof(struct cl_node));
  70. PTR->INFO = data;
  71. if(FIRST == NULL) // no CLL
  72. {
  73. FIRST = LAST = PTR;
  74. PTR->NEXT = FIRST;
  75. }
  76. else
  77. {
  78. LAST->NEXT = PTR;
  79. PTR->NEXT = FIRST;
  80. LAST = PTR;
  81. }
  82. }
  83.  
  84. void delete(int key)
  85. {
  86. struct cl_node *LOC, *TEMP;
  87. LOC = search(key);
  88. if (LOC == NULL)
  89. {
  90. printf("\nElement not found...!\n");
  91. return;
  92. }
  93. if(LOC==FIRST)
  94. {
  95. if(FIRST==LAST) // only element
  96. {
  97. FIRST=LAST=NULL;
  98. free(LOC);
  99. return;
  100. }
  101. else
  102. {
  103. FIRST = FIRST->NEXT;
  104. LAST->NEXT = FIRST;
  105. free(LOC);
  106. return;
  107. }
  108. }
  109. for(TEMP=FIRST; TEMP->NEXT != LOC ; TEMP=TEMP->NEXT)
  110. ;
  111. if(LOC == LAST)
  112. {
  113. LAST = TEMP;
  114. TEMP->NEXT = FIRST;
  115. }
  116. else
  117. TEMP->NEXT = LOC->NEXT;
  118. free(LOC);
  119. return;
  120. }
  121.  
  122. struct cl_node * search(int key)
  123. {
  124. struct cl_node *PTR;
  125.  
  126. if(FIRST==NULL)
  127. return NULL;
  128. if(FIRST->INFO==key)
  129. return FIRST;
  130.  
  131. for(PTR=FIRST; PTR != LAST; PTR=PTR->NEXT)
  132. if(PTR->INFO == key)
  133. return(PTR);
  134. if(LAST->INFO == key)
  135. return(LAST);
  136. else
  137. return(NULL);
  138. }
  139.  
  140. void display()
  141. {
  142. struct cl_node *PTR;
  143. if(FIRST==NULL) // no element
  144. {
  145. printf("\n CIRCULAR Linked list is empty\n");
  146. return;
  147. }
  148.  
  149. if(FIRST == LAST) // Single element
  150. {
  151. printf("\nFIRST --> %d --> LAST\n", FIRST->INFO);
  152. return;
  153. }
  154.  
  155. for (PTR=FIRST; PTR !=LAST; PTR=PTR->NEXT)
  156. printf("%d --> ", PTR->INFO) ;
  157.  
  158. printf("%d --> LAST\n",LAST->INFO);
  159. }
  160.  
Success #stdin #stdout 0s 5640KB
stdin
Standard input is empty
stdout
--------------------------------------------
        CIRCULAR LINKED LIST PROGRAM        
--------------------------------------------
1. Insert 
2. Delete 
3. Search
4. Display
*. Exit
--------------------------------------------
Enter your choice :