C program to implement Doubly Circular Linked List Data Structure
Simple C program to implement Doubly Circular Linked List Data Structure along with following operations:
- Insertion at end
- Insertion at front
- Insert after a value
- Insert before a value
- Delete at end
- Delete at front
- Delete after a value
- Delete before a value
- Delete element itself
- Delete the whole list
- Display linked list
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 || head->next==head){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==head || 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 deletelist(){
struct node *head2=head;
struct node *temp;
if(head==NULL){printf("Deletion is not possible");}
else{
while (head2!=head){
temp=head2;
head2=head2->next;
free(temp);
}
}
head=NULL;
temp=NULL;
}
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;
}
}
}
void displayback(){
if(head==NULL || tail==NULL){printf("list is empty!");}
else{
printf("\t%d",tail->data);
struct node * tail2;
tail2=tail;
tail2=tail2->prev;
while(tail2!=tail){
printf("\t%d",tail2->data);
tail2=tail2->prev;
}
}
}
main(){
head=NULL;
int opt;
do{
printf("\n1. Insert a number");
printf("\t2. Insert at front");
printf("\t3. Insert after");
printf("\t4. Insert before");
printf("\t5. Delete at end");
printf("\t6. Delete from front");
printf("\t7. Delete after");
printf("\t8. Delete before");
printf("\t9. Delete itself");
printf("\t10. Delete the list");
printf("\t11. Display from front ");
printf("\t12. Display from back");
printf("\t13. 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();
case 10:
deletelist();
display();
break;
case 11:
display();
break;
case 12:
displayback();
break;
default:
break;
}
}while(opt!=13);
printf("\nEnd");
getch();
}
Comments
Post a Comment