我对如何存储我的链接列表有疑问。 数据是从像这样的文件输入的 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;
使用以下内容向struct添加数据:
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");
}
答案 0 :(得分:1)
你必须定义一个列表尾指针,然后在尾部添加新的房间数据。
struct roomData *startRoom = NULL, *tail = NULL;
void addRoomData() {
// skip code to set room data
temp.next = NULL;
if (startRoom == NULL) {
startRoom = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
}
答案 1 :(得分:0)
片段代码中的最后一行主体,基本上作为列表中的第一项注入。你有两个基本选择之一。更改添加代码,以便将列表枚举到最后并在那里添加链接。或者编写一个print函数,然后递归调用它,以便以正向顺序展开堆栈/列表。这根本不是一个好主意,所以我会留下如何写这个作为受虐狂学生的练习。如果你不想枚举添加,你可以保留两个指针,一个指向firstLink,一个指向lastLink(确保你保持lastLink更新)