Doubly Linked List
CODE
👇
- include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int info;
- struct node *prev, *next;
- }NODE;
- NODE* createlist(NODE *list);
- NODE* insert(NODE *list, int num, int pos);
- void display(NODE *list);
- NODE* search(NODE *list, int n1);
- NODE* delpos(NODE *list, int pos);
- NODE* delvalue(NODE *list, int n2);
- void main()
- {
- int num, n1, n2, n3, pos;
- NODE *list=NULL, *temp;
- list=createlist(list);
- display(list);
- printf("\nEnter Element to be search:\t");
- scanf("%d", &n1);
- temp=search(list, n1);
- if(temp!=NULL)
- printf(">>'%d' is found<<\n",n1);
- else
- printf(">>'%d' is not found<<\n",n1);
- printf("\nEnter the node data to insert node:\t");
- scanf("%d", &num);
- printf("Enter the node position to insert node:\t");
- scanf("%d", &pos);
- list=insert(list,num,pos);
- display(list);
- printf("\nEnter node postion to delete that node:\t");
- scanf("%d", &pos);
- list=delpos(list, pos);
- display (list);
- printf("\nEnter node data to delete node:\t");
- scanf("%d", &n2);
- list=delvalue(list, n2);
- display(list);
- }
- NODE* createlist(NODE *list)
- {
- int n, count;
- NODE *temp, *newnode;
- printf("How many nodes you want to enter? \n");
- scanf("%d", &n);
- for(count=1; count<=n; count++)
- {
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->prev=newnode->next=NULL;
- printf("Enter the node data:\t");
- scanf("%d", &newnode->info);
- if(list==NULL)
- {
- list=temp=newnode;
- }
- else
- {
- temp->next=newnode;
- newnode->prev=temp;
- temp=newnode;
- }
- }
- return list;
- }
- NODE* insert(NODE *list, int num, int pos)
- {
- NODE *newnode, *temp;
- int i;
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->prev=newnode->next=NULL;
- newnode->info=num;
- if(pos==1)
- {
- newnode->next=list;
- list->prev=newnode;
- list=newnode;
- return list;
- }
- for(i=1,temp=list; i<pos-1 && temp!=NULL; i++)
- temp=temp->next;
- if(temp==NULL)
- {
- printf(">>position is out of range<< \n");
- return list;
- }
- else
- if(temp->next==NULL)
- {
- newnode->next=temp->next;
- newnode->prev=temp;
- temp->next=newnode;
- return list;
- }
- else
- {
- newnode->next=temp->next;
- temp->next->prev=newnode;
- temp->next=newnode;
- newnode->prev=temp;
- return list;
- }
- }
- void display(NODE *list)
- {
- NODE *temp=list;
- for(temp=list; temp!=NULL; temp=temp->next)
- {
- printf("%d", temp->info);
- printf("--->>");
- }
- printf("NULL \n");
- }
- NODE* search(NODE *list, int n1)
- {
- NODE *temp=list;
- for(temp=list; temp!=NULL; temp=temp->next)
- if(temp->info==n1)
- return temp;
- return NULL;
- }
- NODE* delpos(NODE *list, int pos)
- {
- NODE *temp=list, *temp1, *temp2;
- int i;
- if(pos==1)
- {
- list=list->next;
- list->prev=NULL;
- free (temp);
- return list;
- exit;
- }
- for(i=1,temp=list; i<pos-1 && temp!=NULL; i++)
- temp=temp->next;
- if(temp==NULL || temp->next==NULL)
- {
- printf(">>position out of range<< \n");
- return list;
- exit;
- }
- temp1=temp->next;
- if(temp1->next==NULL)
- {
- temp->next=NULL;
- free (temp1);
- return list;
- exit;
- }
- else
- {
- temp2=temp1->next;
- temp2->prev=temp;
- temp->next=temp2;
- free (temp1);
- return list;
- exit;
- }
- }
- NODE* delvalue(NODE *list, int n2)
- {
- NODE *temp=list, *temp1, *temp2;
- if(list->info==n2)
- {
- list=list->next;
- free (temp);
- return list;
- exit;
- }
- for(temp=list; temp->next!=NULL; temp=temp->next)
- {
- if(temp->next->info==n2)
- {
- temp1=temp->next;
- if(temp1->next==NULL)
- {
- temp->next=NULL;
- free (temp1);
- return list;
- exit;
- }
- else
- {
- temp2=temp1->next;
- temp->next=temp2;
- temp2->prev=temp;
- free (temp1);
- return list;
- exit;
- }
- }
- }
- printf(">>Enter valid data<< \n");
- return list;
- }
👉Execute👈
//Output
/*
How many nodes you want to enter?
5
Enter the node data: 33
Enter the node data: 64
Enter the node data: 78
Enter the node data: 13
Enter the node data: 57
33--->>64--->>78--->>13--->>57--->>NULL
Enter Element to be search: 64
>>'64' is found<<
Enter the node data to insert node: 87
Enter the node position to insert node: 4
33--->>64--->>78--->>87--->>13--->>57--->>NULL
Enter node postion to delete that node: 5
33--->>64--->>78--->>87--->>57--->>NULL
Enter node data to delete node: 78
33--->>64--->>87--->>57--->>NULL
*/
//ThE ProFessoR
Comments
Post a Comment