- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在修改链接列表的成员时遇到问题。我们有一个结构体“record”,声明如下:
typedef struct record
{
char *make;
int year;
int stock;
struct record *next;
} Record;
包含有关经销商汽车的信息。
我们首先使用来自 CLI 参数指示的文本文件的输入创建链表,如下所示:
Ford 2012 15
Ford 2011 3
Ford 2010 5
Toyota 2011 7
Toyota 2012 20
Toyota 2009 2
Honda 2011 9
Honda 2012 3
Honda 2013 12
Chevrolet 2013 10
Chevrolet 2012 25
使用 fgets 将这些值读入变量,然后发送到“insertRecordInAsendingOrder”函数,该函数然后调用“createRecord”在链接列表中实际创建每个单独的记录。然后 insertRecordInAscendingOrder 将记录放置到列表上的正确位置。然后使用函数“printList”按排序顺序打印原始列表。
从这里开始,我们继续更新链接列表成员之一的库存。值“Ford”(代表品牌)、“2012”(代表年份)和“12”(代表 newStock 字段)被硬编码到函数“Main”中,并向下发送到“updateStockOfCar”函数,这是以下代码:
int updateStockOfCar(Record *head, char *make, int year, int newStock)
{
if(head == NULL)
{
printf("The list is empty\n");
return 0;
}
Record * matching = malloc(sizeof(Record));
matching->make = make;
matching->year = year;
//If the first record is the one we want to modify, then do so
if(strcmp(head->make, matching->make) == 0)
{
if(head->year == matching->year)
{
head->stock = newStock;
return 1;
}
Record* previous = head;
Record* current = head->next;
//Loop through the list, looking for the record to update
while(current != NULL)
{
if(strcmp(current->make, matching->make) == 0 && current->year == matching->year)
{
current->stock = newStock;
return 1;
}
else
{
previous = current;
current = current -> next;
}
}
}
return 0;
}
如您所见,此函数接受列表的头节点(我们开始搜索要更新的正确节点)、制造商和年份(对于我们要修改库存的节点),和 newStock(我们将用来替换目标节点中标准“stock”字段的值)。
我们的第一种情况是如果 head 为 NULL,在这种情况下,我们说列表为空,并将不成功的“0”返回给“Main”中的调用变量。
我们的第二种情况是如果matching中head中的make字段之间的strcmp相同,并且head和match中的year字段相同,那么我们将head的stock字段更新为newStock并返回成功的“1”功能“主要”。本质上,如果头节点是要修改的正确节点,我们就会这样做。
我们的第三种情况是头节点不是要修改的正确节点。在 current(定义为起始头)不为 NULL 的情况下,我们将当前节点的 make 和year 字段与我们要更改的目标节点进行比较。如果它们匹配,我们将 current 的 stock 字段设置为 newStock 并返回成功的“1”。如果它们不匹配,我们将执行“else”条件,遍历列表,直到找到我们要查找的那个,在这种情况下,我们更新 current 的 stock 字段。
如果所有这些情况都失败,那么第四种情况就会出现,向main返回一个不成功的“0”,表明目标记录不存在于链表中。
但是,当编译并执行代码(我在下面提供给您)时,会为程序的输出生成以下内容:
The original list in ascending order:
--------------------------------------------------
Make Year Stock
--------------------------------------------------
Chevrolet 2012 25
Chevrolet 2013 10
Ford 2010 5
Ford 2011 3
Ford 2012 15
Honda 2011 9
Honda 2012 3
Honda 2013 12
Toyota 2009 2
Toyota 2011 7
Toyota 2012 20
====Update Stock of Car====
Failed to update stock. Please make sure the car that has the make/year exists in the list.
====Removing a Record====
I'm trying to remove the record of Ford 2012...
Removed the record successfully. Printing out the new list...
--------------------------------------------------
Make Year Stock
--------------------------------------------------
Chevrolet 2012 25
Chevrolet 2013 10
Ford 2010 5
Ford 2011 3
Honda 2011 9
Honda 2012 3
Honda 2013 12
Toyota 2009 2
Toyota 2011 7
Toyota 2012 20
====Insert Record in Ascending Order====
I'm trying to insert a record of make Jeep year 2011 stock 5...
Inserted the record successfully. Printing out the new list...
--------------------------------------------------
Make Year Stock
--------------------------------------------------
Chevrolet 2012 25
Chevrolet 2013 10
Ford 2010 5
Ford 2011 3
Honda 2011 9
Honda 2012 3
Honda 2013 12
Jeep 2011 5
Toyota 2009 2
Toyota 2011 7
Toyota 2012 20
I'm trying to insert a record of make Honda, year 2012, and stock 10...
Inserted the record successfully. Printing out the new list...
--------------------------------------------------
Make Year Stock
--------------------------------------------------
Chevrolet 2012 25
Chevrolet 2013 10
Ford 2010 5
Ford 2011 3
Honda 2011 9
Honda 2012 10
Honda 2012 3
Honda 2013 12
Jeep 2011 5
Toyota 2009 2
Toyota 2011 7
Toyota 2012 20
由于某种原因,“Main”函数被告知未找到该节点,即使它存在于列表中。
这是整个程序,虽然还有更多有问题的函数,但我已经排除了其中的大部分:
/*homework2stackoverflow2.c*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 50
#define MAX_MAKE 20
typedef struct record
{
char *make;
int year;
int stock;
struct record *next;
} Record;
int compareCars(Record *car1, Record *car2);
void printList(Record *head);
Record* createRecord(char *make, int year, int stock);
int insertRecordInAscendingOrder(Record **head, char *make, int year, int stock);
int removeRecord(Record **head, char *make, int year);
int updateStockOfCar(Record *head, char *make, int year, int newStock);
int main(int argc, char **argv)
/*START MAIN*/
{
FILE *inFile = NULL;
char line[MAX_LINE + 1];
char *make, *yearStr, *stockStr;
int year, stock, len;
Record* headRecord = NULL;
/*Input and file diagnostics*/
if (argc!=2)
{
printf ("Filename not provided.\n");
return 1;
}
if((inFile=fopen(argv[1], "r"))==NULL)
{
printf("Can't open the file\n");
return 2;
}
/*obtain values for linked list*/
while (fgets(line, MAX_LINE, inFile))
{
make = strtok(line, " ");
yearStr = strtok(NULL, " ");
stockStr = strtok(NULL, " ");
year = atoi(yearStr);
stock = atoi(stockStr);
insertRecordInAscendingOrder(&headRecord,make, year, stock);
}
/*START PRINT ORIGINAL LIST*/
printf("The original list in ascending order: \n");
printList(headRecord);
/*END PRINT ORIGINAL LIST*/
/*START UPDATE STOCK TEST*/
printf("\n====Update Stock of Car====\n");
int newStock = 12;
char* newMake = "Ford";
int updateStatus = updateStockOfCar(headRecord, newMake, year, newStock);
if(updateStatus == 0)
{
printf("Failed to update stock. Please make sure the car that has the make/year exists in the list.\n");
}
if(updateStatus == 1)
{
printf("Updated stock successfully. Printing out the new list...\n");
printList(headRecord);
}
/*END UPDATE STOCK TEST*/
/*START REMOVE RECORD TEST*/
printf("\n====Removing a Record====\n");
char *deleteMake = "Ford";
int deleteYear = 2012;
int removeStatus = removeRecord(&headRecord, deleteMake, deleteYear);
if(removeStatus == 0)
{
printf("Failed to remove the record. Please make sure the care that has the make/year exists in the list.\n");
}
if(removeStatus == 1)
{
printf("Removed the record successfully. Printing out the new list...\n");
printList(headRecord);
}
/*END REMOVE RECORD TEST*/
/*START INSERT IN ASCENDING ORDER TEST*/
printf("\n====Insert Record in Ascending Order====\n");
char* insertMake = "Jeep";
int insertYear = 2011;
int insertStock = 5;
int insertStatus = insertRecordInAscendingOrder(&headRecord, insertMake, insertYear, insertStock);
printf("I'm trying to insert a record of make %s year %d stock %d...\n", insertMake, insertYear, insertStock);
if(insertStatus == 0)
{
printf("Failed to insert the new record. Please make sure the car that has the make/year doesn't exist in the list.\n");
}
if(insertStatus == 1)
{
printf("Inserted the record successfully. Printing out the new list...\n");
printList(headRecord);
}
insertMake = "Honda";
insertYear = 2012;
insertStock = 10;
insertStatus = insertRecordInAscendingOrder(&headRecord, insertMake, insertYear, insertStock);
printf("\nI'm trying to insert a record of make %s, year %d, and stock %d...\n", insertMake, insertYear, insertStock);
if(insertStatus == 0)
{
printf("Failed to insert the new record. Please make sure the car that has the make/year doesn't exist in the list.\n");
}
if(insertStatus == 1)
{
printf("Inserted the record successfully. Printing out the new list...\n");
printList(headRecord);
}
/*END INSERT IN ASCENDING ORDER TEST*/
fclose(inFile);
return 0;
}
/*END MAIN*/
int compareCars(Record *car1, Record *car2)
{
int makecmp = strcmp(car1->make, car2->make);
if( makecmp > 0 )
return 1;
if( makecmp == 0 )
{
if (car1->year > car2->year)
{
return 1;
}
if( car1->year == car2->year )
{
return 0;
}
return -1;
}
/*prints list. lists print.*/
void printList(Record *head)
{
int i;
int j = 50;
Record *aRecord;
aRecord = head;
for(i = 0; i < j; i++)
{
printf("-");
}
printf("\n");
printf("%20s%20s%10s\n", "Make", "Year", "Stock");
for(i = 0; i < j; i++)
{
printf("-");
}
printf("\n");
while(aRecord != NULL)
{
printf("%20s%20d%10d\n", aRecord->make, aRecord->year,
aRecord->stock);
aRecord = aRecord->next;
}
printf("\n");
}
Record* createRecord(char *make, int year, int stock)
{
Record *theRecord;
int len;
if(!make)
{
return NULL;
}
theRecord = malloc(sizeof(Record));
if(!theRecord)
{
printf("Unable to allocate memory for the structure.\n");
return NULL;
}
theRecord->year = year;
theRecord->stock = stock;
len = strlen(make);
theRecord->make = malloc(len + 1);
strncpy(theRecord->make, make, len);
theRecord->make[len] = '\0';
theRecord->next=NULL;
return theRecord;
}
int insertRecordInAscendingOrder(Record **head, char *make, int year, int stock)
{
Record *previous = *head;
Record *newRecord = createRecord(make, year, stock);
int compResult;
Record *current = *head;
if(*head == NULL)
{
*head = newRecord;
//printf("Head is null, list was empty\n");
return 1;
}
else if(compareCars(newRecord, *head) == 0)
{
return 0;
}
else if ( compareCars(newRecord, *head)==-1)
{
*head = newRecord;
(*head)->next = current;
//printf("New record was less than the head, replacing\n");
return 1;
}
else
{
//printf("standard case, searching and inserting\n");
previous = *head;
while ( current != NULL &&(compareCars(newRecord, current)==1))
{
previous = current;
current = current->next;
}
previous->next = newRecord;
previous->next->next = current;
}
return 1;
}
int removeRecord(Record **head, char *make, int year)
{
printf("I'm trying to remove the record of %s %d...\n", make, year);
if(*head == NULL)
{
printf("The list is empty\n");
return 0;
}
Record * delete = malloc(sizeof(Record));
delete->make = make;
delete->year = year;
//If the first record is the one we want to delete, then we need to update the head of the list and free it.
if((strcmp((*head)->make, delete->make) == 0))
{
if((*head)->year == delete->year)
{
delete = *head;
*head = (*head)->next;
free(delete);
return 1;
}
}
Record * previous = *head;
Record * current = (*head)->next;
//Loop through the list, looking for the record to remove.
while(current != NULL)
{
if(strcmp(current->make, delete->make) == 0 && current->year == delete->year)
{
previous->next = current->next;
free(current);
return 1;
}
else
{
previous = current;
current = current->next;
}
}
return 0;
}
int updateStockOfCar(Record *head, char *make, int year, int newStock)
{
if(head == NULL)
{
printf("The list is empty\n");
return 0;
}
Record * matching = malloc(sizeof(Record));
matching->make = make;
matching->year = year;
//If the first record is the one we want to modify, then do so
if(strcmp(head->make, matching->make) == 0)
{
if(head->year == matching->year)
{
head->stock = newStock;
return 1;
}
Record* previous = head;
Record* current = head->next;
//Loop through the list, looking for the record to update
while(current != NULL)
{
if(strcmp(current->make, matching->make) == 0 && current->year == matching->year)
{
current->stock = newStock;
return 1;
}
else
{
previous = current;
current = current -> next;
}
}
}
return 0;
}
是什么导致这个东西表明它不存在于列表中?
是的...这是一项家庭作业:P
最佳答案
我不完全清楚为什么你要分配一条新记录(顺便说一句,该记录正在泄露)只是为了遍历你的链表并在找到节点后更新它。有什么问题吗:
int updateStockOfCar(Record *head, const char *make, int year, int newStock)
{
Record* p = head;
while (p)
{
if ((p->year == year) && !strcmp(p->make, make))
{
p->stock = newStock;
break;
}
p = p->next;
}
return (p ? 1 : 0);
}
根据品牌/年份检索汽车的当前库存:
int getStockOfCar(Record *head, const char *make, int year)
{
Record* p = head;
while (p)
{
if ((p->year == year) && !strcmp(p->make, make))
return p->stock;
p = p->next;
}
return 0;
}
或者我是否错过了问题中的要求(这不是第一次)。?
关于c - 修改链表中结构体的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886685/
《Rust编程与项目实战》(朱文伟,李建英)【摘要 书评 试读】- 京东图书 (jd.com) 在Rust中,结构体(Struct)是一种自定义数据类型,它允许我们将多个相关的值组合在一起,形成一个
这个问题已经有答案了: In C, does a pointer to a structure always point to its first member? (3 个回答) 已关闭 7 年前。
当执行第二个fscanf时,控制台停止工作。我做错了什么? 输入文件包含: 3 minsu 50 80 40 sarah 30 60 40 jason 70 80 90 代码: #define _CR
Swift 结构体是构建代码所用的一种通用且灵活的构造体。 我们可以为结构体定义属性(常量、变量)和添加方法,从而扩展结构体的功能。 与 C 和 Objective C 不同的是: 结构
我想在 javascript 中创建一个结构。我有一对信息,我想使用,例如: array[1] = new Struct(); array[1].name = "parameter-name"; ar
我不允许使用带有 in 关键字的结构,对吗?例如: struct Rect { float x,y,width,height; }; layout(location = 7) in Rect
我的结构声明的片段: struct record{ char type[4]; uint32_t data_size; uint32_t flags; uint32_t
您能否帮助我理解为什么我的 dataStruct 结构的值不是其成员之一的值? (对于simpleDataStruct结构) 我用这一行打印值: printf("dataStruct:........
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
我不确定如何在 C 中创建一个具有不同位大小的变量的结构,例如: 我想创建一个结构体,其中一个变量作为 8 位整数,一个变量作为 16 位 bool 值,一个变量作为 8 位 bool 值,一个变量作
我正在为一个项目编写一个通信协议(protocol),其中包括两个用于请求和响应的 C 结构。根据设备的设置方式,数据传输可以是请求(主模块)或响应(从模块)。 这些结构彼此非常接近。最大的区别在于请
我有以下 C 结构体,代表外部芯片中的寄存器 typedef union { // Individual Fields struct { uint8_t ELEM_1
我了解 C++,并且正在学习 C。我想将 nullptr 定义为 NULL,但是当我使用括号初始化结构时,它会导致错误并显示预期的“}”。我正在使用 Visual Studio 编译器,我的代码示例如
#include struct s { char *a1; int a; }; int main(){ struct s p={"asdv",11}; struct s p1=p;
如果将记录作为参数发送给函数,如何添加记录? struct record { char name[20]; int nr; }; void AddRecord(struct record **p_al
使用python向C语言的链接库传递数组、结构体、指针类型的数据 由于最近的项目频繁使用python调用同事的C语言代码,在调用过程中踩了很多坑,一点一点写出来供大家参考,我们仍然是使用ctype
枚举、结构体、类 注:本文为作者自己总结,过于基础的就不再赘述 ,都是亲自测试的结果。如有错误或者遗漏的地方,欢迎指正,一起学习。 1、枚举 枚举是用来定义一组通用类型的一组相关值 ,关键字enum
Swift 结构体是构建代码所用的一种通用且灵活的构造体 可以为结构体定义属性(常量、变量)和添加方法,从而扩展结构体的功能 与 C 和 Objective C 不同的是: 结构体不需要包
关于结构的快速问题: struct xint { int number; char string[12]; }; int main(int argc, char *argv[])
我是一名优秀的程序员,十分优秀!