Deletion in Circular linked list: Beginning and End

C Program to Delete a node in circular linked list at Beginning

#include<stdio.h> 
#include<conio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
    };
struct node *head=NULL;
struct node* createNode(){
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
    return (newNode);
    }
 
void insertNode(){
	struct node *temp,*ptr;
	temp=createNode();
	printf("enter the data you want to insert:");
	scanf("%d",&temp->data);
	temp->next=NULL;
	if(head==NULL){
		head=temp;
		head->next=head;
	}
	else{
		ptr=head;
		while(ptr->next!=head){
			ptr=ptr->next;
		}
	       ptr->next=temp;
	       temp->next=head;
	     }
    }
 
void deleteNodeAtBegin(){
    if(head==NULL){
		printf("list is empty");
	}
	else{
		struct node* temp=head;
        struct node* ptr=head;
        while(ptr->next!=head){
		    ptr =ptr->next;
	    }
        head=head->next;
        ptr ->next=head;
        printf("\n deleted node with value %d",temp->data);
		free(temp);
	    }
    }
void viewList(){
    struct node* temp=head;
	if(temp==NULL){
		printf("list is empty");
	}
	else{
	    printf("Values of Cicular list \n");
		while(temp->next!=head)
		{
			printf("%d\t",temp->data);
			temp=temp->next;
		}
		printf("%d \t",temp->data);
	}
}
 
int menu(){
    int choice;
    printf("\n 1.Add value to the list");
    printf("\n 2.Delete node at Begining");
    printf("\n 3.Travesre/View List");
    printf("\n 4. exit");
    printf("\n Please enter your choice: \t");
    scanf("%d",&choice);
    return(choice);
}
void main(){
    while(1){
        switch(menu()){
            case 1:
                insertNode();
                break;
            case 2:
                deleteNodeAtBegin();
                break;
            case 3:
                viewList();
                break;
            case 4:
                exit(0);
            default:
                printf("invalid choice");
        }
    }
    getch();
}

Output:

Deletion in circular linked list at beginning
Deletion in circular linked list at beginning

C Program to Delete a node in Circular linked list at End

#include<stdio.h> 
#include<conio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
    };
struct node *head=NULL;
struct node* createNode(){
    struct node *newNode = (struct node *)malloc(sizeof(struct node));
     
return (newNode);
    }
 
void insertNode(){
	struct node *temp,*ptr;
	temp=createNode();
	printf("enter the data you want to insert:");
	scanf("%d",&temp->data);
	temp->next=NULL;
	if(head==NULL){
		head=temp;
		head->next=head;
	}
	else{
		ptr=head;
		while(ptr->next!=head){
			ptr=ptr->next;
		}
	       ptr->next=temp;
	       temp->next=head;
	     }
    }
 
void deleteNodeAtEnd(){
	struct node *temp,*secondLast;
	if(head==NULL){
		printf("list is empty");
	}
	else{
		temp=head;
		secondLast=head;
		while(temp->next!=head){
			secondLast=temp;
			temp=temp->next;
		}
		if(temp==head){
			head=NULL;
		}
		else{
			secondLast->next=head;
		}
		free(temp);
	}
 }
void viewList(){
    struct node* temp=head;
	if(temp==NULL){
		printf("list is empty");
	}
	else{
	    printf("Values of Cicular list \n");
		while(temp->next!=head)
		{
			printf("%d\t",temp->data);
			temp=temp->next;
		}
		printf("%d \t",temp->data);
	}
}
 
int menu(){
    int choice;
    printf("\n 1.Add value to the list");
    printf("\n 2.Delete node at End");
    printf("\n 3.Travesre/View List");
    printf("\n 4. exit");
    printf("\n Please enter your choice: \t");
    scanf("%d",&choice);
    return(choice);
}
void main(){
    while(1){
        switch(menu()){
            case 1:
                insertNode();
                break;
            case 2:
                deleteNodeAtEnd();
                break;
            case 3:
                viewList();
                break;
            case 4:
                exit(0);
            default:
                printf("invalid choice");
        }
    }
        getch();
    }

Output: