#include #include #include #define LINES 20 #define MAX 50 #define SZ 25 struct date { int mo, day, yr; char moabbbr[LINES]; }; typedef struct roomalldata { float len, wid; char rmname[MAX]; char rmtype; int rmnumber; int stockamts[6]; float area; // sq ft float rentalrate; struct roomalldata *nextroom; } MYROOMS; /* functions */ int bubsort(struct roomalldata rooms[25], int s); // not used struct roomalldata * bubsort2(struct roomalldata *begin); int main() { struct date orderdate, odate; FILE *infile; int i; struct roomalldata *startroom, *currroom=NULL, *prevroom=NULL, *folroom=NULL; startroom = NULL; infile = fopen("test13datab.txt","r"); // Read data until end of file (feof(infile)) is true // Read data from a file while (!feof(infile)) { // Set pointers for traversing list prevroom = startroom; folroom = NULL; currroom = (struct roomalldata *)malloc(sizeof (struct roomalldata)); //put data into new struct fscanf(infile, "%d %d %d %d %d %d %f %f", &(currroom->stockamts[0]), &(currroom->stockamts[1]), &(currroom->stockamts[2]), &(currroom->stockamts[3]), &(currroom->stockamts[4]), &(currroom->stockamts[5]), &(currroom->len), &(currroom->wid)); currroom->nextroom = NULL; //error check printf("Read in at %p: ",currroom); print1s(currroom); // link new struct in order in linked list // sorting on length from smallest to largest //is new element the first element in the list? if (startroom == NULL) { startroom = currroom; } //does new element go before beginning of list? else if (currroom->len < prevroom->len) { currroom->nextroom = prevroom; startroom = currroom; } else { while ((prevroom != NULL) && (currroom->len > prevroom->len)) { //traversing the list to find correct location folroom = prevroom; prevroom = prevroom->nextroom; } //does new element go between two elements? if (prevroom != NULL) { currroom->nextroom = prevroom; folroom->nextroom = currroom; } //does new element go at end? else { folroom->nextroom = currroom; } } } currroom = startroom; printf("\nRead and sorted into order by len:\n "); printll(startroom); startroom = bubsort2(startroom); currroom = startroom; printf("\nSorted by wid:\n "); printll(currroom); return 0; } // Print 1 struct int print1s(struct roomalldata *currroom) { { printf("\t%d\t %d\t %d\t %d\t %d\t %d\t %f\t %f\t %p", (currroom->stockamts[0]), (currroom->stockamts[1]), (currroom->stockamts[2]), (currroom->stockamts[3]), (currroom->stockamts[4]), (currroom->stockamts[5]), (currroom->len), currroom->wid, currroom->nextroom); printf("\n"); } return 1; } // Print whole linked list int printll(struct roomalldata *currroom) { int c=1; do { printf("\t%p\t: %d\t %d\t %d\t %d\t %d\t %d\t %f\t %f\t %p",currroom, (currroom->stockamts[0]), (currroom->stockamts[1]), (currroom->stockamts[2]), (currroom->stockamts[3]), (currroom->stockamts[4]), (currroom->stockamts[5]), (currroom->len), currroom->wid, currroom->nextroom); printf("\n"); currroom = currroom->nextroom; c++; } while ((currroom != NULL)&&(c<12)); printf("\n"); return 1; } int bubsort(struct roomalldata rooms[SZ], int size) { } // Bubble sort on room width // revised with linked list instead of array // This version has lots of print statements to help trace what is occurring // Notice that it also returns a ptr in case sorting changed the first element struct roomalldata * bubsort2(struct roomalldata *begin) { int j,k,l,m, swap=1; struct roomalldata *yroom=NULL,*troom=NULL, *froom=NULL, *sroom=NULL, *hroom=NULL; // printf("\nIn bubsort2 with begin %p troom %p sroom %p froom %p yroom %p",begin, troom, sroom, froom, yroom); while (swap > 0) { troom = NULL; // these four pointers help with swapping sroom = begin; // froom (first room) is compared with sroom (second room) froom = begin->nextroom; // always reset the four ptrs to beginning of LL yroom = froom->nextroom; // in order t->s->f->y where s (second) is then compared to f (first) // printf("\nIn bubsort2 with troom %p sroom %p froom %p yroom %p",troom, sroom, froom, yroom); printf("\nIn while swap > 0\n"); printll(begin); swap = 0; while (froom != NULL) { printf("\nIn while froom != NULL"); // Compare room width if (froom->wid < sroom->wid) { // reset the links to the new order if (troom != NULL) troom->nextroom = froom; else begin = froom; froom->nextroom = sroom; sroom->nextroom = yroom; // reset the external pointers to match new order hroom = froom; froom = sroom; sroom = hroom; swap++; printf(" **Swap**"); // printf(" with troom %p sroom %p froom %p yroom %p swap %d",troom, sroom, froom, yroom, swap); } else { printf(" NO swap "); // printf("with troom %p sroom %p froom %p yroom %p swap %d",troom, sroom, froom, yroom, swap); } // traverse list troom = sroom; sroom = froom; froom = yroom; if (yroom != NULL) yroom = yroom->nextroom; } } return begin; }