Reputation: 1
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.
Upvotes: 0
Views: 251
Reputation:
To remove a node from a doubly linked list, you need to know the pointer of the node by searching the list.
It is recommended that you link the first and last item of the doubly linked list together to form a ring, so that there are no boundary conditions (where node->before
or node->after
is NULL
).
/* works in a circular doubly linked list, where the first item (*list)
is linked with the last item of the list */
/* returns the list after the node is removed */
struct node* remove_node(struct node *list, struct node *node)
{
assert(list != NULL);
assert(node != NULL);
/* node must be in a list to be removed */
assert(node->before != NULL);
assert(node->after != NULL);
/* removing head */
if (list == node)
{
/* move head to next node */
list = list->after;
/* still the same node (only one node in list) */
if (list == node)
list = NULL;
}
/* Linking the previous and next node together will
effectively remove the node from the list */
node->before->after = node->after;
node->after->before = node->before;
/* this is not necessary but recommended for error checking */
node->before = NULL;
node->after = NULL;
return list;
}
Upvotes: 0