リンクリストのメンバーを変更できません。次のように宣言された構造体「レコード」があります。
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」を使用して、元のリストがソートされた順序で印刷されます。
ここから、リンクリストのメンバーの1人の在庫を更新します。値「Ford」(makeの場合)、「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;
}
ご覧のとおり、この関数は、リストのヘッドノード(更新する正しいノードの検索を開始する場所)、make&year(在庫を変更する対象のノード)、およびnewStock(ターゲットノードの標準の「stock」フィールドを置き換えるために使用する値)。
最初のケースは、headがNULLの場合です。この場合、リストは空であり、失敗した「0」を「Main」の呼び出し元の変数に返します。
2番目のケースは、一致するheadのmakeフィールド間のstrcmpが同じで、headとmatchingのyearフィールドが同じである場合、headのstockフィールドをnewStockに更新し、成功した「1」を関数に戻します。主要"。本質的に、ヘッドノードが変更するのに適切なノードである場合は、そうします。
3番目のケースは、ヘッドノードが変更する正しいノードではない場合です。current(最初のヘッドとして定義)がNULLでないという条件で、現在のノードのmakeフィールドとyearフィールドを、変更するターゲットノードと比較します。それらが一致する場合、currentのstockフィールドをnewStockに設定し、成功した「1」を返します。それらが一致しない場合は、「else」条件を通過し、探しているものが見つかるまでリストをトラバースします。その場合は、現在の在庫フィールドを更新します。
これらのすべてのケースが失敗した場合、4番目のケースが現れ、失敗した「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
何らかの理由で、「メイン」関数は、ノードがリストに存在していても、ノードが見つからないことを通知されています。
これがプログラム全体です。問題のある関数は他にもありますが、それらのほとんどを除外しました。
/*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