I am new to C and was trying to create a phonebook application using a doubly-linked list. However, I have not been able to figure out how to delete the contact i.e. the first name, last name, and number of the person and linking the previous node the next one. I have attached the code below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void create(int,char*,char*);
void search();
void display();
void del();
struct node
{
struct node *before;
int data;
char fname[50];
char lname[50];
struct node* after;
};
struct node* head;
int main()
{
printf("WELCOME TO PHONE DIRECTORY");
int item,choice;
char surname[50];
char lastname[50];
do
{
printf("\n1.CREATE\n2.SEARCH\n3.DELETE\n 4.EXIT\n 5.DISPLAY \n 6.ENTER YOUR CHOICE:");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("ENTER NUMBER:");
scanf("%d",&item);
printf("ENTER FIRST NAME:");
scanf("%s",surname);
printf("ENTER LAST NAME:");
scanf("%s",lastname);
create (item,surname,lastname);
break;
case 2:
search();
break;
case 3:
del();
break;
case 4:
return;
break;
case 5:
display();
break;
default:
printf("\n INVALID NUMBER");
break;
}
} while (choice!=4);
}
//DISPLAY FUNCTION
void display()
{
struct node* temp=head;
if(temp==NULL)
{
printf("LIST IS EMPTY");
}
while(temp!=NULL)
{
printf("%d-> \n",temp->data);
printf("%s-> \n",temp->fname);
printf("%s-> \n\n",temp->lname);
temp=temp->after;
}
}
//CREATE FUNCTION
void create(int item,char *surname,char *lastname)
{
struct node *newNode=(struct node*) malloc(sizeof(struct node));
struct node* temp;
if(newNode==NULL)
{
printf("OVERFLOW");
}
else
{
newNode->data=item;
strcpy(newNode->fname,surname);
strcpy(newNode->lname,lastname);
if(head==NULL)
{
newNode->before=NULL;
newNode->after=NULL;
head=newNode;
printf("FIRST NODE INSERTED %d %s %s",item,surname,lastname);
}
else
{
temp=head;
while(temp->after!=NULL)
{
temp=temp->after;
}
temp->after=newNode;
newNode->before=temp;
newNode->after=NULL;
printf("\n Node inserted %d %s %s",item,surname,lastname);
}
}
}
//DELETE FUNCTION
void del()
{
struct node *pretemp,*temp;
char *f,*l;
int num;
temp=head;
pretemp=head->after;
printf("Enter name and number :");
scanf("%s",&f);
scanf("%s",&l);
scanf("%d",&num);
while(temp!=NULL)
{
if((pretemp->fname==f)&&(pretemp->lname==l)&&(pretemp->data==num))
{
printf("%s ",temp->fname);
printf("%s ",temp->lname);
printf("%d ",temp->data);
temp->after=pretemp->after;
free(pretemp);
break;
}
else
{
temp=temp->after;
pretemp=pretemp->after;
}
}
}
//SEARCH FUNCTION
void search()
{
struct node* temp;
int item,i=0,flag;
char name[50];
temp=head;
if(head==NULL)
{
printf("\n LIST IS EMPTY");
}
else
{
printf("ENTER THE NUMBER AND FIRST NAME TO BE SEARCHED: ");
scanf("%d %s",&item,&name);
while(temp!=NULL)
{
if(temp->data==item)
{
printf("ITEM FOUND AT LOCATION %d",i+1);
flag=0;
break;
}
else if(temp->fname==name)
{
printf("ITEM FOUND AT LOCATION %s",i+1);
flag=0;
break;
}
else
{
flag=1;
}
i++;
temp=temp->after;
}
if(flag==1)
{
printf("\n ITEM NOT FOUND");
}
}
}
Any help would be appreciated. Thanks.
pretemp->fname==fis not how you compare strings in C. See strcmpchar *f,*l;will not work as buffers in call toprintf("Enter name and number :"); scanf("%s",&f); scanf("%s",&l);Suggest using meaningful variable names in the form of a regularchararray: eg:char first[80] = {0}; char last[80] = {0};void search(); void display(); void del();When declaring forward function declarations, (prototypes) that do not take any parameters, there needs to beNULLbetween the parens otherwise the compiler will generate code that can accept any number/type of parameters, which 'may' work, but is a very poor programming practice.