我有一个关于如何存储我的链接列表的问题。数据是从像 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");
}