C program to implement Circular Linked List

Simple C program to implement Circular Linked List with following operations:

  • Insert a number
  • Insert at front
  • Insert after
  • Insert before
  • Delete at end
  • Delete from front
  • Delete after
  • Delete before
  • Delete itself
  • Display from front

Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *head,*tail;


void insertend(){
int val;
printf("\nEnter the value to be Entered: ");
scanf("%d",&val);
struct node *head2;
head2=head;
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->next=NULL;
temp->prev=NULL;


if(head==NULL){
head=temp;
temp->next=head;
temp->prev=head;
}
else{
while(head2->next!=head){
head2=head2->next;
}
temp->prev=head2;
temp->next=head;
head2->next=temp;
tail=temp;
head->prev=tail;


}
}








void insertf(){
int val;
printf("Enter a value to be entered ");
scanf("%d",&val);
struct node *temp;
temp=(struct node*) malloc(sizeof(struct node));
temp->data=val;
temp->next=NULL;
temp->prev=NULL;
if(head==NULL){
head=temp;
}
else{
temp->next=head;
head->prev=temp;
head=temp;
head->prev=tail;
tail->next=head;
}
}


void insertafter(){
int val,target;
printf("\nEnter the number after insert: ");
scanf("%d",&target);
printf("Enter the value to be inserted: ");
scanf("%d",&val);
struct node *head2;
head2=head;
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->next=NULL;
temp->prev=NULL;


if(head==NULL){


head=temp;
}
else{


while(head2->data!=target && head2->next!=head){
head2=head2->next;
}
if(head2->next==head){insertend();}
else{if(head2->data==target){


temp->prev=head2;
temp->next=head2->next;
head2->next->prev=temp;
head2->next=temp;
}
else{printf("\nElement not found!");}
}
}
}






void insertbefore(){
int val,target;
printf("\nEnter the number before insert: ");
scanf("%d",&target);
printf("Enter the value to be inserted: ");
scanf("%d",&val);
struct node *head2;
head2=head;
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=val;
temp->next=NULL;
temp->prev=NULL;


if(head==NULL){


head=temp;
}
else if(head->next==head || head->data==target){insertf();}
else{
while(head2->next->data!=target && head2->next->next!=head){
head2=head2->next;
}
if(head2->next->data==target){
temp->prev=head2;
temp->next=head2->next;
head2->next->prev=temp;
head2->next=temp;
}
else{printf("Element not found");}
}
}


void deleteend(){
struct node *head2;
head2=head;
if(head2->next==head){
printf("Deletion not possible");
}
else{
while(head2->next->next!=head){
head2=head2->next;
}
struct node *temp;
temp=head2->next;
head2->next=head;
free (temp);
tail=head2;
head->prev=tail;
}
}


void deletefront(){
if(head->next==head){
printf("Deletion not possible");
}
else{
struct node *temp;
temp=head;
head=head->next;
head->prev=tail;
free(temp);
tail->next=head;
}
}


void deleteafter(){
struct node *head2;
head2=head;
int a;
printf("\nEnter the number after which the number is to be deleed: ");
scanf("%d",&a);
if(head==NULL || head2->next==NULL || head->data==a){printf("\nDeletion not possible");}
else{
while(head2->data!=a && head2->next!=head){
head2=head2->next;
}
if(head2->data==a){
if(head2->next==head){deletefront();}
else{
struct node *temp;
temp=head2->next;
head2->next=temp->next;
temp->next->prev=head2;
free(temp);
}
}
else{printf("Element not found");}
}
}






void deletebefore(){
struct node *head2;
head2=head;
int a;
printf("\nEnter the number bedore which the number is to be deleed: ");
scanf("%d",&a);
if(head2->next==NULL || head==NULL){
printf("\nDeletion not possible");
}
else if(head->data==a){deleteend();}
else {
while(head2->next->next->data!=a && head2->next->next->next!=head){
head2=head2->next;
}
if(head2->next->next->data==a){


struct node *temp;
temp=head2->next;
head2->next=temp->next;
temp->next->prev=head2;
free(temp);
}


else{printf("Element not found");}
}
}




void deleteitself(){
struct node *head2;
head2=head;
int a;
printf("\nEnter the number to be deleted: ");
scanf("%d",&a);
if(head2->next==NULL || head==NULL){
printf("\nDeletion not possible");
}
else if(head->data==a){deletefront();}
else{
while(head2->next->data!=a && head2->next->next!=head){
head2=head2->next;
}
if(head2->next->data==a){
if(head2->next->next==head){deleteend();}
else{
struct node *temp;
temp=head2->next;
head2->next=temp->next;
temp->next->prev=head2;
free(temp);
}
}
else{printf("Element not found");}
}
}




void display(){
if(head==NULL){
printf("List is empty!");
}
else{
struct node *head2=head;
printf("\n");
printf("\t%d",head2->data);
head2=head2->next;
while(head2!=head){
printf("\t%d",head2->data);
head2=head2->next;
}
}
}




main(){
head=NULL;


int opt;
do{
printf("\n\n1. Insert a number");
printf("\t2. Insert at front");
printf("\t3. Insert after");
printf("\n4. Insert before");
printf("\t5. Delete at end");
printf("\t6. Delete from front");
printf("\n7. Delete after");
printf("\t8. Delete before");
printf("\t9. Delete itself");
printf("\n10. Display from front ");
printf("\t11. Exit");
printf("\nEnter your choice: ");
scanf("%d",&opt);


switch(opt){
case 1:
insertend();
display();
break;
case 2:
insertf();
display();
break;
case 3:
insertafter();
display();
break;
case 4:
insertbefore();
display();
break;
case 5:
deleteend();
display();
break;
case 6:
deletefront();
display();
break;
case 7:
deleteafter();
display();
break;
case 8:
deletebefore();
display();
break;
case 9:
deleteitself();
display();
break;
case 10:
display();
break;
default:
break;
}
}while(opt!=11);
printf("\nEnd");
getch();
}

Comments

Popular posts from this blog

C program to evaluate Prefix Expression using Stack data structure

Java Program to Implement sorting algorithm using TCP on Server application

C++ program to perform data transformation Min-max and Z score Normalization