Note that there are some explanatory texts on larger screens.

plurals
  1. POLinked List, Storing in Wrong Order?
    text
    copied!<p>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.</p> <p>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!</p> <p>Code:</p> <pre><code>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; </code></pre> <p>Declaring the struct linked list like this in main:</p> <pre><code>struct roomData *rooms; //Sets them to empty startRoom = NULL; </code></pre> <p>Adding data to struct with:</p> <pre><code> 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-&gt;roomType, word3); word3 = strtok(NULL," "); //roomNumberOfTYpe temp-&gt;roomNumberOfType = atoi(word3); word3 = strtok(NULL," "); //roomLengthFeet temp-&gt;lengthFeet = atof(word3); word3 = strtok(NULL," "); //roomLengthInches temp-&gt;lengthInch = atof(word3); word3 = strtok(NULL," "); //roomWidthFeet temp-&gt;widthFeet = atof(word3); word3 = strtok(NULL," "); //roomWidthInches temp-&gt;widthInch = atof(word3); //room area temp-&gt;area = (((temp-&gt;lengthFeet * 12) + temp-&gt;lengthInch) /12) * (((temp-&gt;widthFeet * 12) + temp-&gt;widthInch) /12); word3 = strtok(NULL," "); //rentalRate temp-&gt;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-&gt;roomNumberOfType); strcat(salesName, roomNumber); //adds room number to salesName string if(strcmp(temp-&gt;roomType, "S")==0) strcpy(temp-&gt;roomName, salesName);//store salesName with roomNumner if(check == 1) //for stock values in room { temp-&gt;roomHasStock = 1; n++; /* strcpy(temp-&gt;stockLine, array[n].input); printf("%s", array[n].input); int a,b = 0 ; word3 = strtok(array[n].input," "); //stockType printf("%s \n", word3); temp-&gt;roomStock[a][0] = atoi(word3); //sores stock number printf("%s \n", word3); word3 = strtok(NULL, " "); //stockCount temp-&gt;roomStock[a][1] = atoi(word3); //sores stock inventory temp-&gt;roomStockCount = 0; //for storing how many stocks in room a++; //next value in array temp-&gt;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-&gt;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-&gt;roomStockCount++; //inventory + 1 b=0; } } a = 0; //reset values b = 0; //reset values */ }//end if if (startRoom== NULL) { startRoom=temp; startRoom-&gt;nextRoom=NULL; } else { temp-&gt;nextRoom=startRoom; startRoom=temp; } } </code></pre> <p>printing it with this:</p> <pre><code> 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-&gt;roomType); printf("Room Number: %d of Type %s\n", r-&gt;roomNumberOfType, r-&gt;roomType); printf("Room Length- Feet: %.2f Inches: %.2f\n", r-&gt;lengthFeet, r-&gt;lengthInch ); printf("Room Widh- Feet: %.2f Inches: %.2f\n", r-&gt;widthFeet, r-&gt;widthInch ); printf("Room Area: %.2f sq ft\n", r-&gt;area); printf("Room Rental Rate: $%.2f\n", r-&gt;rentalRate); printf("Gross Profit: $%.2f\n", r-&gt;grossProfit); printf("Net Profit: $%.2f\n", r-&gt;netProfit); if(strcmp(r-&gt;roomType, "S")==0) //if room is a sales room printf("Sales Room Name: %s\n", r-&gt;roomName); if(r-&gt;roomHasStock == 0) //if room has no stock printf("Stock Avaliable: No\n"); if(r-&gt;roomHasStock == 1) //if room has stock { printf("Stock Avaliable: Yes\n"); printf("Stocks: %s\n", r-&gt;stockLine); for(y=0; y&lt;r-&gt;roomStockCount; y++) //how many stock does room have? { printf("Stock Number: %d, Stock Inventory: %d\n", r-&gt;roomStock[y][0], r-&gt;roomStock[y][1]); } } printf("\n"); r=r-&gt;nextRoom; } printf("**************************************************\n"); printf("\n"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload