C Program For Implementation of Circular Linked List

/* Program for implementation of circular Linked List with insertion, find, delete and display operations. */
# include <stdio.h>
# include <stdlib.h>
struct node
{
   int data;
   struct node *next;
};
typedef struct node node;
node *insert(node *p, int n)
{
   node *temp;
   /* if the existing list is empty then insert a new node as the starting node */
       if(p==NULL)
       {
         p=(node *)malloc(sizeof(node)); /* creates new node data value passes as parameter */
         p-> data = n;
         p-> next = p; /* makes the pointer pointing to itself because it is a circular list*/
       }
       else
       {
         temp = p;
         /* traverses the existing list to get the pointer to the last node of it */
         while (temp-> next != p)
                temp = temp-> next;
         temp-> next = (node *)malloc(sizeof(node));
         /* creates new node using data value passes as parameter and puts
         its address in the next field of last node of the existing list*/
            temp = temp-> next;
            temp-> data = n;
            temp-> next = p;
        }
   return (p);
}
   void printlist ( node *p )
   {
      node *temp;
      temp = p;
      printf("\nThe data values in the list are\n");
      if(p!= NULL)
      {
          do
          {
                    printf("%d\t",temp->data);
                    temp = temp->next;
          }
          while (temp!= p);
      }
     else
     printf("The list is empty\n");
      
}
node *removenode(node *list, int value)
{
    node *first = list, *last = list, *previous = list;

    if (list == NULL)
        {
        printf("The circularly linked list is already empty!\n");
        return;
        }
    //handle the first element of the list
    if (list->data == value)
        {
        //handle only one element in the list
        if (list->next == list)
            {
            free(list);
            return NULL;
            }
        //get the last element of the list
        while(last->next != first) last = last->next;
        //connect last element with 2nd element
        first = first->next;
        last->next = first;
        free(list);
        return first;
        }
    //handle the middle elements
    while (list->next != first)
        {
        if (list->data == value)
            {
            previous->next = list->next;
            free(list);
            return first;
            }
        previous = list;
        list = list->next;
        }
    //handle the last element
    if (list->data == value)
        {
        previous->next = first;
        free(list);
        return first;
        }
    else
        {
        //if the element is not found
        printf("The element %d has not been found in the list!\n", value);
        return first;
        }
}

void find(node *list, int value)
{
    node *first = list, *previous = list;
    if (list == NULL)
    {
    printf("The circularly linked list is empty!\n");
    return;
    }
    //handle the first element
    if (list->data == value)
        {
        printf("The %d is found at the beginning of the list before %d!\n", value,
         list->next->data);
        return;
        }
    //handle the middle elements
    while (list->next != first)
        {
        if (list->data == value)
            {
            printf("\n%d has been found between %d and %d\n", value, previous->data,
             list->next->data);
            return;
            }
        previous = list;
        list = list->next;
        }
    //handle the last element
    if (list->data == value)
        {
        printf("The \n%d is found at the end of the list after %d!\n", value, previous->data);
        return;
        }
    printf("\nThe element %d is not found in the list!\n");
    return;
}
   void main()
   {
      int n,j;
      int x;
      node *start = NULL ;
      printf("Enter the nodes to be created \n");
      scanf("%d",&n);
      while (n-- > 0 )
      {
         printf( "Enter the data values to be placed in a node\n");
         scanf("%d",&x);
         start = insert ( start, x );
      }
      printf("The created list is\n");
      printlist ( start );
      printf ( "\n enter the element to remove from Linked List\n" ) ;
      scanf("%d",&j);
      start = removenode(start,j);
      printlist ( start );
      printf ( "\n enter the element to find from Linked List\n" ) ;
      scanf("%d",&j);
      find(start,j);

   }
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment