c - 链表,以错误的顺序存储?

标签 c hyperlink struct linked-list

我对如何存储我的链表有疑问。 数据是从一个文件输入的 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");
            }

最佳答案

你必须定义一个列表尾部指针,然后在尾部添加新的房间数据。

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;
    }
 }

关于c - 链表,以错误的顺序存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13714916/

相关文章:

css - 导航链接突然无法点击

c - 如何获取结构中的元素总数

c - Valgrind 说 "definitely leak"但它是吗?

c - 意外的无符号整数行为

c++ - CreateProcess() 启动子应用程序

css - 我想要 anchor 标记将 div 框定向到一页上,但是有没有办法让它们在将您带到那里后居中?

c - 每当在 C 中调用函数 'main' 时,变量都未初始化

javascript - 使用浏览器扩展突出显示来自网站的链接

c - Microchip 改变了 C 语言的规则?

c - 形式论证中如何修改结构变量