我是C的新手,曾试图使用双链表创建一个电话簿应用程序。然而,我还不知道如何删除联系人,即联系人的名字、姓氏和号码,并将上一个节点链接到下一个节点。我附上了下面的代码。
#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.CREATEn2.SEARCHn3.DELETEn 4.EXITn 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-> nn",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");
}
}
}
如有任何帮助,我们将不胜感激。谢谢
要从双链接列表中删除节点,您需要通过搜索列表来知道节点的指针。
建议将双链接列表的第一项和最后一项链接在一起以形成环,这样就没有边界条件(其中node->before
或node->after
是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;
}