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(){
printf("Delete a node in circular linked list at Beginning");
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:
Delete a node in circular linked list at Beginning
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 1
enter the data you want to insert:4
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 1
enter the data you want to insert:3
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 1
enter the data you want to insert:3
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 1
enter the data you want to insert:7
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 3
Values of Cicular list
4 3 3 7
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 2
deleted node with value 4
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 3
Values of Cicular list
3 3 7
1.Add value to the list
2.Delete node at Begining
3.Travesre/View List
4.Exit
Please enter your choice: 4
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(){
printf("Delete a node in circular linked list at End");
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:
Delete a node in circular linked list at End
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 1
enter the data you want to insert:3
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 1
enter the data you want to insert:6
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 1
enter the data you want to insert:3
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 1
enter the data you want to insert:7
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 3
Values of Cicular list
3 6 3 7
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 2
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 3
Values of Cicular list
3 6 3
1.Add value to the list
2.Delete node at End
3.Travesre/View List
4. exit
Please enter your choice: 4
C Program to Delete a node in circular linked list at Given Location
#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 deleteNodeAtPosition(int position) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct node *temp = head, *prev = NULL;
int count = 1;
// Case 1: Delete head node
if (position == 1) {
int deletedData = head->data;
if (head->next == head) {
free(head);
head = NULL;
} else {
struct node *last = head;
while (last->next != head)
last = last->next;
last->next = head->next;
temp = head;
head = head->next;
free(temp);
}
printf("Deleted node value: %d\n", deletedData);
return;
}
// Traverse to the node at (position - 1)
while (count < position && temp->next != head) {
prev = temp;
temp = temp->next;
count++;
}
if (count != position) {
printf("Invalid position.\n");
return;
}
int deletedData = temp->data;
prev->next = temp->next;
free(temp);
printf("Deleted node value: %d\n", deletedData);
}
void viewList() {
struct node* temp = head;
if (temp == NULL) {
printf("List is empty.\n");
} else {
printf("Values of Circular 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\n***** MENU *****");
printf("\n 1. Add value to the list");
printf("\n 2. Delete node from given position");
printf("\n 3. Traverse/View List");
printf("\n 4. Exit");
printf("\n Please enter your choice: ");
scanf("%d", &choice);
return choice;
}
void main() {
printf("Circular Linked List Operations\n");
while (1) {
switch (menu()) {
case 1:
insertNode();
break;
case 2:{
int pos;
printf("Enter the position to delete: ");
scanf("%d", &pos);
deleteNodeAtPosition(pos);
break;
}
case 3:
viewList();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
getch();
}
Output
Circular Linked List Operations
***** MENU *****
1. Add value to the list
2. Delete node from given position
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter the data you want to insert: 6
***** MENU *****
1. Add value to the list
2. Delete node from given position
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter the data you want to insert: 3
***** MENU *****
1. Add value to the list
2. Delete node from given position
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter the data you want to insert: 4
***** MENU *****
1. Add value to the list
2. Delete node from given position
3. Traverse/View List
4. Exit
Please enter your choice: 1
Enter the data you want to insert: 8
***** MENU *****
1. Add value to the list
2. Delete node from given position
3. Traverse/View List
4. Exit
Please enter your choice: 3
Values of Circular list:
6 3 4 8
***** MENU *****
1. Add value to the list
2. Delete node from given position
3. Traverse/View List
4. Exit
Please enter your choice: 2
Enter the position to delete: 2
Deleted node value: 3
***** MENU *****
1. Add value to the list
2. Delete node from given position
3. Traverse/View List
4. Exit
Please enter your choice: 4
What did you think?