C PROGRAM FOR IMPLEMENTATION OF DOUBLY LINKED LIST (ALL FUNCTIONS)

//this code is designed by codenation.

//29/1/2021 9:49


#include<stdio.h>

#include<stdlib.h>


struct data{

int data;

struct data *nxt,*pr;

}*st,*end;


void insertstart();

void insertbeforedata();

void insertafterdata();

void insertbylocation();

void insertend();

void deletestart();

void deletebydata();

void deletebylocation();

void deleteend();

void traverseforward();

void traversebackward();

void searchlist();


int main()

{

int choice;

do{

printf("enter your choice :\n");

printf("1.INSERT AT START OF DLL\n2.INSERT BEFORE NODE IN DLL\n3.INSERT AFTER NODE IN DLL\n4.INSERT BY LOCATION IN DLL\n5.INSERT AT END OF DLL\n6.DELETE AT START OF DLL\n7.DELETE BY DATA IN DLL\n8.DELETE BY LOCATION IN DLL\n9.DELETE AT END OF DLL\n10.TRAVERSE DLL FORWARD\n11.TRAVERSE DLL BACKWARD\n12.SEARCH DLL\n13.EXIT\n");

scanf("%d",&choice);

switch(choice)

{

case 1:

insertstart();

break;

case 2:

insertbeforedata();

break;

case 3:

insertafterdata();

break;

case 4:

insertbylocation();

break;

case 5:

insertend();

break;

case 6:

deletestart();

break;

case 7:

deletebydata();

break;

case 8:

deletebylocation();

break;

case 9:

deleteend();

break;

case 10:

traverseforward();

break;

case 11:

traversebackward();

break;

case 12:

searchlist();

break;

case 13:

return 0;

default:

printf("ENTER VALID CHOICE\n");

}

}while(choice!=13);

}


void insertstart()

{

struct data *new;

new=(struct data*) malloc(sizeof(struct data));

if(new==NULL)

{

printf("MEMORY FULL\n");

return;

}

printf("enter data :\n");

scanf("%d",&new->data);

new->nxt=NULL;

new->pr=NULL;

if(st==NULL)

{

st=new;

end=new;

}

else

{

new->nxt=st;

st->pr=new;

st=new;

}

printf("DATA INSERTED SUCCESSFULLY AT START\n");


return;

}


void insertbeforedata()

{

struct data *new,*t;

int element;

if(st==NULL)

{

printf("NO LINKED LIST PRESENT\n");

return;

}

printf("enter the data in node before you want to insert :\n");

scanf("%d",&element);

if(st->data==element)

{

insertstart();

return;

}

new=(struct data*) malloc(sizeof(struct data));

if(new==NULL)

{

printf("MEMORY FULL\n");

return;

}

t=st;

while(t->data!=element && t!=end)

{

t=t->nxt;

}

if(t->data==element)

{

printf("enter data :\n");

scanf("%d",&new->data);

new->nxt=t;

new->pr=t->pr;

t->pr->nxt=new;

t->pr=new;

printf("DATA INSERTED SUCCESSFULLY BEFORE NODE\n");

}

else

{

printf("DATA NOT FOUND IN DLL\n");

}

return;

}


void insertafterdata()

{

struct data *new,*t;

int element;

if(st==NULL)

{

printf("NO LINKED LIST PRESENT\n");

return;

}

printf("enter the data in node after you want to insert :\n");

scanf("%d",&element);

if(end->data==element)

{

insertend();

return;

}

new=(struct data*) malloc(sizeof(struct data));

if(new==NULL)

{

printf("MEMORY FULL\n");

return;

}

t=st;

while(t->data!=element && t!=end)

{

t=t->nxt;

}

if(t->data==element)

{

printf("enter data :\n");

scanf("%d",&new->data);

new->pr=t;

new->nxt=t->nxt;

t->nxt->pr=new;

t->nxt=new;

printf("DATA INSERTED SUCCESSFULLY AFTER NODE\n");

}

else

{

printf("DATA NOT FOUND IN DLL\n");

}

return;

}


void insertbylocation()

{

struct data *new,*t;

int location,ctr=1;

if(st==NULL)

{

printf("INSERTING AT START AS NO LINKED LIST PRESENT\n");

insertstart();

return;

}

printf("enter the location of data where you want to insert :\n");

scanf("%d",&location);


if(location==1)

{

insertstart();

return;

}

t=st;

while(ctr<location && t!=end)

{

t=t->nxt;

ctr++;

}

if(t==end)

{

insertend();

return;

}

new=(struct data*) malloc(sizeof(struct data));

if(new==NULL)

{

printf("MEMORY FULL\n");

return;

}

if(ctr==location)

{

printf("enter data :\n");

scanf("%d",&new->data);

t->pr->nxt=new;

t->pr=new;

new->nxt=t;

new->pr=t->pr;

printf("DATA INSERTED SUCCESSFULLY AT LOCATION %d\n",location);

}

else

{

printf("LOCATION NOT FOUND IN DLL\n");

}

return;

}


void insertend()

{

struct data *new;

new=(struct data*) malloc(sizeof(struct data));

if(new==NULL)

{

printf("MEMORY FULL\n");

return;

}

printf("enter data :\n");

scanf("%d",&new->data);

new->nxt=NULL;

new->pr=NULL;

if(st==NULL)

{

st=new;

end=new;

}

else

{

new->pr=end;

end->nxt=new;

end=new;

}

printf("DATA INSERTED SUCCESSFULLY AT END\n");

return;

}


void deletestart()

{

struct data *t;

if(st==NULL)

{

printf("NO LINKED LIST PRESENT\n");

return;

}

t=st;

if(st->nxt==NULL)

{

printf("DATA FROM DELETED NODE IS :%d\n",t->data);

printf("DATA DELETED SUCCESSFULLY FROM START\n");

free(t);

st=NULL;

end=NULL;  

}

else

{

st=st->nxt;

st->pr=NULL;

printf("DATA FROM DELETED NODE IS :%d\n",t->data);

printf("DATA DELETED SUCCESSFULLY FROM START\n");

free(t);

}

return;

}


void deletebydata()

{

struct data *t;

int element;

if(st==NULL)

{

printf("NO LINKED LIST PRESENT\n");

return;

}

printf("enter the data in node which you want to delete :\n");

scanf("%d",&element);

if(st->data==element)

{

deletestart();

return;

}

if(end->data==element)

{

deleteend();

return;

}

t=st;

while(t->data!=element && t!=end)

{

t=t->nxt;

}

if(t->data==element)

{

t->pr->nxt=t->nxt;

t->nxt->pr=t->pr;

printf("DATA FROM DELETED NODE IS :%d\n",t->data);

printf("DATA DELETED SUCCESSFULLY FROM NODE\n");

free(t);

}

else

{

printf("DATA NOT FOUND IN DLL\n");

}

return;

}


void deletebylocation()

{

struct data *t;

int location,ctr=1;

if(st==NULL)

{

printf("NO LINKED LIST PRESENT\n");

return;

}

printf("enter the location of data you want to delete :\n");

scanf("%d",&location);


if(location==1)

{

deletestart();

return;

}

t=st;

while(ctr<location && t!=end)

{

t=t->nxt;

ctr++;

}

if(t==end)

{

deleteend();

return;

}

if(ctr==location)

{

t->pr->nxt=t->nxt;

t->nxt->pr=t->pr;

printf("DATA FROM DELETED NODE IS :%d\n",t->data);

printf("DATA DELETED SUCCESSFULLY FROM NODE AT LOCATION %d\n",location);

free(t);

}

else

{

printf("LOCATION NOT FOUND IN DLL\n");

}

return;

}


void deleteend()

{

struct data *t;

if(st==NULL)

{

printf("NO LINKED LIST PRESENT\n");

return;

}

t=end;

if(end->pr==NULL)

{

printf("DATA FROM DELETED NODE IS :%d\n",t->data);

printf("DATA DELETED SUCCESSFULLY FROM END\n");

free(t);

st=NULL;

end=NULL;  

}

else

{

end=end->pr;

end->nxt=NULL;

printf("DATA FROM DELETED NODE IS :%d\n",t->data);

printf("DATA DELETED SUCCESSFULLY FROM END\n");

free(t);

}

return;

}


void traverseforward()

{

struct data *temp;

int i=1;

if(st==NULL)

{

printf("THERE IS NO LINKED LIST PRESENT\n");

return;

}

temp=st;

while(temp!=NULL)

{

printf("data from %d node is :\n",i);

printf("%d\n",temp->data);

temp=temp->nxt;

i++;

}

return;

}


void traversebackward()

{

struct data *temp;

if(st==NULL)

{

printf("THERE IS NO LINKED LIST PRESENT\n");

return;

}

temp=end;

while(temp!=NULL)

{

printf("data is :\n");

printf("%d\n",temp->data);

temp=temp->pr;

}

return;

}


void searchlist()

{

int item,i=1;

struct data *t;

printf("enter the data you want to find in DLL :\n");

scanf("%d",&item);

if(item==st->data)

{

t=st;

printf("DATA FOUND AT START\n");

printf("DATA IS :%d\n",t->data);

return;

}

if(item==end->data)

{

t=end;

printf("DATA FOUND AT END\n");

printf("DATA IS :%d\n",t->data);

return;

}

t=st;

while(t->data!=item && t!=end)

{

t=t->nxt;

i++;

}

if(t->data==item)

{

printf("DATA FOUND AT LOCATION %d\n",i);

printf("DATA IS :%d\n",t->data);

}

else

{

printf("DATA NOT FOUND IN DLL\n");

}

return;

}


//this code is developed by codenation.

Comments