リンクされたリストがどのように保存されているかについて質問があります。データは、0 1 2 3 4 のようなファイルから入力されており、各数字はデータの異なる行を表します。ただし、この質問ではデータは重要ではありません。
リンクされたリストに保存しようとして保存されていますが、下から上に保存されているため、出力すると間違った順序で出力されます。間違った順序で保存しているのか、間違った順序で印刷しているのかわかりません。助けてください、ありがとうございます!
コード:
struct roomData
{
float widthFeet, widthInch;
float lengthFeet, lengthInch;
char roomName[100];
int roomNumberOfType;
char roomType[6]; //char of room type
int roomStock[100][2]; //for storing each room stock types
int roomHasStock; //if the room has a stock avaliable
int roomStockCount; //how many stocks the room has
float area; // sq ft
float rentalRate;
float profitsPerRoom;
float netProfit;
float grossProfit;
char stockLine[200];
int x;
struct roomData *nextRoom;
}*startRoom;
main で次のように構造体リンク リストを宣言します。
struct roomData *rooms;
//Sets them to empty
startRoom = NULL;
次を使用して構造体にデータを追加します。
void addRoomData(int n, int x, struct fileInput array[300], int check)
{
/*******************************************************************
* NAME : void addRoomData(int n, int x, struct fileInput array[300], int check)
DESCRIPTION : fills up the room struct
INPUTS : n, x, array, check
OUTPUTS: None
*/
struct roomData *temp;
temp=(struct roomData *)malloc(sizeof(struct roomData));
char * word3 = (char *) malloc(100) ; // used with strTok
char salesName[100] = "";
word3 = strtok(array[n].input," "); //roomType
strcpy(temp->roomType, word3);
word3 = strtok(NULL," "); //roomNumberOfTYpe
temp->roomNumberOfType = atoi(word3);
word3 = strtok(NULL," "); //roomLengthFeet
temp->lengthFeet = atof(word3);
word3 = strtok(NULL," "); //roomLengthInches
temp->lengthInch = atof(word3);
word3 = strtok(NULL," "); //roomWidthFeet
temp->widthFeet = atof(word3);
word3 = strtok(NULL," "); //roomWidthInches
temp->widthInch = atof(word3);
//room area
temp->area = (((temp->lengthFeet * 12) + temp->lengthInch) /12) * (((temp->widthFeet * 12) + temp->widthInch) /12);
word3 = strtok(NULL," "); //rentalRate
temp->rentalRate = atof(word3);
word3 = strtok(NULL," "); //forSalesName
while(word3 != NULL ) //fills up the name array and stores it into salesName with concatanation
{
strcat(salesName, word3);
strcat(salesName, " ");
word3 = strtok(NULL, " ");
}
char *ptr = (char *) malloc(100); //delets new line from string
if( (ptr = strchr(salesName, '\n')) != NULL)
*ptr = '\0';
char roomNumber[10];
sprintf(roomNumber, " %d", temp->roomNumberOfType);
strcat(salesName, roomNumber); //adds room number to salesName string
if(strcmp(temp->roomType, "S")==0)
strcpy(temp->roomName, salesName);//store salesName with roomNumner
if(check == 1) //for stock values in room
{
temp->roomHasStock = 1;
n++;
/*
strcpy(temp->stockLine, array[n].input);
printf("%s", array[n].input);
int a,b = 0 ;
word3 = strtok(array[n].input," "); //stockType
printf("%s \n", word3);
temp->roomStock[a][0] = atoi(word3); //sores stock number
printf("%s \n", word3);
word3 = strtok(NULL, " "); //stockCount
temp->roomStock[a][1] = atoi(word3); //sores stock inventory
temp->roomStockCount = 0; //for storing how many stocks in room
a++; //next value in array
temp->roomStockCount++; //if a stock was saved, then inventory + 1
while(word3 != NULL ) //fills up the name array and stores it into salesName with concatanation
{
word3 = strtok(NULL, " "); //takes each value stockItem and stockCount
temp->roomStock[a][b] = atoi(word3); //stores
b++; //for count
if(b == 2) //if reaches after count, reset so it can store item number
{
a++; //next item number
temp->roomStockCount++; //inventory + 1
b=0;
}
}
a = 0; //reset values
b = 0; //reset values
*/
}//end if
if (startRoom== NULL)
{
startRoom=temp;
startRoom->nextRoom=NULL;
}
else
{
temp->nextRoom=startRoom;
startRoom=temp;
}
}
これで印刷します:
void printRoomData(struct roomData *r)
{
/*******************************************************************
* NAME : void printRoomData(struct roomData *r)
DESCRIPTION : print room information
INPUTS : struct roomData
OUTPUTS: None
*/
r=startRoom;
if(r==NULL)
{
return;
}
int y;
printf("**************************************************\n");
printf("Room Information:\n\n");
while(r!=NULL)
{
printf("Room Type: %s\n", r->roomType);
printf("Room Number: %d of Type %s\n", r->roomNumberOfType, r->roomType);
printf("Room Length- Feet: %.2f Inches: %.2f\n", r->lengthFeet, r->lengthInch );
printf("Room Widh- Feet: %.2f Inches: %.2f\n", r->widthFeet, r->widthInch );
printf("Room Area: %.2f sq ft\n", r->area);
printf("Room Rental Rate: $%.2f\n", r->rentalRate);
printf("Gross Profit: $%.2f\n", r->grossProfit);
printf("Net Profit: $%.2f\n", r->netProfit);
if(strcmp(r->roomType, "S")==0) //if room is a sales room
printf("Sales Room Name: %s\n", r->roomName);
if(r->roomHasStock == 0) //if room has no stock
printf("Stock Avaliable: No\n");
if(r->roomHasStock == 1) //if room has stock
{
printf("Stock Avaliable: Yes\n");
printf("Stocks: %s\n", r->stockLine);
for(y=0; y<r->roomStockCount; y++) //how many stock does room have?
{
printf("Stock Number: %d, Stock Inventory: %d\n", r->roomStock[y][0], r->roomStock[y][1]);
}
}
printf("\n");
r=r->nextRoom;
}
printf("**************************************************\n");
printf("\n");
}