user-0 提问: C单链表怎么加查询功能

回复:
user: user-1
content: 你输入链表的地方其实就是不对的，scanf("%d,%s,%s",&p1->num,&p1->name,p1->jg);这样子写是不行的。 假设你输入"11,cc,dd",那么p1->num会被写入11，p1->name会被写入"cc,dd",而其实p1->jg根本没有写入东西 所以你应该改成scanf("%d%s%s",&p1->num,&p1->name,p1->jg);这样才行 输入的时候用空格分隔，比如:11 cc dd  # include "stdio.h" # include "malloc.h" #include "stdlib.h" #include <memory.h> #include <string.h> # define NULL 0 # define LEN sizeof(struct student)  struct student { 	int num; 	char name[40]; 	char jg[60]; 	struct student *next; }student;  int n; /* 新建链表 */  struct student *my_creat(void) { 	struct student *head = 0, *p1 = 0, *p2 = 0; 	n = 0; 	p1 = p2 = (struct student *)malloc(LEN);	//	开辟一个新单元 	memset(p1, 0, LEN); 	scanf("%d%s%s", &p1->num, &p1->name, p1->jg); 	head = NULL; 	while (p1->num != NULL)	//当输入的学号为0时,停止循环 	{ 		n = n + 1; 		if (n == 1) 			head = p1; 		else p2->next = p1; 		p2 = p1; 		p1 = (struct student *)malloc(LEN); 		memset(p1, 0, LEN); 		scanf("%d%s%s", &p1->num, &p1->name, p1->jg); 	} 	p2->next = NULL; 	return(head); } /* 输出链表 */  void print(struct student *head) { 	struct student *p1 = 0; 	printf("\n现在显示输入内容:\n", n); 	p1 = head; 	while (p1 != NULL) 	{ 		printf("%d,%s,%s\n", p1->num, p1->name, p1->jg); 		p1 = p1->next; 	} 	return; } /* 删除节点 */  struct student *del(struct student *head, int num) { 	struct student *p1 = 0, *p2 = 0; 	if (head == NULL) 	{ 		printf("\n 没有记录! \n"); 		goto end; 	} 	p1 = head; 	while (p1->num != num && p1->next != NULL) 	{ 		p2 = p1; 		p1 = p1->next; 	} 	if (p1->num == num)	//找到删除的学号 	{ 		if (p1 == head) head = p1->next; 		else p2->next = p1->next; 		n = n - 1; //节点数减1 		free(p1); 		printf("删除成功"); 	} 	else 		printf("%d号找不到!删除失败\n", num); end: 	return(head); } /* 查找 */ void search(struct student*head, char jg[20]) { 	struct student *p1; 	for (p1 = head; p1 != NULL; p1 = p1->next) 	{ 		if (strcmp(jg, p1->jg) == 0) 		{ 			printf("%ld\t%s\t\t%s\n", p1->num, p1->name, p1->jg); 		} 	} }  //主界面 void main() { 	struct student *head = 0, *stu = 0; 	int del_num/*,delet_num*/; 	int k = 1; 	char sjg[60]; 	while (k != 0) 	{ 		printf("***************************\n"); 		printf("* *\n"); 		printf("* * 学生籍贯管理系统 * *\n"); 		printf("* *\n"); 		printf("* 1 ............创建链表 *\n"); 		printf("* *\n"); 		printf("* 2 ............查找籍贯 *\n"); 		printf("* *\n"); 		printf("* 3 ............删除记录 *\n"); 		printf("* *\n"); 		printf("* 4 ............显示链表 *\n"); 		printf("* *\n"); 		printf("* 输入  1...4 *\n"); 		printf("* *\n"); 		printf("***************************\n"); 		scanf("%d", &k); 		switch (k) 		{ 		case 1: printf("按照(学号 姓名 籍贯)来输入学生数据(按0结束):\n"); 			head = my_creat();	//创建链表 			print(head);	//输出所有节点 			printf("\n");; break;  		case 2:	printf("请输入要查找学生的籍贯(按0结束):\n");	//节点查找 			fflush(stdin); 			gets(sjg); 			search(head,sjg); 			break;  		case 3: printf("\n请输入要删除学生的学号(按0结束):");	//链表删除 			scanf("%d", &del_num); 			while (del_num != NULL) 			{ 				head = del(head, del_num); 				print(head); 				printf("\n请输入要删除学生的学号(按0结束):"); 				scanf("%d", &del_num); 			} 			printf("\n");; break;  		case 4:	print(head);	//输出链表 			printf("\n");; break; 		} 	} }

user: user-0
content: 引用 1 楼 qq423399099 的回复:1.遍历链表 2.strcmp比较输入的字符串和每个结点的jg成员是不是一样的，一样的话就输出 void search(struct student*head,char jg[20]) {    struct student *p1;    for(p1=head;p1!=NULL;p1=p1->next)    { if(strcmp(jg,p1->jg)==0)        {         printf("%ld\t%s\t\t%s\n",p1->num,p1->name,p1->jg);       }     } }   这样写可以吗，可运行了无法读取jg..

user: user-2
content: 2L正解，给我上波分~

user: user-1
content: 1.遍历链表 2.strcmp比较输入的字符串和每个结点的jg成员是不是一样的，一样的话就输出