Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //dll
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- int val;
- struct dll
- {
- int info;
- struct dll *next;
- struct dll *prev;
- }*node=NULL,*start=NULL,*prev=NULL,*temp=NULL;
- void create_node()
- {
- node=(struct dll *)malloc(sizeof(struct dll));
- if(node==NULL)
- {
- printf("\n Node is not allocated memory");
- return;
- }
- node->next=NULL;
- node->prev=NULL;
- }
- void insert_beg();
- void insert_end();
- void display()
- {
- temp=start;
- if(start==NULL)
- {
- printf("\n DLL is empty for DIsplay");
- return;
- }
- printf("\n Displayed DLl");
- while(temp!=NULL)
- {
- printf("\n => %d",temp->info);
- temp=temp->next;
- }
- }
- void insert_beg()
- {
- create_node();
- printf("\n Enter the value: ");
- scanf("%d",&val);
- if(start==NULL)
- {
- node->info=val;
- start=node;
- }
- else
- {
- node->info=val;
- node->next=start;
- start->prev=node;
- start=node;
- }
- display();
- }
- void insert_end()
- {
- if(start==NULL)
- {
- insert_beg();
- return;
- }
- create_node();
- temp=start;
- while(temp->next!=NULL)
- {
- temp=temp->next;
- }
- printf("\n Enter the value: ");
- scanf("%d",&val);
- node->info=val;
- node->prev=temp;
- temp->next=node;
- display();
- }
- void insert_spc_val()
- {
- int t=0,l;
- if(start==NULL)
- {
- printf("\n no values for select ");
- return;
- }
- printf("\n select from following dll\n");
- display();
- printf("\n select the the value: ");
- scanf("%d",&val);
- temp=start;
- while(temp!=NULL)
- {
- if(start->info==val)
- {
- insert_beg();
- t=1;
- return;
- }
- if(temp->info==val)
- {
- t=1;
- break;
- }
- prev=temp;
- temp=temp->next;
- }
- if(t==0)
- {
- printf("\n wrong value inserted");
- display();
- }
- else
- {
- create_node();
- printf("\n ENter the value: ");
- scanf("%d",&l);
- node->info=l;
- node->prev=prev;
- node->next=temp;
- prev->next=node;
- temp->prev=node;
- display();
- }
- }
- void insert_spc_posi()
- {
- int t=0;
- if(start==NULL)
- {
- printf("\n no values for select ");
- return;
- }
- printf("\n select from following dll\n");
- display();
- printf("\n select the the position: ");
- scanf("%d",&val);
- if(val==1)
- {
- insert_beg();
- return;
- }
- temp=start;
- int i=1;
- while(temp!=NULL)
- {
- if(i==val)
- {
- t=1;
- create_node();
- printf("\n ENter the value: ");
- scanf("%d",&val);
- node->info=val;
- node->prev=prev;
- node->next=temp;
- prev->next=node;
- temp->prev=node;
- display();
- break;
- }
- i++;
- prev=temp;
- temp=temp->next;
- }
- if(t==0)
- {
- printf("\n wrong position inserted");
- display();
- }
- }
- void del_beg()
- {
- if(start==NULL)
- {
- printf("\n Dll is EMpty for Delete");
- return;
- }
- printf("\n %d is deleted\n",start->info);
- node=start;
- start=start->next;
- free(node);
- display();
- }
- void del_end()
- {
- if(start==NULL)
- {
- printf("\n Dll is empty for delete");
- return;
- }
- if(start->next==NULL)
- {
- del_beg();
- return;
- }
- temp=start;
- while(temp->next!=NULL)
- {
- prev=temp;
- temp=temp->next;
- }
- printf("\n %d is deleted\n",temp->info);
- prev->next=NULL;
- free(temp);
- display();
- }
- void del_the_value()
- {
- int t=0;
- if(start==NULL)
- {
- printf("\n Dll is empty for delete");
- return;
- }
- printf("\n\n \t Choose the value you want to Delete");
- display();
- printf("\n \nENter the value: ");
- scanf("%d",&val);
- if(start->info==val)
- {
- del_beg();
- return;
- }
- temp=start;
- while(temp!=NULL)
- {
- if(temp->info==val)
- {
- t=1;
- prev->next=temp->next;
- printf("\n %d is deleted\n",temp->info);
- free(temp);
- display();
- break;
- }
- prev=temp;
- temp=temp->next;
- }
- if(t==0)
- {
- printf("\n Wrong value inserted");
- display();
- }
- }
- void del_the_posi()
- {
- int t=0,l,i=16;
- if(start==NULL)
- {
- printf("\n the DLL is empty for delete");
- return;
- }
- printf("\n chose the position you want to delete:- ");
- display();
- printf("\n Enter the position: ");
- scanf("%d",&l);
- if(l==1)
- {
- del_beg();
- return;
- }
- temp=start;
- while(temp!=NULL)
- {
- if(i==l)
- {
- prev->next=temp->next;
- printf("\n %d is deleted\n",temp->info);
- free(temp);
- display();
- break;
- }
- i++;
- prev=temp;
- temp=temp->next;
- }
- }
- int main()
- {
- int ch;
- do
- {
- printf("\n\t\t DLL menu");
- printf("\n 1.insert_beg");
- printf("\n 2.insert_end");
- printf("\n 3.insert_spc_value");
- printf("\n 4.insert_spc_position");
- printf("\n 5.delete_beg");
- printf("\n 6.delete_end");
- printf("\n 7.delete_spc_value");
- printf("\n 8.delete_spc_position");
- printf("\n 9.display");
- printf("\n Enter your choice: ");
- scanf("%d",&ch);
- switch(ch)
- {
- case 1:
- insert_beg();
- break;
- case 2:
- insert_end();
- break;
- case 3:
- insert_spc_val();
- break;
- case 4:
- insert_spc_posi();
- break;
- case 5:
- del_beg();
- break;
- case 6:
- del_end();
- break;
- case 7:
- del_the_value();
- break;
- case 8:
- del_the_posi();
- break;
- case 9:
- display();
- }
- }while(ch<=9 && ch>=1);
- return 0;
- }
Add Comment
Please, Sign In to add comment