判断的返回值,不管真假都是返回1
/*
内存池
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Contact
{
char name;
char num;
struct Contact *next;
};
void addPerson(struct Contact **head,int n);
void findPerson(struct Contact *head,int n);
void changePerson(struct Contact *head,int n);
void delPerson(struct Contact **head,char *target);
void displayContact(struct Contact *head,int n);
void getinput(struct Contact *head);
void getinput(struct Contact *head)
{
printf("请输入联系人姓名:");
scanf("%s",head->name);
printf("\n请输入联系人电话号码:");
scanf("%s",head->num);
}
void addPerson(struct Contact **head,int n)
{
struct Contact *current;
static struct Contact *tail;
current = (struct Contact *)malloc(sizeof(struct Contact));
getinput(current);//填充节点信息
if (*head != NULL)
{
tail->next = current;
current->next = NULL;
}
else//空链表,即第一次输入的时候
{
*head = current;
current->next = NULL;
}
tail = current;
}
void delPerson(struct Contact **head,char *target)
{
struct Contact *current,*previous,*start;
previous = NULL;
current = *head;
start = *head;
int i,k;
printf("\n目标:%s",target);
i = (target != current->name || target != current->num);
printf("\ni ===== %d\n",i);
if (*head != NULL) //链表有节点
{
while(current != NULL && (target != current->name || target != current->num)) //current == NULL 或 匹配成功为假
{
//printf("1循环i = %d",i);
previous = current;printf("成功1 \n");
current = current->next;printf("成功2\n");
//k = !(!(strcmp(target,current->name)) || !(strcmp(target,current->num)));printf("成功3");
// printf("\n2循环i = %d",i);
}
if (current == NULL)//找到最后还是没找到
{
printf("\n没有找到目标");
}
else
{
if (previous == NULL) //只有一个节点
{
*head = current->next;
printf("\n删除成功");
}
else
{
previous->next = current->next;
printf("\n删除成功");
}
}
}
else //空链表
{
printf("\n通讯录里没有成员");
}
}
int main (void)
{
struct Contact *head = NULL;
int n = 0;
while(1)
{
printf("\n请输入对应的数字:1.插入 2.查找 3.更改 4.删除 5.显示所有 -1.退出\n");
scanf("%d",&n);
if (n == 1)
{
addPerson(&head,n);printf("成功1 ");
printf("\n添加成功");
}
else if (n == 2)
{
}
else if (n == 3)
{
}
else if (n == 4)
{
char what;
printf("请输入成员名称或电话:");
scanf("%s",what);
delPerson(&head,what);
}
else if (n == 5)
{
}
else if (n == -1)
{
break;
}
else
{
}
}
return 0;
}
大家帮我看看第61行的 i 不管匹配不匹配它都是返回1. 第61行: i = (target != current->name || target != current->num);
target和current->name都是指针,而且target是临时变量,肯定和current->name 不相等。current->num也是类似。所以第61行一直为1是没有问题的。
正确的做法是比较其内容
页:
[1]