C 语言单链表查询问题?
请老师们帮忙看看,怎么修改下面的代码。能满足我的需求。查询iStaffID是1003时, 期望将 所有1003的记录都返回。
测试数据入如: 1001 张三 123456 1002 李四 123456 1003 曹操 123456 1003 曹植 123456期望查找到员工记录: 1003 曹操 1234561003 曹植 123456
程序跑出来只是有“1003 曹操 123456”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
void *Data; //存放数据的地址
struct list *next;
};
struct staff {
int iStaffID;
char acName[20];
char acPasswd[10];
};
// 打印回调函数
void staff_printf(void *data) {
struct staff *people = (struct staff*)data;
printf("%5d%10s%10s\n", people->iStaffID,people->acName, people->acPasswd);
}
void list_display_generic(struct list *head, void (*callback)(void *)) {
struct list *p1 = head->next;
while (p1 != NULL) {
void *data = p1->Data;
callback(data);
p1 = p1->next;
}
}
void list_add(struct list *head, void *data) {
struct list *pNode, *p1 = head;
pNode = (struct list *)malloc(sizeof(struct list));
while (p1->next != NULL) {
p1 = p1->next;
}
p1->next = pNode;
pNode->Data = data;
pNode->next = NULL;
}
struct list *list_init(void *data) {
struct list *head;
head = (struct list *)malloc(sizeof(struct list));
head->Data = data;
head->next = NULL;
return head;
}
// 释放回调函数
void free_staff(void *data) {
struct staff *people = (struct staff*)data;
free(people);
}
void list_destroy(struct list *head, void (*callback)(void *)) {
struct list *p1 = head->next, *p2;
while (p1 != NULL) {
p2 = p1;
void *data = p1->Data;
callback(data); //释放存储的数据
p1 = p1->next;
free(p2);
}
free(head); //释放头节点所占据的内存
}
// 通用查找函数
void *list_find_generic(struct list *head, void *key, int (*callback)(void *, void *)) {
struct list *p = head->next;
while (p != NULL) {
void *data = p->Data;
if (callback(data, key) == 1) {
return data;
}
p = p->next;
}
return NULL;
}
// 查找回调函数
int staff_id_find(void *data, void *key) {
struct staff *staff = (struct staff *)data;
int staff_id = *(int *)key;
if (staff->iStaffID == staff_id) {
return 1;
} else
return 0;
}
int main() {
struct list *head;
struct staff *people1, *people2, *people3, *people4;
//初始化链表
head = list_init(NULL);//头节点不存储数据,参数为NULL
people1 = (struct staff *)malloc(sizeof(struct staff));
people2 = (struct staff *)malloc(sizeof(struct staff));
people3 = (struct staff *)malloc(sizeof(struct staff));
people4 = (struct staff *)malloc(sizeof(struct staff));
people1->iStaffID = 1001;
strcpy(people1->acName, "张三");
strcpy(people1->acPasswd, "123456");
people2->iStaffID = 1002;
strcpy(people2->acName, "李四");
strcpy(people2->acPasswd, "123456");
people3->iStaffID = 1003;
strcpy(people3->acName, "曹操");
strcpy(people3->acPasswd, "123456");
people4->iStaffID = 1004;
strcpy(people4->acName, "曹植");
strcpy(people4->acPasswd, "123456");
//添加链表节点
list_add(head, people1);
list_add(head, people2);
list_add(head, people3);
list_add(head, people4);
//员工信息打印函数
list_display_generic(head, staff_printf);
// 查找员工
int search_id = 1009;
struct staff *searchResult = (struct staff *)list_find_generic(head, &search_id, staff_id_find);
staff_printf(searchResult);
//销毁链表
list_destroy(head, free_staff);
return 0;
}
回复
1个回答
test
2024-06-26
// ... [其它代码不变]
// 通用查找函数,找到所有匹配的数据
struct list *list_find_all_generic(struct list *head, void *key, int (*callback)(void *, void *)) {
struct list *resultHead = list_init(NULL);
struct list *p = head->next;
while (p != NULL) {
void *data = p->Data;
if (callback(data, key) == 1) {
list_add(resultHead, data);
}
p = p->next;
}
return resultHead;
}
int main() {
// ... [其它代码不变]
// 查找员工
int search_id = 1003;
struct list *searchResults = list_find_all_generic(head, &search_id, staff_id_find);
// 打印所有找到的结果
list_display_generic(searchResults, staff_printf);
//销毁查找结果链表
free(searchResults); // 这里只释放头节点,因为数据是共享的,不应该再释放
//销毁主链表
list_destroy(head, free_staff);
return 0;
}
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容