C 创建嵌套结构的新列表
我在尝试从旧结构创建新的结构链表时遇到了一些麻烦。新链表的基础是,属于用户指定的特定品种的狗将被添加到新链表中,旧链表中的所有狗都不会被继承。我把一条狗加入列表中并没有问题,但我认为当我尝试添加多条狗时,我的代码有问题。我假设,当我创建指向列表结果的第二个临时列表时,当我添加到其中时,它会修改结果,但情况似乎并非如此。任何指示都将不胜感激 代码C 创建嵌套结构的新列表,c,linked-list,C,Linked List,我在尝试从旧结构创建新的结构链表时遇到了一些麻烦。新链表的基础是,属于用户指定的特定品种的狗将被添加到新链表中,旧链表中的所有狗都不会被继承。我把一条狗加入列表中并没有问题,但我认为当我尝试添加多条狗时,我的代码有问题。我假设,当我创建指向列表结果的第二个临时列表时,当我添加到其中时,它会修改结果,但情况似乎并非如此。任何指示都将不胜感激 代码struct container*list\u of_breed(char*breed)中的最后一个函数似乎就是我遇到的问题。其他一切都按预期进行。我相信
struct container*list\u of_breed(char*breed)
中的最后一个函数似乎就是我遇到的问题。其他一切都按预期进行。我相信else的说法似乎是我错的地方,因为当只有一只狗与该品种相匹配时,它似乎能够从中列出
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#pragma warning(disable: 4996)
// used to create a linked list of containers, each contaning a "dog"
struct container {
struct dog *dog;
struct container *next;
} *list = NULL;
// used to hold dog information and linked list of "checkups"
struct dog {
char name[30];
char breed[30];
struct checkup *checkups;
};
// used to create a linked list of checkups containing "dates"
struct checkup {
char date[30];
struct checkup *next;
};
void flush();
void branching(char);
void helper(char);
void remove_all(struct container*);
void display(struct container*);
void add_dog(char*, char*);
struct dog* search_dog(char*);
void add_checkup(char*, char*);
struct container* list_of_breed(char*);
int main()
{
char ch = 'i';
printf("Dog Adoption Center\n\n");
do
{
printf("Please enter your selection:\n");
printf("\ta: add a new dog to the list\n");
printf("\ts: search for a dog on the list\n");
printf("\tc: add a checkup date for dog\n");
printf("\tb: display list of dogs of breed\n");
printf("\tq: quit\n");
ch = tolower(getchar());
flush();
branching(ch);
} while (ch != 'q');
remove_all(list);
list = NULL;
_CrtDumpMemoryLeaks();
return 0;
}
void flush()
{
int c;
do c = getchar(); while (c != '\n' && c != EOF);
}
void branching(char c)
{
switch (c)
{
case 'a':
case 's':
case 'r':
case 'c':
case 'l':
case 'b':
case 'n': helper(c); break;
case 'q': break;
default: printf("Invalid input!\n");
}
}
void helper(char c)
{
if (c == 'a')
{
char input[100];
printf("\nPlease enter the dog's info in the following format:\n");
printf("name:breed\n");
fgets(input, sizeof(input), stdin);
input[strlen(input) - 1] = '\0';
char* name = strtok(input, ":");
char* breed = strtok(NULL, ":");
struct dog* result = search_dog(name);
if (result == NULL)
{
add_dog(name, breed);
printf("\nDog added to list successfully\n\n");
}
else
printf("\nThat dog is already on the list\n\n");
}
else if (c == 's' || c == 'r' || c == 'c' || c == 'l')
{
char name[30];
printf("\nPlease enter the dog's name:\n");
fgets(name, sizeof(name), stdin);
name[strlen(name) - 1] = '\0';
struct dog* result = search_dog(name);
if (result == NULL)
printf("\nThat dog is not on the list\n\n");
else if (c == 's')
printf("\nBreed: %s\n\n", result->breed);
else if (c == 'c')
{
char date[30];
printf("\nPlease enter the date of the checkup:\n");
fgets(date, sizeof(date), stdin);
date[strlen(date) - 1] = '\0';
add_checkup(name, date);
printf("\nCheckup added\n\n");
}
}
else if (c == 'b')
{
char breed[30];
printf("\nPlease enter the breed:\n");
fgets(breed, sizeof(breed), stdin);
breed[strlen(breed) - 1] = '\0';
struct container* result = list_of_breed(breed);
printf("\nList of dogs with breed type %s:\n\n", breed);
display(result);
remove_all(result);
result = NULL;
}
}
void remove_all(struct container* dogs)
{
struct checkup* temp;
if (dogs != NULL)
{
remove_all(dogs->next);
while (dogs->dog->checkups != NULL)
{
temp = dogs->dog->checkups;
dogs->dog->checkups = dogs->dog->checkups->next;
free(temp);
}
free(dogs->dog);
free(dogs);
}
}
void display(struct container* dogs)
{
struct container* container_traverser = dogs;
if (container_traverser == NULL)
{
printf("\nThere are no dogs on this list!\n\n");
return;
}
while (container_traverser != NULL)
{
printf("Name: %s\n", container_traverser->dog->name);
printf("Breed: %s\n", container_traverser->dog->breed);
printf("Checkups on file: ");
struct checkup* ptr = container_traverser->dog->checkups;
if (ptr == NULL)
{
printf("No checkups documented.");
}
else
{
while (ptr != NULL)
{
printf("\n%s", ptr->date);
ptr = ptr->next;
}
}
printf("\n\n");
container_traverser = container_traverser->next;
}
}
void add_dog(char* name, char* breed)
{
struct dog *tempDog = (struct dog *) malloc(sizeof(struct dog));
strcpy(tempDog->name, name);
strcpy(tempDog->breed, breed);
struct container *tempCont = (struct container *) malloc(sizeof(struct container));
tempCont->dog = tempDog;
tempCont->next = list;
list = tempCont;
}
struct dog* search_dog(char* name)
{
struct container *temp = list;
while (temp != NULL) {
if (strcmp(temp->dog->name, name) == 0) {
return temp->dog;
}
temp = temp->next;
}
return NULL;
}
void add_checkup(char* name, char* date)
{
struct container *tempList = (struct container *) malloc(sizeof(struct container));
tempList = list;
struct checkup *tempCheck = (struct checkup *) malloc(sizeof(struct checkup));
while (tempList != NULL) {
if (strcmp(tempList->dog->name, name) == 0) {
strcpy(tempCheck->date, date);
tempList->dog->checkups = tempCheck;
}
tempList = tempList->next;
}
}
//THIS IS THE FUNCTION I AM HAVING ISSUES WITH SPECIFICALLY RETURNING MULTIPLE STRUCTURES TO THE LIST
struct container* list_of_breed(char* breed)
{
struct container* result = NULL;
struct container* temp = list;
while (temp != NULL) {
struct dog* tempDog = (struct dog*) malloc(sizeof(struct dog));
tempDog = temp->dog;
if (strcmp(temp->dog->breed, breed) == 0) {
struct container *cont_add = (struct container*) malloc(sizeof(struct container));
struct dog *dog_add = (struct dog*) malloc(sizeof(struct dog));
strcpy(dog_add->name, temp->dog->name);
strcpy(dog_add->breed, breed);
dog_add->checkups = temp->dog->checkups;
cont_add->dog = dog_add;
if (result == NULL) {
result = cont_add;
}
else {
struct container* temp2 = result;
while (temp2->next != NULL) {
temp2 = temp2->next;
}
temp2->next = cont_add;
}
}
temp = temp->next;
}
return result;
}
#包括
#包括
#包括
#包括
#定义CRTDBG\u映射\u分配
#包括
#杂注警告(禁用:4996)
//用于创建容器的链接列表,每个容器包含一条“狗”
结构容器{
结构狗*dog;
结构容器*next;
}*列表=空;
//用于保存狗狗信息和“检查”链接列表
结构狗{
字符名[30];
查尔犬[30];
结构检查*检查;
};
//用于创建包含“日期”的检查链接列表
结构检查{
字符日期[30];
结构检查*下一步;
};
无效冲洗();
空洞分枝(char);
void-helper(char);
void remove_all(结构容器*);
无效显示(结构容器*);
无效添加狗(字符*,字符*);
结构狗*搜索狗(字符*);
无效添加检查(字符*,字符*);
结构容器*品种列表(字符*);
int main()
{
char ch='i';
printf(“狗收养中心”);
做
{
printf(“请输入您的选择:\n”);
printf(“\ta:将新狗添加到列表\n”);
printf(“\ts:搜索列表上的狗\n”);
printf(“\tc:为狗添加检查日期\n”);
printf(“\tb:显示犬种列表\n”);
printf(“\tq:quit\n”);
ch=tolower(getchar());
冲洗();
分枝(ch);
}while(ch!=“q”);
删除所有(列表);
列表=空;
_CrtDumpMemoryLeaks();
返回0;
}
无效刷新()
{
INTC;
do c=getchar();while(c!='\n'&&c!=EOF);
}
空分支(字符c)
{
开关(c)
{
案例“a”:
案例s:
案例“r”:
案例“c”:
案例“l”:
案例“b”:
案例“n”:助手(c);中断;
案例“q”:中断;
默认值:printf(“无效输入!\n”);
}
}
无效辅助对象(字符c)
{
如果(c=='a')
{
字符输入[100];
printf(“\n请按以下格式输入狗的信息:\n”);
printf(“名称:品种\n”);
fgets(输入,sizeof(输入),标准输入);
输入[strlen(输入)-1]='\0';
char*name=strtok(输入“:”);
char*bride=strtok(空,“:”);
结构狗*结果=搜索狗(名称);
如果(结果==NULL)
{
添加_狗(名称、品种);
printf(“\n日志已成功添加到列表\n\n”);
}
其他的
printf(“\n该狗已在列表中\n\n”);
}
else如果(c='s'| | c='r'| | c='c'| | c='l')
{
字符名[30];
printf(“\n请输入狗的名字:\n”);
fgets(名称、尺寸(名称)、标准尺寸);
名称[strlen(name)-1]='\0';
结构狗*结果=搜索狗(名称);
如果(结果==NULL)
printf(“\n该狗不在列表中\n\n”);
else如果(c='s')
printf(“\n种子:%s\n\n”,结果->品种);
else如果(c=='c')
{
字符日期[30];
printf(“\n请输入检查日期:\n”);
fgets(日期、尺寸(日期)、标准日期);
日期[strlen(date)-1]='\0';
添加检查(姓名、日期);
printf(“\n已添加检查\n\n”);
}
}
else如果(c=='b')
{
查尔犬[30];
printf(“\n请输入品种:\n”);
fgets(品种、尺寸(品种)、标准尺寸);
品种[strlen(品种)-1]='\0';
结构容器*结果=品种列表(品种);
printf(“\n品种类型为%s的狗的列表:\n\n”,品种);
显示(结果);
删除所有(结果);
结果=空;
}
}
void remove_all(结构容器*dogs)
{
结构检查*温度;
if(dogs!=NULL)
{
移除所有(狗->下一步);
while(狗->狗->检查!=NULL)
{
临时=狗->狗->检查;
狗->狗->检查=狗->狗->检查->下一步;
免费(临时);
}
免费(狗->狗);
免费(狗);
}
}
无效显示(结构容器*狗)
{
结构容器*容器遍历器=狗;
if(容器遍历器==NULL)
{
printf(“\n此列表中没有狗!\n\n”);
返回;
}
while(容器遍历器!=NULL)
{
printf(“名称:%s\n”,容器\u遍历器->狗->名称);
printf(“品种:%s\n”,容器遍历器->狗->品种);
printf(“文件检查:”);
结构检查*ptr=container\u traverser->dog->checkup;
如果(ptr==NULL)
{
printf(“无检查记录”);
}
其他的
{
while(ptr!=NULL)
{
printf(“\n%s”,ptr->date);
ptr=ptr->next;
}
}
printf(“\n\n”);
容器遍历器=容器遍历器->下一步;
}
}
无效添加狗(字符*名称,字符*品种)
{
结构狗*tempDog=(结构狗*)malloc(sizeof(结构狗));
strcpy(tempDog->name,name);
strcpy(临时狗->品种,品种);
结构容器*temp
tempDog->checkups = NULL;
strcpy(tempDog->breed, breed);
cont_add->next = NULL;
cont_add->dog = dog_add;
dog_add->checkups = temp->dog->checkups;