Program to implement Doubly Linked List with String using C Language

Programming Languages

/* Program to implement Doubly Linked List with String using C Language */

#include<studio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

struct list
{
char info[40];
struct list *next;
struct list *prev;
}head=NULL,tail=NULL;
void insertion();
void deletion();
void display();
void modify();
void main()
{
int choice,check;
char ch;
clrscr();
do
{
textcolor(9);
clrscr();
printf(“\n\n\t===============================================”);
printf(“\n\n\t\t\tMENU”);
printf(“\n\n\t===============================================”);
printf(“\n\n\t1.INSERT THE STRING INTO THE LINKED LIST”);
printf(“\n\n\t2.DELETE THE STRING FROM THE LINKED LIST”);
printf(“\n\n\t3.MODIFY THE STRING IN THE LINKED LIST”);
printf(“\n\n\t4.DISPLAY THE STRING IN THE LINKED LIST”);
printf(“\n\n\t5.EXIT”);
printf(“\n\n\t===============================================”);
printf(“\n\n\t\tENTER YOUR CHOICE : “);
check=scanf(“%d”,&choice);
while(check!=1)
{
printf(“ENTER ONLY INTEGERS VALUES FROM THE ABOVE CHOICES :”);
fflush(stdin);
check=scanf(“%d”,&choice);
}
switch(choice)
{
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
// modify();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf(“\n\t\t===============================================”);
printf(“\n\t\tYOU ENTERED WRONG CHOICE, TRY AGAIN”);
printf(“\n\t\t===============================================”);
}
fflush(stdin);
printf(“\n\t\tDO YOU WANTS TO EXIT PRESS ‘n’ ELSE PRESS ANY KEY :”);
ch=getche();
}while(ch!=’n’);
getch();
}
void insertion()
{
struct list ptr,ptr1,loc,location;//,temp,temp2;
ptr=(struct list*)malloc(sizeof(struct list));
printf(“\n\nENTER THE STRING :”);
fflush(stdin);
gets(ptr->info);
if(head==NULL)
{
head=ptr;
tail=ptr;
tail->next=head;
head->prev=tail;
}
else
{
ptr1=head;
do
{
if(strcmp(ptr1->info,ptr->info)<0) { loc=ptr1; ptr1=ptr1->next;
continue;
}
else if(strcmp(ptr1->info,ptr->info)>0)
{
location=ptr1;
break;
}
else
{
printf(“\n\n\tSTRING IS ALREADY EXIST, TRY AGAIN”);
getch();
main();
}
}while(ptr1!=head);
if(location==head)
{
location->prev=ptr;
ptr->next=location;
ptr->prev=tail;
tail->next=ptr;
head=ptr;
}
else if(loc==head && loc==tail)
{
loc->next=ptr;
ptr->prev=loc;
tail=ptr;
head->prev=tail;
tail->next=head;
}
else if(loc->next==location)
{
ptr->next=loc->next;
loc->next=ptr;
location->prev=ptr;
ptr->prev=loc;
}
else
{
ptr->prev=tail;
tail->next=ptr;
tail=ptr;
tail->next=head;
head->prev=tail;
}
}
display();
}
void deletion()
{
struct list *ptr1,*dloc;
int flag=0;
char str[40];
printf(“\n\n\t\tENTER THE STRING THAT YOU WANTS TO DELETE :”);
fflush(stdin);
gets(str);
if(head==NULL && tail==NULL)
{
printf(“\n\n\t\tTHERE IS NO ITEM IN THE LIST”);
}
else
{
ptr1=head;
do
{
if(strcmp(ptr1->info,str)==0)
{
flag=0;
dloc=ptr1;
break;
}
else if(strcmp(ptr1->info,str)!=0)
{
flag++;
}
ptr1=ptr1->next;
}while(ptr1!=head);
if(flag==0)
{
if(dloc==head)
{
if(dloc==tail)
{
head=NULL;
tail=NULL;
}
else
{
head=dloc->next;
head->prev=tail;
tail->next=head;
}
}
else if(dloc==tail)
{
if(dloc==head)
{
head=NULL;
tail=NULL;
}
else
{
tail=dloc->prev;
tail->next=head;
}
}
else
{
dloc->prev->next=dloc->next;
dloc->next->prev=dloc->prev;
}
}
else
{
printf(“\n\n\t\tSTRING IS NOT THERE,TRY AGAIN”);
getch();
main();
}
}
display();
}
void display()
{
struct list *ptr,*ptr1;
if(head==NULL)
{
printf(“\n\n\t\t THERE IS NO ELEMENT IN THE LIST “);
}
else
{
ptr1=head;
do
{
puts(ptr1->info);
ptr1=ptr1->next;
}while(ptr1!=head);
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.