/* WAP TO IMPLEMENT VARIOUS OPERATION ON DOUBLE CIRCULAR
LINKED LIST USING STRING */
/*INCLUDE HEADER FILES**/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
/*DECLARE PROTOTYPE OF FUNCTIONS**/
void insert();
void delet();
void display();
void modify();
void upper();
void sort();
void location();
void remove1();
/*DECLARE SELF REFRENTIAL STRUCTURE**/
struct linked_list
{
char info[50];
struct linked_list *next;
struct linked_list *prev;
}start=NULL,node,tail,loc1,*ptr;
/*MAIN FUNCTION START HERE**/
void main()
{
int choice,check; //DECLARE LOCAL VARIABLE
char character,check1;
clrscr();
/*DO-WHILE LOOP FOR ASKING USER REPEATDILY**/
do
{
clrscr(); //TO CLEAR THE SCREEN
printf("\n\t::::::::::::::::::::::::::::::::::::::::::::::::::\n");
printf("\n\t::::::::::::::::: MENU :::::::::::::::::\n");
printf("\n\t::::::::::::::::::::::::::::::::::::::::::::::::::\n");
printf("\n\n\t\t1.INSERT STRING IN LINKED LIST\n");
printf("\n\t\t2.DELETE STRING FROM LINKED LIST\n");
printf("\n\t\t3.DISPLAY THE LINKED LIST\n");
printf("\n\t\t4.MODIFY THE LINKED LIST\n");
printf("\n\t\t5.EXIT FROM LINKED LIST\n");
printf("\n\nENTER YOUR CHOICE PLEASE..... : ");
fflush(stdin); //CLEAR THE BUFFER
check=scanf("%d",&choice); //ASK THE VALUE OF CHOICE
/*WHILE LOOP FOR CHECKING ITS CHOICE**/
while(choice <1 || choice >5) //WHILE CONTINUE UNTILL CHOICE IS VALID
{
printf("\nWRONG CHOICE ENTERED ,TRY AGAIN.....\n");
printf("\n\nENTER YOUR CHOICE PLEASE..... : ");
fflush(stdin); //TO CLEAR THE BUFFER
scanf("%d",&choice);
}
/*WHILE LOOP FOR CHECK WHETEHR CHOICE IS CHARCTER OR INT ?*/
while(check!=1)
{
printf("\n\ENTER ONLY INTEGERS VALUES,TRY AGAIN....\n");
fflush(stdin); //TO CLEAR THE BUFFER
printf("\n\nENTER YOUR CHOICE PLEASE..... : ");
check=scanf("%d",&choice);
}
/*SWITCH CASE START HERE*/
switch(choice)
{
case 1:
insert(); //CALL INSERT FUN FOR INSERT THE STRING
break;
case 2:
delet(); //CALL DELET FUN FOR DELETE THE STRING
break;
case 3:
display(); //DISPLAY THE LINKED LIST
break;
case 4:
modify(); //DISPLAY THE LINKED LIST
break;
case 5:
exit(); //FOR EXIT FROM LINKED LIST
break;
}
printf("\n\nYOU WANT TO CONTINUE....PRESS Y OR N :");
fflush(stdin); // TO CLEAR THE BUFFER
character=getche(); //ASK THE USER TO CONTINUE OR NOT
check1=character; //ASSIGN THE VALUE OF CH TO CHECK1 VARIABLE
/*USE WHILE LOOP TO CHECK IF CHOICE IS WRONG*/
while(check1!='Y' && check1!='N' && check1!='y' && check1!='n')
{
printf("\n\n\nINVALID VALUE,PRESS 'Y' TO CONTINUE AND 'N' FOR EXIT........ :");
fflush(stdin); // TO CLEAR THE BUFFER
check1=getche();
}
/*END OF SWITCH CASE*/
character=check1;
}while(character==’y’ || character==’Y’);
/*END OF DO-WHILE LOOP****/
getch();
}
/*END OF MAIN FUNCTION**/
/*DEFINE INSERT FUNCTION**/
void insert()
{
int index,temp;
ptr=(struct linked_list*)malloc(sizeof(struct linked_list)); //DYNAMICALLY ALLOCATION
printf(“\n\nENTER THE STRING YOU WANT TO INSERT…….:”);
fflush(stdin); //CLEAR THE BUFFER
gets(ptr->info); //ENTER THE STRING
upper(ptr);
/*DEFINE PUSH FUNCTION**/
if(start==NULL)
{
start=ptr; //PUT THE START TO POINTER
ptr->next=start; //PUT THE START TO ADDRESS OF POINTER'S NEXT
tail=ptr; //ASSIGN THE ADDRESS OF POINTER TO TAIL POINTER
node=ptr;
ptr->prev=tail; // SET POINTER PREVIOUS ADDRESS TO TAIL
}
else
{
sort(); //CALL SORT FUNCTION FOR FIND LOCATION
fflush(stdin); //TO CLEAR THE BUFFER
location(); //CALL LOCATION FUNCTION TO SET YHE NODE
}
printf("\nSTRING IS INSERT NOW\n");
display(); // CALL THE DISPLAY THE FUNCTION
}
/*DEFINE DELET FUNCTION**/
void delet()
{
int index,temp;
char str[20];
ptr=(struct linked_list*)malloc(sizeof(struct linked_list)); //DYNAMICALLY ALLOCATION
if(start==NULL) //CHECK WHETHER STACK IS EMPTY OR NOT ?
{
printf(“\nLINKED LIST IS EMPTY….\n”);
}
else
{
fflush(stdin); //TO CLEAR THE BUFFER
printf(“\n\nENTER THE STRING YOU WANT TO DELETE….. :”);
gets(str); //PUT THE STRING INTO NEW NODE
index=0; //RESET THE INDEX
while(str[index]!=’\0′) //WHILE LOOP FOR CONVERT
{ //LOWER TO UPPER CASE
str[index]=toupper(str[index]);
index++;
}
str[index]=NULL;
ptr=start; //SET START TO POINTER
/*WHILE LOOP FOR FIND THE LOCATION OF THE STRING IN LINKED LIST ***/
do
{
temp=strcmp(ptr->info,str); //AGAIN USE STRING
if(temp==0) //COMPARE FUN
{
loc1=ptr;
break; //BERAK ST. FOR EXIT OF LOOP
}
else
{
loc1=ptr;
ptr=ptr->next; //INCREMENT THE POINTER
}
}while(ptr!=start);
/*DELETE THE NODE FROM LINKED LIST*/
if(temp==0)
{
remove1(); //CALL REMOVE FUNCTION FOR REMOVE
printf("\nSTRING IS DELETE NOW\n"); //DESIRED NODE
display(); //CALL THE DISPLAY FUNCTION
}
else
{
printf("\n\nSTRING IS NOT IN LINKED LIST......");
}
}
}
/*DEFINE MODIFY FUNCTION**/
void modify()
{
int index,temp,len,len1;
char str[20];
ptr=(struct linked_list*)malloc(sizeof(struct linked_list)); //DYNAMICALLY ALLOCATION
if(start==NULL) //CHECK WHETHER STACK IS EMPTY OR NOT ?
{
printf(“\nLINKED LISTS IS EMPTY…….\n”);
}
else
{
fflush(stdin); //TO CLEAR THE BUFFER
printf(“\nENTER THE STRING YOU WANT TO MODIFY…… :”);
gets(str); //PUT STRING TO NEW NODE
index=0;
while(str[index]!=’\0′) //CONVERT STRING TO UPPER CASE
{
str[index]=toupper(str[index]);
index++; //INCREMENT THE INDEX
}
str[index]=NULL;
ptr=start;
/*WHILE LOOP FOR FIND THE LOCATION OF THE STRING WHICH IS TO BE MODFIED**/
do
{
temp=strcmp(ptr->info,str); //AGAIN USE STRING
if(temp==0) //COMPARE FUN
{
loc1=ptr;
break; //USE BREAK TO EXIT THE LOOP
}
loc1=ptr;
ptr=ptr->next; //INCREMENT THE POINTER
}while(ptr!=start);
/*REMOVE THAT NODE FROM THE LINKED LIST*/
if(temp==0)
{
remove1(); //CALL REMOVE FUNCTION
/*INSERT NEW STRING TO OLDER ONE*/
fflush(stdin);
printf("\nENTER NEW STRING :");
gets(str);
index=0;
while(str[index]!='\0') //WHILE LOOP FOR CONVERT
{ //LOWER TO UPPER CASE
ptr->info[index]=str[index];
index++;
}
ptr->info[index]=NULL;
upper(ptr);
node=start; //POINT NODE TO START
index=0; //RESET THE INDEX
if(node==NULL) //INSERT STRING AT START IF LIST IS EMPTY
{
start=ptr;
ptr->next=start;
tail=ptr;
ptr->prev=ptr;
}
else
{
/*INSERT THE NODE IN TO ITS SPECFIC LOCATION*/
sort(); //CALL SORT FUNCTION FOR FINDIND LOCATION
location(); //CALL LOCATION FUNCTION FOR SET THE NODE
}
}
else
{
printf("\nSTRING IS NOT PRESENT IN LINKED LIST....\n");
}
display(); //CALL THE DISPLAY FUNCTION
}
}
/*DEFINE DISPLAY FUNCTION**/
void display()
{
ptr=(struct linked_list*)malloc(sizeof(struct linked_list));
//DYNAMICALLY ALLOCATION
if(start==NULL) //CHECK WHETHER THE STACK IS EMPTY OR NOT ?
{
printf(“\nLINKED LIST EMPTY….\n”);
}
else
{
ptr=start; //ASSIGN THE POINTER START TO PTR POINTER
printf(“\nNOW LINKED LIST IS……\n\n”);
do // WHILE LOOP FOR CONTINUE THE LOOP UNTILL NULL STRIKE
{
puts(ptr->info); //PRINT THE PTR POINTER’S VALUE
ptr=ptr->next; //INCREMENT THE POINTER
}while(ptr!=start);
}
}
/*DEFINE UPPER FUNCTION**/
void upper()
{
int index=0;
while(ptr->info[index]!=’\0′) // WHILE LOOP FOR CONVERT
{ // LOWER TO UPPER CASE
ptr->info[index]=toupper(ptr->info[index]);
index++;
}
ptr->info[index]=NULL;
}
/*DEFINE SORT FUNCTION**/
void sort()
{
int index=0;
do
{
while(node->info[index]==ptr->info[index])
{
index++; //INCREMENT INDEX
}
if(node->info[index] > ptr->info[index])
{
loc1=node; //SET CURRENT LOCATION TO LOC1
break; //BREAK ST.FOR EXIT FROM LOOP
}
loc1=node;
node=node->next; //INCREMENT THE NODE
index=0;
}while(node!=start);
}
/*DEFINE LOCATION FUNCTION**/
void location()
{
int temp;
if(loc1==start)
{
/*USE STRING COMPARE FUN TO COMPARE THE STRING*/
temp=strcmp(loc1->info,ptr->info);
/*IF INSERT BEFORE START***/
if(temp>0)
{
ptr->next=start; //POINT POINTER'NEXT TO START
start->prev=ptr;
ptr->prev=tail;
tail->next=ptr;
start=ptr; //SET START TO POINTER
node=ptr; //SET NODE TO POINTER
}
/*IF INSERT AFTER START***/
else
{
ptr->next=start;
ptr->prev=start;
start->next=ptr;
start->prev=ptr;
tail=ptr;
node=start; //SET NODE TO START
}
}
/*INSERT STRING WHEN LOCATION IS AT START*/
else if(loc1->next==start)
{
temp=strcmp(loc1->info,ptr->info); //AGAIN USE STRING COMPARE FUN
/*IF INSERT BEFORE LAST POSITION*/
if(temp>0)
{
ptr->next=tail;
ptr->prev=tail->prev;
tail->prev->next=ptr;
tail->prev=ptr; //POINT THE POINTER'NEXT TO LOCATIO
node=start; //SET NODE TO START
}
else //IF INSERT AFTER LAST POSITIOM
{
ptr->next=start; //POINT THE POINTER'NEXT TO LOCATION'NEXT
start->prev=ptr;
loc1->next=ptr;
ptr->prev=loc1;
tail=ptr; //SET TAIL TO POINTER
node=start; //SET NODE TO START
}
}
/*INSERT STRING WHEN LOCATION IS SOMWHERE ELSE*/
else
{
ptr->next=loc1;
ptr->prev=loc1->prev;
loc1->prev->next=ptr;
loc1->prev=ptr;
node=start; //POINT NODE TO START
}
}
/*DEFINE REMOVE FUNCTION**/
void remove1()
{
if(loc1==start && loc1->next!=start) //DELETE THE FIRST NODE
{ // AND SHIFT START TO NEXT
start=start->next; //SET START TO START’NEXT
start->prev=tail; //SET START’S PREVIOUS TO TAIL
tail->next=start;
}
else if(loc1==start) //IF LIST HAS ONLY ONE STRING
{
start=NULL; //SET START TO NULL
}
else if(loc1->next==start) //IF LOCATION IS AT LAST
{
loc1->prev->next=start; //CODE FOR REMOVING
start->prev=loc1->prev; //LAST NODE
tail=loc1->prev;
}
else
{
loc1->prev->next=loc1->next; //CODE FOR REMOVE
loc1->next->prev=loc1->prev; //ANY NODE
}
}