user-0 提问: 查链表中的数据找到后strcmp后判断条件为什么要加上&&才能运行成功

回复:
user: user-0
content: 引用 9 楼 m0_46108109的回复:这么写没错啊，你写成什么了报错？ 把并且前面去掉了

user: user-1
content: 这么写没错啊，你写成什么了报错？

user: user-0
content: 原来如此谢谢大佬

user: user-2
content: 如果没有这个&&， 只剩下后面的strcmpy， 那么当chalist是NULL的时候， chalist->x 这个操作就会让程序崩溃了

user: user-0
content: 引用 1 楼 qq_33097303 的回复:你把赋空放到后面就不用&&了把&&不写就会出问题

user: user-2
content: 加&&， 就是为了防备 chalist 是 NULL的情况 如果它是NULL， && 后面的部分就不会被调用，从而避免了空指针操作出异常

user: user-0
content: 引用 1 楼 qq_33097303 的回复:你把赋空放到后面就不用&&了#define _CRT_SECURE_NO_WARNINGS # include <stdio.h> # include <malloc.h> # include <string>  struct studant { 	char x[50]; 	struct studant* next; };  //创建链表 <2>   靠插入来完成链表 /*struct studant* Apex() { 	struct studant* AApex = (struct studant*)malloc(sizeof(struct studant)); 	AApex->next = NULL; 	return AApex; }*/  //创建链表 <1>    先制作链表再来操作链表 struct studant* apex(void) { 	int i, g; 	struct studant* papex; 	papex = (struct studant*)malloc(sizeof(struct studant)); 	papex->next = NULL;  	struct studant* aapex = papex; 	aapex->next = NULL;  	printf("需要多少个链表："); 	scanf("%d", &g); 	while (getchar() != '\n') continue; 	for (i = 0; i < g; i++) 	{ 		struct studant* paapex = (struct studant*)malloc(sizeof(struct studant)); 		paapex->next = NULL; 		printf("输入第%d个链表内的数据：", i + 1); 		scanf("%s", paapex->x); 		while (getchar() != '\n') continue; 		paapex->next = aapex->next; 		aapex->next = paapex; 	} 	return papex; }  //插入节点 struct studant* zjlist(struct studant* ppapex, char a[50]) { 	struct studant* qqapex; 	qqapex = (struct studant*)malloc(sizeof(struct studant)); 	strcpy(qqapex->x, a); 	qqapex->next = ppapex->next; 	ppapex->next = qqapex; 	return qqapex; }  //函数 头插法插入链表（增） void sclist(struct studant* llist) { 	char z; 	char zz[20]; 	int i = 1; 	printf("是否开始(y/n):"); 	scanf("%c", &z); 	while (getchar() != '\n') continue; 	if (z == 'y' || z == 'Y') 		while (z == 'y' || z == 'Y') 		{ 			while (getchar() != '\n') continue; 			printf("\n链表%d\n", i++); 			printf("输入数据："); 			scanf("%s", zz); 			zjlist(llist, zz); 			while (getchar() != '\n') continue; 			printf("\n是否继续(y/n)："); 			scanf("%c", &z); 			if (z == 'y' || z == 'Y'); //判断是否继续创建插入链表 			else 				break; 		} }  //删除链表 void shanlist(struct studant* freelist, char a[50])//头结点 和 要删除的数据（找到链表）：从头结点开始找到相关数据删除 { 	struct studant* Freelist = freelist->next; 	struct studant* SFreelist = freelist; 	if (Freelist->next == NULL) 	{ 		printf("链表为空！无法删除！！\n"); 		return; 	} 	while (Freelist && strcmp(Freelist->x, a) != 0) 	{ 		SFreelist = Freelist; 		Freelist = Freelist->next; 	} 	if (strcmp(Freelist->x, a) == 0) 	{ 		SFreelist->next = Freelist->next; 		free(Freelist); 		printf("\n成功删除！！\n"); 	} 	else 		printf("没有相关数据！无法删除！！\n"); }  //打印遍历链表 void bllist(struct studant* bllist) { 	struct studant* bblist = bllist->next;//让指针指向头结点的地址然后从下一个节点开始打印（因为头结点不存放数据  会乱码  ） 	if (bblist != NULL) 		while (bblist != NULL) 		{ 			printf("%s\n", bblist->x); 			bblist = bblist->next; 		} 	else 		printf("链表为空！\n"); }   void List(); void xzcz(); void chalist(); void quitfree(); struct studant* list = apex();   int main(void) { 	List(); 	xzcz();  	return 0; }   void xzcz() { 	char a, b, c; 	char v[50]; 	int f = 1; 	while (f) 	{ 		int b = 0; 		printf("操作链表：\n"); 		printf("<1>增加链表\n"); 		printf("<2>删除链表\n"); 		printf("<3>遍历链表\n"); 		printf("<4>查询链表\n"); 		printf("<5>退出程序\n"); 		printf("请输入："); 		scanf("%d", &b); 		if (b > 5 || b < 0) 			printf("\n输入错误！！\n\n"); 			switch (b) 			{ 			case 1: 				while (getchar() != '\n') continue; 				printf("要增加链表吗(y/n)："); 				scanf("%c", &a); 				if (a == 'y' || a == 'Y') 				{ 					while (getchar() != '\n') continue; 					sclist(list); 					bllist(list); 				} 				break; 			case 2: 				while (getchar() != '\n') continue; 				printf("\n要删除链表吗(y/n)："); 				scanf("%c", &b); 				if (b == 'y' || b == 'Y') 				{ 					while (getchar() != '\n') continue; 					printf("输入要删除的数据："); 					scanf("%s", v); 					shanlist(list, v); 				} 				break; 			case 3: 				while (getchar() != '\n') continue; 				printf("是否要再次遍历(y/n)："); 				scanf("%c", &c); 				if (c == 'y' || c == 'Y') 				{ 					while (getchar() != '\n') continue; 					bllist(list); 				} 				break; 			case 4: 				chalist(); 				break; 			case 5: 				quitfree(); 				f = 0; 				break; 			} 	}  }  void List()//自定义个数建好链表 { 	char a, b, c; 	char v[50];  	bllist(list);//遍历链表 	// 	printf("要增加链表吗(y/n)："); 	scanf("%c", &a); 	if (a == 'y' || a == 'Y') 	{ 		while (getchar() != '\n') continue; 		sclist(list); 		while (getchar() != '\n') continue; 		bllist(list); 	}  	// 	printf("\n要删除链表吗(y/n)："); 	scanf("%c", &b); 	if (b == 'y' || b == 'Y') 	{ 		while (getchar() != '\n') continue; 		printf("输入要删除的数据："); 		scanf("%s", v); 		while (getchar() != '\n') continue; 		shanlist(list, v); 	}  	bllist(list);  	// 	printf("是否要再次遍历(y/n)："); 	scanf("%c", &c); 	if (c == 'y' || c == 'Y') 	{ 		while (getchar() != '\n') continue; 		bllist(list); 	} }  void chalist() { 	char a[50]; 	printf("输入要查找的数据："); 	scanf("%s", a); 	while (getchar() != '\n') continue; 	struct studant* chalist = list; 	while (strcmp(chalist->x, a) != 0) 	{ 		chalist = chalist->next; 		if (chalist == NULL) 			break; 	} 	if (chalist && strcmp(chalist->x, a) == 0)//chalist为NIULL后没有找到数据 	{ 		printf("查找到的数据：\n"); 		printf("%s\n\n", chalist->x); 	} 	else 		printf("\n没有找到指定的数据！\n"); }  void quitfree() { 	struct studant* quit = list; 	struct studant* exit = list->next; 	while (quit != NULL) 	{ 		quit = exit; 		if (quit == NULL && exit == NULL) 		{ 			printf("\n退出程序！\n成功清空链表！！\n"); 			break; 		} 		exit = exit->next; 		free(quit); 	} 	list->next = NULL; }

user: user-0
content: 引用 1 楼 qq_33097303的回复:你把赋空放到后面就不用&&了 是头结点的赋空还是

user: user-3
content: 你把赋空放到后面就不用&&了