void insertAfterCurrent(ListRef L, long data){ //inserts node after current if(!offEnd(L)){ //pre: !offEnd NodeRef newnode = malloc(sizeof(NodeType)); newnode->data = data; newnode->count = 1; newnode->next = NULL; newnode->prev = NULL; if(isEmpty(L)){ //checks if list is empty L->first = newnode; L->last = newnode; L->current = newnode; }else if(atLast(L)){ L->last->next = newnode; newnode->prev = L->last; L->last = newnode; }else{ //otherwise connects node to list newnode->next = L->current->next; newnode->prev = L->current; L->current->next->prev = newnode; L->current->next = newnode; } L->length++; assert(!offEnd(L) && !isEmpty(L)); //post: !offEnd() and !isEmpty() } }
/*Check if customer already purchased certain book*/ int customerCompare(long id, ListRef *customers, int custNum){ ListRef l = customers[custNum]; moveFirst(l); while(!offEnd(l)){ if(getCurrent(l) == id) return true; if(atLast(l)) break; moveNext(l); } return false; }
/*Inserts copurchases into hash*/ void addToHash(HashRef h, long bookId, ListRef * customers, int custNum, BookRef list){ moveFirst(customers[custNum]); //add cust previous books to hash of current book while(!offEnd(customers[custNum])){ long current = getCurrent(customers[custNum]); if(getId(list)!= current){ //don't add itself to book list insertionSort(getBookList(list), current); } if(atLast(customers[custNum])) break; moveNext(customers[custNum]); } }
/* main program*/ int main(int argc, char *argv[]){ for(int i = 1; i < argc; i++){ // open file and read in numVerts and numQueries FILE* in = fopen(argv[i], "r"); int numVerts, numQueries; char tempLine[1024]; fgets(tempLine, 1024, in); sscanf(tempLine, "%d %d", &numVerts, &numQueries); // read in and make graph Graph Map = newGraph(numVerts); for(int i = 0; i < numVerts; i++){ fgets(tempLine, 1024, in); int vert = atoi(strtok(tempLine, " ")); char *tempTo = strtok(NULL, " "); while(tempTo != NULL){ addEdge(Map, vert, atoi(tempTo)); tempTo = strtok(NULL, " "); } } // process queries for(int i = 0; i < numQueries; i++){ int from, to; fscanf(in, "%d %d", &from, &to); doBFS(Map, from); if(getDistance(Map, to) == -1) printf("No path from %d to %d exists.\n\n", from, to); else{ printf("The shortest path from %d to %d requires %d edges: \n", from, to, getDistance(Map, to)); ListHndl L = getPathTo(Map, to); moveFirst(L); while(!atLast(L)){ printf("%d -> ", getCurrent(L)); moveNext(L); } printf("%d \n\n", getCurrent(L)); freeList(&L); } } /*free everythingggggggggggg*/ freeGraph(&Map); fclose(in); } /* end program */ return 0; }
/*This method prints out the IDs of the ListHndl in the Hash.*/ void printList(ListHndl L) { moveFirst(L); while(!offEnd(L)) { if(!atLast(L)) { printf("%d -> ", L->current->data); }else{ printf("%d \n", L->current->data); } moveNext(L); } printf("\n"); }
void deleteCurrent(ListRef L){ //deletes current node and frees it if(!isEmpty(L) && !offEnd(L)){ //pre: !isEmpty() and !offEnd() if(atLast(L)) deleteLast(L); else if(atFirst(L)) deleteFirst(L); else{ NodeRef oldnode = L->current; L->current->prev->next = L->current->next; L->current->next->prev = L->current->prev; L->current = L->current->next; free(oldnode); oldnode = NULL; L->length--; } } }
/*Gets copurchases then adds to hash*/ void addCopurchase(HashRef h, long bookId, ListRef * customers, int custNum){ BookRef list = getElement(h, bookId); if(list==NULL) list = insert(h, bookId, NULL); addToHash(h, bookId, customers, custNum, list);//add symmetrically, need for loop to add moveFirst(customers[custNum]); while(!offEnd(customers[custNum])){ long id = getCurrent(customers[custNum]); BookRef l = getElement(h, id); if(l==NULL) l = insert(h, id, NULL); addToHash(h, id, customers, custNum, l); if(atLast(customers[custNum])) break; moveNext(customers[custNum]); } }
void deleteLast(ListRef L){ //deletes last node and frees it if(!isEmpty(L)){ //pre: !isEmpty() NodeRef oldnode = L->last; if(L->first == L->last){ //checks if list is only a single node L->first = NULL; L->last = NULL; L->current = NULL; }else{ if(atLast(L)) L->current = L->current->prev; L->last = L->last->prev; L->last->next = NULL; } free(oldnode); oldnode = NULL; L->length--; } }
/* * insert after the current node, if defined */ void insertAfterCurrent(ListRef L, long data) { NodeRef n; if (L == NULL) { printf( "Error: Called insertAfterCurrent() on NULL ListRef\n"); //exit(EXIT_FAILURE); } else if (isEmpty(L)) { printf( "Error: Called insertAfterCurrent() on empty ListRef\n"); //exit(EXIT_FAILURE); } if (atLast(L)) { insertAfterLast(L, data); } else { n = newNode(data); n->next = L->current->next; n->prev = L->current; L->current->next = n; n->next->prev = n; L->length++; } }
/*Gets best recommendation then prints it*/ void printRecommendation(HashRef h, long bookId, ListRef* customers, int custNum){ moveFirst(customers[custNum]); int max=0; long maxId=0; if(getElement(h,bookId)==NULL){ printf("No recommendations for book: %lu\n", bookId); return; } ListRef l = getBookList(getElement(h,bookId)); moveFirst(l); while(!offEnd(l)){ if(getCount(l) >=max && !customerCompare(getCurrent(l), customers, custNum)){ max = getCount(l); maxId=getCurrent(l); } if(atLast(l)) break; moveNext(l); } if(maxId!=0) printf("Recommendation for book %lu is : %lu\n",bookId,maxId); else printf("No recommendations for book: %lu\n", bookId); }
int main(int argc, char **argv) { FILE *inFile; char str[20000]; char *token; int nVerts; int nReqs; Graph pathGraph; /* * Validate input file * */ if (argc < 2) { perror("Please specify an input file\n"); return (-1); } inFile = fopen(argv[1], "r"); if (inFile == NULL) { perror("Could not open input file"); return (-1); } /* * Read Input file * */ fgets(str, 60, inFile); strtok(str, "\n"); token = strtok(str, " "); nVerts = atoi( token ); token = strtok(NULL, " "); nReqs = atoi( token ); /* * Build Graph & Insert Edges * */ pathGraph = newGraph( nVerts ); for (int from = 0; from < nVerts; ++from ) { fgets (str, 20000, inFile); strtok(str, "\n"); token = strtok(str, " "); token = strtok (NULL, " "); while( token != NULL) { int to = atoi( token ); insertEdge( pathGraph, from, to); token = strtok (NULL, " "); } } /* * Process requests and find paths * */ for (int from = 0; from < nReqs; ++from ) { int source; int dest; fgets (str, 20000, inFile); strtok(str, "\n"); token = strtok(str, " "); source = atoi( token ); token = strtok(NULL, " "); dest = atoi( token ); doBFS(pathGraph, source); int dist = getDistance( pathGraph, dest); if ( dist > -1) { printf("The shortest path from %d to %d requires %d edge(s):\n", source, dest, dist); ListHndl path = getPathTo( pathGraph, dest); moveFirst( path ); while( !offEnd( path )) { printf("%d ", getCurrent( path)); if ( !atLast( path )) { printf( "-> "); } else { printf("\n\n"); } moveNext( path ); } freeList( path ); } else { printf("No path from %d to %d exists\n\n", source, dest); } } fclose (inFile); freeGraph(pathGraph); return 0; }
int main (){ /***Exercise List constructor***/ ListHndl List; List = NULL; List = newList (); if(List){ printf("List Created\n"); }else{ printf("List Not Created\n"); } printf("isEmpty %d\n",isEmpty(List)); /*should print 1*/ /***Populate with test data***/ int i; for(i=0; i<=4; i++){ insertAtFront(List,i); } printList(stdout, List); printf("isEmpty %d\n",isEmpty(List)); /*should print 0*/ int j; for(j=5; j<=9; j++){ insertAtBack(List,j); } printList(stdout, List); /***Exercise all access functions***/ printf("offEnd %d\n",offEnd(List));/*should print 0*/ printf("atFirst %d\n",atFirst(List));/*should print 0*/ printf("atLast %d\n",atLast(List));/*should print 0*/ printf("getFirst %d\n", getFirst(List));/*should print 4*/ printf("getLast %d\n", getLast(List));/*should print 9*/ printf("getCurrent %d\n", getCurrent(List));/*should print 0*/ /***Exercise all removal manipulation functions***/ deleteLast(List); printList(stdout, List); printf("getLast %d\n", getLast(List));/*should print 8*/ deleteFirst(List); printList(stdout, List); printf("getFirst \n", getFirst(List));/*should print 3*/ deleteCurrent(List); printList(stdout, List); moveLast(List); printList(stdout, List); movePrev(List); printList(stdout, List); moveNext(List); printList(stdout, List); /***Exercise various edge cases***/ makeEmpty(List); insertAtFront(List, 40); moveFirst(List); deleteCurrent(List); insertAtFront(List, 41); insertAtBack(List, 42); moveFirst(List); insertBeforeCurrent(List, 43); printList(stdout, List); /***Exercise List destructors***/ deleteCurrent(List); printList(stdout, List); makeEmpty(List); printf("offEnd %d\n",offEnd(List));/*should print 1*/ freeList(&List); return(0); }