#include #include #include #define MAXSEATTYPES 5 #define MAXVENUES 15 #define MINVEN 2 #define MAXEVENTS 20 #define MINEVENTS 6 struct address { int stnum; char street[100]; int aptno; char city[100]; char state[2]; int zip5; char country[100]; }; struct seating { char stype; int smount; float discperc; float regperc; }; struct venue { int vcode; char venname[40]; char vtype; double venuecost; int vkindsstg; struct seating venseating[MAXSEATTYPES]; struct venue *next; }; struct event { int eday,emo,eyr; int elength; char esetting; double efixed; char ename[40]; }; int printvenbycode(struct venue *read, int tcode); int printonevenue(struct venue *read); int printven(struct venue *read); int input(struct venue venarray[15], struct event earray[20], int tv, int te); struct venue * sortl2s(struct venue *head); int main(void) { int done = 0; int cntr = 0; struct venue *newven=NULL, *currven = NULL, *headven = NULL, *folven=NULL; while (!done) { cntr++; newven = (struct venue *) malloc (sizeof(struct venue)); //print messages and read data into newven i.e. newven->vcode etc. newven->vcode = fabs(rand())/10000; //test data newven->next = NULL; #ifdef DEBUG printf("\nNewven vcode is %d",newven->vcode); #endif //create linked list unsorted or sorted smallest to largest by vcode if (headven == NULL) { headven = newven; #ifdef DEBUG printf("\nStarted new linked list"); printven(headven); #endif } #ifdef SORTSMLG else if (headven->vcode > newven->vcode) //new element goes at front before head { newven->next = headven; headven = newven; #ifdef DEBUG printf("\nPut new head element on list"); printven(headven); #endif } else { while ((currven->next != NULL) && (currven->vcode < newven->vcode)) { folven = currven; currven = currven->next; #ifdef DEBUG printf("\n moving through list at code :"); printonevenue(currven); #endif } if (currven->vcode > newven->vcode) //new element goes between two elements in list { newven->next = currven; folven->next = newven; #ifdef DEBUG printf("\nAdded element in middle of list"); printven(headven); #endif } #endif //new element goes at end of list else { currven->next = newven; #ifdef DEBUG printf("\nAdded new element to end of list"); printven(headven); #endif } #ifdef SORTSMLG } currven = headven; #else currven = newven; #endif printf("\nCurrent list is: "); printven(headven); #ifdef DEBUG printf("\n Stuff in linked list"); #endif printf("\nAre you done? Enter 1 for yes and 0 for no : "); scanf("%d",&done); if ((done)&&(cntrnext; follow = head; tail = NULL; #ifdef DEBUGSORT printf("\nfollow addr is %p and : ",follow); printonevenue(follow); printf("\ncurr addr is %p and : ",curr); printonevenue(curr); #endif while (curr != NULL) // goes through list once { #ifdef DEBUGSORT printf("\n\n--while curr != NULL"); #endif hold = curr->next; #ifdef DEBUGSORT printf("\nhold addr is %p",hold); #endif //compare current element to follow element if (curr->vcode > follow->vcode) { #ifdef DEBUGSORT printf("\n\t+++curr > follow"); printf("\n\tfollow addr is %p, -> is %p and : ",follow,follow->next); printonevenue(follow); printf("\n\tcurr addr is %p, -> is %p and : ",curr,curr->next); printonevenue(curr); #endif //if current is bigger than follow //swap the elements swaps++; #ifdef DEBUGSORT printf("\n\tswap"); #endif curr->next = follow; follow->next = hold; #ifdef DEBUGSORT printf("\n\tmove links"); printf("\n\tfollow addr is %p, -> is %p and : ",follow,follow->next); printonevenue(follow); printf("\n\tcurr addr is %p, -> is %p and : ",curr,curr->next); printonevenue(curr); printf("\n\tlist from head after two links changed : "); printven(head); #endif if (tail == NULL) { head = curr; #ifdef DEBUGSORT printf("\n\thead = curr"); #endif } else //if (tail != NULL) { #ifdef DEBUGSORT printf("\n\ttail addr is %p, -> is %p and : ",tail,tail->next); #endif tail->next = curr; #ifdef DEBUGSORT printf("\n\ttail %p links to curr %p", tail,curr); #endif } #ifdef DEBUGSORT printf("\n\tlist from head after head or tail link changed : "); printven(head); #endif // this is where I've screwed up repeatedly curr = follow; if (tail == NULL) follow = head; else follow = tail->next; #ifdef DEBUGSORT printf("\n\t*********list after swap :\n"); printf("\n\ttail addr is %p, -> is %p and : ",tail,((tail==NULL)?NULL:tail->next)); if (tail != NULL) printonevenue(tail); printf("\n\tfollow addr is %p, -> is %p and : ",follow,follow->next); printonevenue(follow); printf("\n\tcurr addr is %p, -> is %p and : ",curr,curr->next); printonevenue(curr); printf("\n"); printven(head); #endif } #ifdef DEBUGSORT printf("\nAfter compare curr > follow"); #endif tail = follow; follow = curr; curr = hold; #ifdef DEBUGSORT printf("\n\ttail addr is %p, -> is %p and : ",tail,tail->next); printonevenue(tail); printf("\n\tfollow addr is %p, -> is %p and : ",follow,follow->next); printonevenue(follow); if (curr != NULL) { printf("\n\tcurr addr is %p, -> is %p and : ",curr,curr->next); printonevenue(curr); } else printf("\n\tcurr addr is %p",curr); #endif } } printf("\nSorted large to small"); printven(head); return head; } int printven(struct venue *read) { while (read != NULL) // (!read) { printf("\n Address is %p, ->next is %p and ",read,read->next); printonevenue(read); read = read->next; } printf("\n"); } int printvenbycode(struct venue *read, int tcode) { printf("\nPrinting venue matching code %d\n",tcode); while(read != NULL) { if (tcode == read->vcode) { printf("\n "); printonevenue(read); } read = read->next; } printf("\n"); return 0; } int printonevenue(struct venue *read) { printf("Vcode is %d",read->vcode); } int input(struct venue venarray[15], struct event earray[20], int tv, int te) { int m; struct venue temp; for (m = 0; m < tv; m++) { printf("\nVenue %d has code %d and %d kinds of seating",m+1,venarray[m].vcode, venarray[m].vkindsstg); } temp = venarray[0]; printf("\n\nVenue temp has code %d and %d kinds of seating",temp.vcode, temp.vkindsstg); return 1; }