Take the 2-minute tour ×

I have a question about how my linked lists are being stored. The data is being inputted from a file like 0 1 2 3 4 each number represents a different line of data. The data is not important in this question however.

I am trying to store it into my linked lists and its being stored, but It is being stored in from bottom to top, so when i Output it, it prints out in the wrong order. I am not sure if i am storing it in the wrong order, or printing it in the wrong order Please help me, Thank You!


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;


Declaring the struct linked list like this in main:

struct roomData *rooms;
//Sets them to empty
startRoom = NULL;

Adding data to struct with:

            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;
                    strcpy(temp->stockLine, array[n].input);
                    printf("%s", array[n].input);

                    int a,b = 0 ;
                    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

                        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

                        a = 0; //reset values
                        b = 0; //reset values
                }//end if

                if (startRoom== NULL)

printing it with this:

            void printRoomData(struct roomData *r)
            * NAME :            void printRoomData(struct roomData *r)
              DESCRIPTION :     print room information
              INPUTS :          struct roomData
              OUTPUTS:          None
                int y;
                printf("Room Information:\n\n");
                    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]);
share|improve this question
what's the struct roomData *rooms; for? I don't think you ever use it. –  Travis Griggs Dec 5 '12 at 1:31

2 Answers 2

You have to define a list tail pointer, then add new room data at tail.

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;
share|improve this answer
Thank You!!! Helped so much :) –  user1856602 Dec 5 '12 at 4:13
@user1856602 could you please accept the answer by clicking the correct sign? –  Evan Li Dec 5 '12 at 4:25

Your last line body in the fragment code, basically injects as the first item in the list. You have one of two basic choices. Change the add code, so that it enumerates the list to the end and add the link there. Or write a print function, and then call it recursively so that it unwinds the stack/list in forward order. This really isn't a good idea generally, at all, so I'll leave how to write that as an exercise for the masochist student. If you don't want to enumerate to add, you can keep two pointers, one to the firstLink, and one to the lastLink (make sure you keep lastLink updated)

share|improve this answer

Your Answer


