Circular Doubly Linked List
CODE
👇
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int info;
- struct node *next;
- struct node *prev;
- }NODE;
- void createlist(NODE *list);
- void display(NODE *list);
- void search(NODE *list);
- void insert(NODE *list, int num, int pos);
- void delpos(NODE *list, int pos);
- void delvalue(NODE *list, int num);
- void main()
- {
- int pos, num, n, no;
- NODE *list=(NODE*)malloc(sizeof(NODE));
- list->next=list;
- createlist(list);
- display(list);
- printf("\n");
- do
- {
- printf("\nOperation on Doubly Linked List:");
- printf("\n1. Insert:");
- printf("\n2. Display");
- printf("\n3. Search");
- printf("\n4. Delete by position");
- printf("\n5. Delete by value");
- printf("\n6. Exit\n");
- printf("\n Enter your choice:");
- scanf("%d", &no);
- switch(no)
- {
- case 1: printf(" Enter the node data:");
- scanf("%d", &num);
- printf("Enter the node position to insert node:");
- scanf("%d", &pos);
- insert(list, num, pos);
- display(list);
- printf("\n");
- break;
- case 2: printf(">>Circular Doubly Linked List<<\n");
- display(list);
- printf("\n");
- break;
- case 3: search(list);
- display(list);
- printf("\n");
- break;
- case 4: printf("Enter node position to be delete that node:");
- scanf("%d", &pos);
- delpos(list, pos);
- display(list);
- printf("\n");
- break;
- case 5: printf("Enter node data to be delete :");
- scanf("%d", &num);
- delvalue(list, num);
- display(list);
- printf("\n");
- break;
- case 6: printf("End\n");
- break;
- default:
- printf("\n>>Enter Valid Choice<<\n");
- }
- }
- while(no!=6);
- }
- void createlist(NODE *list)
- {
- int n, i;
- NODE *temp=list, *nn;
- printf("How Many Nodes you want to enter ?\n");
- scanf("%d", &n);
- for(i=1; i<=n; i++)
- {
- nn=(NODE*)malloc(sizeof(NODE));
- printf("\n Enter the node data:\t");
- scanf("%d", &nn->info);
- nn->next=list;
- temp->next=nn;
- temp->prev=temp;
- list->prev=nn;
- temp=nn;
- }
- }
- void display(NODE *list)
- {
- NODE *temp;
- for(temp=list->next; temp!=list; temp=temp->next)
- {
- printf("%d", temp->info);
- printf("--->");
- }
- }
- void search(NODE *list)
- {
- int num, flag=0;
- NODE *temp;
- printf("Enter Element to search:\t");
- scanf("%d", &num);
- for(temp=list->next; temp!=list; temp=temp->next)
- {
- if(temp->info==num)
- {
- printf(">>%d is found<<\n", num);
- flag=1;
- exit;
- }
- }
- if(flag==0)
- printf(">>%d is not found<< \n", num);
- }
- void insert(NODE *list, int num, int pos)
- {
- NODE *nn, *temp=list, *temp1;
- int i;
- nn=(NODE*)malloc(sizeof(NODE));
- nn->info=num;
- for(i=1, temp=list; temp->next!=list && i<pos; i++)
- temp=temp->next;
- if(i<pos)
- {
- printf("Position is out range\n");
- return;
- }
- temp1=temp->next;
- nn->next=temp1;
- temp1->prev=nn;
- temp->next=nn;
- nn->prev=temp;
- }
- void delpos(NODE *list, int pos)
- {
- NODE *temp=list, *temp1, *temp2;
- int i;
- for(i=1,temp=list; temp->next!=list && i<pos; i++)
- temp=temp->next;
- if(i<pos || temp->next==list)
- {
- printf(">>Position is out of range<<\n");
- return;
- }
- temp1=temp->next;
- temp2=temp1->next;
- temp->next=temp2;
- temp2->prev=temp;
- free (temp1);
- }
- void delvalue(NODE *list, int num)
- {
- NODE *temp=list, *temp1, *temp2;
- if(list->info==num)
- {
- list=list->next;
- free (temp);
- return;
- exit;
- }
- for(temp=list; temp->next!=list; temp=temp->next)
- {
- if(temp->next->info==num)
- {
- temp1=temp->next;
- if(temp1->next==NULL)
- {
- temp->next=NULL;
- free (temp);
- return;
- exit;
- }
- else
- {
- temp2=temp1->next;
- temp->next=temp2;
- temp2->prev=temp;
- free (temp1);
- return;
- exit;
- }
- }
- }
- printf(">>Enter valid data<<\n");
- return;
- }
👉Execute👈
//Output
/*
How Many Nodes you want to enter ?
5
Enter the node data: 8
Enter the node data: 1
Enter the node data: 5
Enter the node data: 6
Enter the node data: 4
8--->1--->5--->6--->4--->
Operation on Doubly Linked List:
1. Insert:
2. Display
3. Search
4. Delete by position
5. Delete by value
6. Exit
Enter your choice:1
Enter the node data:9
Enter the node position to insert node:2
8--->9--->1--->5--->6--->4--->
Operation on Doubly Linked List:
1. Insert:
2. Display
3. Search
4. Delete by position
5. Delete by value
6. Exit
Enter your choice:2
>>Circular Doubly Linked List<<
8--->9--->1--->5--->6--->4--->
Operation on Doubly Linked List:
1. Insert:
2. Display
3. Search
4. Delete by position
5. Delete by value
6. Exit
Enter your choice:3
Enter Element to search: 1
1 is found
8--->9--->1--->5--->6--->4--->
Operation on Doubly Linked List:
1. Insert:
2. Display
3. Search
4. Delete by position
5. Delete by value
6. Exit
Enter your choice:4
Enter node position to be delete that node: 4
8--->9--->1--->6--->4--->
Operation on Doubly Linked List:
1. Insert:
2. Display
3. Search
4. Delete by position
5. Delete by value
6. Exit
Enter your choice:5
Enter node data to be delete : 1
8--->9--->6--->4--->
Operation on Doubly Linked List:
1. Insert:
2. Display
3. Search
4. Delete by position
5. Delete by value
6. Exit
Enter your choice:6
End
Comments
Post a Comment