ALGORITHM

Initialization and declaration

#include <stdio.h>
#include <stdlib.h>
struct node   
{  
    int data;  
    struct node *next;   
};  
struct node *head;

Functions used

void beginsert ();   
void lastinsert ();    
void begin_delete();  
void last_delete();    
void display();

main and choice menu

void main ()  
{  
    int choice =0;  
    while(choice != 7)   
    {  
        printf("\\n*********Main Menu*********\\n");  
        printf("\\nChoose one option from the following list ...\\n");  
        printf("\\n===============================================\\n");  
        printf("\\n1.Insert in beginning\\n2.Insert at last\\n3.Delete from Beginning\\n4.Delete from last\\n5.Search for an element\\n6.Show\\n7.Exit\\n");  
        printf("\\nEnter your choice: ");         
        scanf("\\n%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            beginsert();
			display();    
            break;  
            case 2:  
            lastinsert();
			display();        
            break;  
            case 3:  
            begin_delete();
			display();      
            break;  
            case 4:  
            last_delete();
			display();        
            break;  
            case 5:  
            search();
			display();        
            break;  
            case 6:  
            display();        
            break;  
            case 7:  
            exit(0);  
            break;  
            default:  
            printf("Please enter valid choice..");  
        }  
    }  
}

Insertion Functions

Insert at beginning

void beginsert()  
{  
    struct node *ptr,*temp;   
    int item;   
    ptr = (struct node *)malloc(sizeof(struct node));  
    if(ptr == NULL)  
    {  
        printf("\\nOVERFLOW");  
    }  
    else   
    {  
        printf("\\nEnter the node data: ");  
        scanf("%d",&item);  
        ptr -> data = item;  
        if(head == NULL)  
        {  
            head = ptr;  
            ptr -> next = head;  
        }  
        else   
        {     
            temp = head;  
            while(temp->next != head)  
                temp = temp->next;  
            ptr->next = head;   
            temp -> next = ptr;   
            head = ptr;  
        }   
        printf("\\nnode inserted\\n");  
    }  
              
}

Insert at end of circular linked list

void lastinsert()  
{  
    struct node *ptr,*temp;   
    int item;  
    ptr = (struct node *)malloc(sizeof(struct node));  
    if(ptr == NULL)  
    {  
        printf("\\nOVERFLOW\\n");  
    }  
    else  
    {  
        printf("\\nEnter Data?");  
        scanf("%d",&item);  
        ptr->data = item;  
        if(head == NULL)  
        {  
            head = ptr;  
            ptr -> next = head;    
        }  
        else  
        {  
            temp = head;  
            while(temp -> next != head)  
            {  
                temp = temp -> next;  
            }  
            temp -> next = ptr;   
            ptr -> next = head;  
        }  
          
        printf("\\nnode inserted\\n");  
    }  
  
}

Deletion functions

Delete from beginning

void begin_delete()  
{  
    struct node *ptr;   
    if(head == NULL)  
    {  
        printf("\\nUNDERFLOW");    
    }  
    else if(head->next == head)  
    {  
        head = NULL;  
        free(head);  
        printf("\\nnode deleted\\n");  
    }  
      
    else  
    {   ptr = head;   
        while(ptr -> next != head)  
            ptr = ptr -> next;   
        ptr->next = head->next;  
        free(head);  
        head = ptr->next;  
        printf("\\nnode deleted\\n");  
  
    }  
}

Deleting the last node

void last_delete()  
{  
    struct node *ptr, *preptr;  
    if(head==NULL)  
    {  
        printf("\\nUNDERFLOW");  
    }  
    else if (head ->next == head)  
    {  
        head = NULL;  
        free(head);  
        printf("\\nnode deleted\\n");  
  
    }  
    else   
    {  
        ptr = head;  
        while(ptr ->next != head)  
        {  
            preptr=ptr;  
            ptr = ptr->next;  
        }  
        preptr->next = ptr -> next;  
        free(ptr);  
        printf("\\nnode deleted\\n");  
  
    }  
}

Traversal