#include #include #include #include /* * Example from Nov 23, 2013 * * * */ #define MAXVALS 10 #define MAX 20 #define BIGSEARCH 13 #define AVOWEL 'a' #define SORTMAX 15 const int MAXSZ=42; const char roomtypenames[5][40] = {"indoor sales room", "loading area", "outdoor sales garden section", "bathroom", "office" }; // declaring a struct type struct day { int day, month, year; char moname[20]; }; typedef struct { float area; int amountofstock[6]; char roomname[100]; int roomnumber; float length, width; int roomtype; struct day roomday; } ROOM; struct person { long ID; char firstname[100]; char *lastname; char jbtyp; float hrwg; float hrswkd; struct person *next; struct person *prev; }; struct person * bubblesort(struct person *list); struct person * bubblesortname(struct person *list); int printll(struct person *currptr); int main(void) { int scorelist[BIGSEARCH] = {90,87,88,-79, 45, 98, 32, 99, 100, -46, 87,68}; char nameclass[ ]= {'C','_','c','l','a','s','s','\0'}; char nameclass2[40]="C 1320"; char buffer[100]; int sorttest[] = {105, 30, 42, 200, 2, 60, 17, 61, 100, 25, 62, -18, 63, 99, 31}; char sti, sta; int se = 68, ft; float in = 0.0, wage, hours; int linresult, temp; char words[20][40]={{0}}; // array of strings char *tempwd, *word, *num; // &(buffer[0]) int *tempint, numvals; int fibofk, factofk, index1, index2, index3; int day, mo, yr; long int tempidnum; FILE *infile, outfile; struct person *headptr, *currptr, *followptr, *newptr; int seed=4, flag; infile = fopen("peoplefileA.txt","r"); if (infile == NULL) { printf ("Input file is not available"); return 0; } headptr = (struct person *) malloc(sizeof(struct person)); tempwd = (char *) malloc(sizeof(char)*100); // give values to elements of the struct fscanf(infile, "%ld %c %f %f %s", &tempidnum, &sti, &wage, &hours, &words[0]); getline(&tempwd, &index1, infile); printf("\nInput data for head ptr is id= %ld, type=%c, wage=%f, hours=%f, name is %s %s", tempidnum, sti, wage, hours, words[0], tempwd); // get values from a file, from the user, from defined info... headptr->ID = tempidnum; headptr->jbtyp = sti; headptr->hrwg = wage; headptr->hrswkd = hours; strcpy(headptr->firstname, words[0]); headptr->lastname = (char *) malloc(sizeof(char)*(index1+1)); strcpy(headptr->lastname, tempwd); printf("\nheadptr->ID is %ld", headptr->ID); headptr->next = NULL; headptr->prev = NULL; currptr = headptr; followptr = NULL; // now start adding structs to the linked list while(!feof(infile)) // condition to stop reading data { newptr = (struct person *) malloc(sizeof(struct person)); // put in values fscanf(infile, "%ld %c %f %f %s", &tempidnum, &sti, &wage, &hours, &words[0]); getline(&tempwd, &index1, infile); printf("\nInput data for new ptr is id= %ld, job type = %c, wage=%f, hours=%f, name is %s %s", tempidnum, sti, wage, hours, words[0], tempwd); // get values from a file, from the user, from defined info... newptr->ID = tempidnum; newptr->jbtyp = sti; newptr->hrwg = wage; newptr->hrswkd = hours; strcpy(newptr->firstname, words[0]); newptr->lastname = (char *) malloc(sizeof(char)*(index1+1)); strcpy(newptr->lastname, tempwd); newptr->next = NULL; newptr->prev = NULL; printf("\ndata in the ptr is id= %ld, type=%c, wage=%f, hours=%f, name is %s %s\n", newptr->ID, newptr->jbtyp, newptr->hrwg, newptr->hrswkd, newptr->firstname, newptr->lastname); currptr = headptr; followptr = NULL; // newptr->ID = rand(); printf("\nnewptr->ID is %ld", newptr->ID); if (newptr->ID < headptr->ID) { // new head of list newptr->next = headptr; printf("\nbefore head of list: newptr->ID is %ld, headptr->ID is %ld",newptr->ID, headptr->ID); headptr = newptr; } else { // find location in between two elements while ((currptr->next != NULL) && // order of the tests is important (newptr->ID > currptr->ID )) { //increment the current pointer followptr = currptr; currptr = currptr->next; printf("\nin while: followptr->ID is %ld, currptr->ID is %ld",followptr->ID, currptr->ID); } if (newptr->ID < currptr->ID) // currptr->ID = followptr->next->ID { followptr->next = newptr; newptr->next = currptr; printf("\nbetween: followptr->ID is %ld, newptr->ID is %ld, currptr->ID is %ld",followptr->ID, newptr->ID, currptr->ID); } else if (currptr->next == NULL) // after last element { currptr->next = newptr; printf("\nat end: currptr->ID is %ld, newptr->ID is %ld",currptr->ID, newptr->ID); } else { printf("\nWHAT CASE AM I?"); } } } // GO through the list and print addresses of each struct currptr = headptr; printf("\n\nLinked list elements:"); while ( currptr != NULL) { printf("\n currptr is %p, ID is %ld, currptr->next is %p",currptr, currptr->ID, currptr->next); printf("\ndata in the ptr is id= %ld, type=%c, wage=%f, hours=%f, name is %s %s\n", currptr->ID, currptr->jbtyp, currptr->hrwg, currptr->hrswkd, currptr->firstname, currptr->lastname); currptr = currptr->next; } printf("\nEnd of Linked list \n[[[[Begin bubblesort on hours worked\n"); headptr = bubblesort(headptr); //printll(headptr); printf("\nEnd of Linked list \n[[[[[Begin bubblesort on name\n"); headptr = bubblesortname(headptr); // printll(headptr); /* */ return 0; } int printll(struct person *currptr) { printf("\n\nLinked list elements:"); while ( currptr != NULL) { printf("\n currptr is %p, ID is %ld, currptr->next is %p",currptr, currptr->ID, currptr->next); printf("\ndata in the ptr is id= %ld, type=%c, wage=%f, hours=%f, name is %s %s\n", currptr->ID, currptr->jbtyp, currptr->hrwg, currptr->hrswkd, currptr->firstname, currptr->lastname); currptr = currptr->next; } printf("\nEnd of printll Linked list"); } /* // Want to sort the data now before searching. What do we need to know? // data type, size, how to handle duplications when present, how to sort. // very common to sort small to large. */ struct person * bubblesort(struct person *list) { //Bubble sort of a linked list with unknown values int swap = 1; struct person tempp; struct person *curr, *foll, *b4, *after, *head = list, *temppers = &tempp; //Until the list is sorted, do the following: printf("\n\nBefore bubblesort performed:"); printll(list); while (swap > 0) { swap = 0; curr = head->next; b4 = curr->next; foll = head; after = NULL; //For every pair of adjacent values in list, while(curr != NULL) { // Compare the values and if forward value (curr) is larger than // following value(foll), if (curr->hrswkd < foll->hrswkd) { // then swap the values // printf("\ncurr->hrswkd %f is < foll->hrswkd %f so swap", curr->hrswkd, foll->hrswkd); // printf("\nbefore swap: head %p, curr %p and foll %p", head, curr, foll); /* from head --> ..after -->foll-->curr->b4 w/ foll->next = curr; after->next = foll; curr->next = b4 to head-->..after-->curr-->foll-->b4 w/curr->next = foll; after->next = curr; foll->next = b4 */ if (after != NULL) { after->next = curr; } curr->next = foll; foll->next = b4; if (foll == head) { head = curr; // printf("\n**Move head to %p",head); } temppers = curr; curr = foll; foll = temppers; // printf("\nafter swap: head %p, curr %p and foll %p\n", head, curr, foll); swap++; } after = foll; foll = curr; curr = b4; if (b4 != NULL) { b4 = b4->next; } // printf("\n--After compare"); // printf(": head %p, curr %p and foll %p\n", head, curr, foll); } printf("\nEnd of pass w %d swaps\n", swap); } list = head; printf("\nBubblesort results:: "); printll(list); return list; } struct person * bubblesortname(struct person *list) { //Bubble sort of a linked list with unknown values int swap = 1, i=0; struct person tempp; char lastcurr[100], lastfoll[100]; struct person *curr, *foll, *b4, *after, *head = list, *temppers = &tempp; //Until the list is sorted, do the following: printf("\n\nBefore bubblesortname performed:"); printll(list); while (swap > 0) { swap = 0; curr = head->next; b4 = curr->next; foll = head; after = NULL; //For every pair of adjacent values in list, while(curr != NULL) { // Compare the values and if forward value (curr) is larger than // following value(foll), // if ((strcmp(curr->lastname,foll->lastname)) < 0) strcpy(lastcurr,curr->lastname); while(lastcurr[i] != '\0') { lastcurr[i] = toupper((unsigned char)lastcurr[i++]); } i=0; strcpy(lastfoll,foll->lastname); while(lastfoll[i] != '\0') { lastfoll[i] = toupper((unsigned char)lastfoll[i++]); } printf("\n^^^^lastcurr *%s* and lastfoll *%s*",lastcurr,lastfoll); if ((strcmp(lastcurr, lastfoll)) < 0) { // then swap the values // printf("\ncurr->hrswkd %f is < foll->hrswkd %f so swap", curr->hrswkd, foll->hrswkd); // printf("\nbefore swap: head %p, curr %p and foll %p", head, curr, foll); /* from head --> ..after -->foll-->curr->b4 w/ foll->next = curr; after->next = foll; curr->next = b4 to head-->..after-->curr-->foll-->b4 w/curr->next = foll; after->next = curr; foll->next = b4 */ if (after != NULL) { after->next = curr; } curr->next = foll; foll->next = b4; if (foll == head) { head = curr; // printf("\n**Move head to %p",head); } temppers = curr; curr = foll; foll = temppers; // printf("\nafter swap: head %p, curr %p and foll %p\n", head, curr, foll); swap++; } after = foll; foll = curr; curr = b4; if (b4 != NULL) { b4 = b4->next; } // printf("\n--After compare"); // printf(": head %p, curr %p and foll %p\n", head, curr, foll); } printf("\nEnd of name pass w %d swaps\n", swap); } list = head; printf("\nBubblesortname results:: "); printll(list); return list; } //Searching for what element - same type as data in array int linsearch(int sl[], int maxsize, int searchelement) { int index = 0; int foundat = -1; printf("\nIn linsearch, searchelement is %d and maxsize is %d\n",searchelement, maxsize); // for( index = 0; (index < maxsize)&&(sl[index] != searchelement) ; index++) for( index = 0; (index < maxsize); index++) { if (sl[index] == searchelement) foundat = index; printf("\nIn for loop, sl[%d] is %d\n", index, sl[index]); } //foundat = index; printf("\nIn linsearch, foundat is %d\n",foundat); return foundat; }