int main() { ListHndl intList; intList = newList(); int data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; /* * Integer list testing * */ printHeading(" INTEGER LIST ", '#', 80); printList(stdout, intList); accessorTest("isEmpty", 1, isEmpty(intList)); for(int i = 0; i < 10; i++) { insertAtFront(intList, data[i]); mutatorTest("%s : data = %d", "insertAtFront", data[i]); } moveFirst(intList); for(int i = 0; i < 5; i++) { printList(stdout, intList); insertBeforeCurrent(intList, data[i]); mutatorTest("insertBeforeCurrent : data = %d", data[i]); moveNext(intList); } accessorTest("isEmpty", 0, isEmpty(intList)); printList(stdout, intList); moveFirst(intList); while( !offEnd(intList) ) { printList(stdout, intList); moveNext(intList); } moveLast(intList); while( !offEnd(intList) ) { printList(stdout, intList); movePrev(intList); } makeEmpty(intList); mutatorTest("makeEmpty( intList)"); printList(stdout, intList); accessorTest("isEmpty", 1, isEmpty(intList)); freeList(intList); return 0; }
int main(){ /*make list and output file*/ ListHndl TheList = newList(); FILE *out = fopen("out.out", "w"); /*test empty in empty case*/ if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); printf("testing insert one number\n"); insertAtFront(TheList,(unsigned long*)25728); printf("%lu\n",(unsigned long)getFirst(TheList)); printf("%lu\n",(unsigned long)getLast(TheList)); /*should have same value*/ printf("testing list with three numbers\n"); insertAtFront(TheList,(unsigned long*)1589458); insertAtBack(TheList,(unsigned long*)35762111234); printf("%lu\n",(unsigned long)getFirst(TheList)); /*test empty in full case*/ if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); /*test moving the current pointer around*/ moveFirst(TheList); moveNext(TheList); printf("%lu\n",(unsigned long)getCurrent(TheList)); moveLast(TheList); movePrev(TheList); printf("%lu\n",(unsigned long)getCurrent(TheList)); /*test printList*/ printList(out, TheList); /*test makeEmpty*/ makeEmpty(TheList); if(isEmpty(TheList)) printf("Empty\n"); else printf("not Empty\n"); /*test inserting functions*/ insertAtFront(TheList,(unsigned long*)2); insertAtFront(TheList,(unsigned long*)1); insertAtFront(TheList,(unsigned long*)4); insertAtBack(TheList,(unsigned long*)4); moveLast(TheList); insertBeforeCurrent(TheList,(unsigned long*)3); printList(out,TheList); deleteFirst(TheList); deleteCurrent(TheList); deleteLast(TheList); printList(out,TheList); makeEmpty(TheList); printList(out,TheList); /*free list and close output file*/ freeList(&TheList); fclose(out); return 0; }
void doBFS(GraphRef g, int source){ ListRef list = newList(); initGraph(g); insertAfterLast(list, source); g->color[source]= 1; g->distance[source]=0; while(!isEmpty(list)){ int current = getFirst(list); if(current > g->numVertices){ deleteFirst(list); break; } ListRef edgeList = g->vertices[current]; moveFirst(edgeList); while(!offEnd(edgeList)){ int edge = getCurrent(edgeList); if(g->color[edge]==0){ g->distance[edge] = g->distance[current]+1; g->color[edge] = 1; g->parent[edge] = current; insertAfterLast(list, edge); } moveNext(edgeList); } deleteFirst(list); g->color[current] = 2; } makeEmpty(list); freeList(list); }
// Perform breadth first search on graph with given source. void doBFS(Graph *G, int source) { // Initialize graph first. for (int i = 0; i < G->numVertices; ++i) { G->color[i] = 0; // Set to white. G->distance[i] = 0; G->parent[i] = -1; } G->color[source] = 1; // Set to grey. G->distance[source] = 0; // Create a list Q and add source to it. ListHndl Q = newList(); insertAtBack(Q, source); while (!isEmpty(Q)) { //Dequeue and delete first element. int u = getFirst(Q); deleteFirst(Q); // Move current position to point at first. if (!isEmpty(G->vertices[u])) moveFirst(G->vertices[u]); //Traverse vertices of u. while (!offEnd(G->vertices[u])) { int v = getCurrent(G->vertices[u]); if (G->color[v] == 0) { // Check if V is white. G->color[v] = 1; // Color V grey. G->distance[v] = G->distance[u] + 1; G->parent[v] = u; insertAtBack(Q, v); } moveNext(G->vertices[u]); } G->color[u] = 2; // Color u black. } freeList(&Q); }
void doBFS(GraphHndl g, int source) { ListHndl tempList = NewList(); /*Make the lists white, reset distances and parents*/ for(int i = 0; i < g->vertNums; i++) { g->parentPtr[i] = -1; g->distancePtr[i] = INT_MAX; g->colorPtr[i] = 0; } /*Temporary list holds paths from source*/ insertAtBack(tempList, source); g->colorPtr[source] = 1; g->distancePtr[source] = 0; /*For every node in the source's adjacency list..*/ while(!isEmpty(tempList)) { int currNode = getFirst(tempList); /*Handle case where we're passed a bigger node*/ if(currNode > g->vertNums) { deleteFirst(tempList); break; } /*Second temporary list holds edge path*/ ListHndl tempList2 = g->vertices[currNode]; moveFirst(tempList2); /* For each of the next node's adjacent nodes .. */ while(!offEnd(tempList2)) { int tempEdge = getCurrent(tempList2); /* If the node was white, increase the distance, * color it grey and set it's parent to be currnode * and enqueue this node into the path list */ if(g->colorPtr[tempEdge] == 0) { g->distancePtr[tempEdge] = g->distancePtr[currNode]+1; g->colorPtr[tempEdge] = 1; g->parentPtr[tempEdge] = currNode; insertAtBack(tempList, tempEdge); } moveNext(tempList2); } /*Dequeue the first element and set black to show we're done.*/ deleteFirst(tempList); g->colorPtr[currNode] = 2; } makeEmpty(tempList); //freeList(tempList); }
/*This method prints out the IDs of the ListHndl in the Hash.*/ void printIDs(ListHndl L) { moveFirst(L); while(!offEnd(L)) { long* bookid = L->current->data; printf("%lu ", *bookid); moveNext(L); }printf("\n"); }
void doBFS(Graph G, int source) { assert(G->numVerts != 0); ListHndl L = newList(); /*printf("start %d\n", source);*/ insertAtBack(L, source); int i = 0; for(i = 0; i < G->numVerts; i++) { G->color[i]= WHITE; G->parent[i] = NULL; G->distance[i] = INFINITY; } G->color[source] = GREY; G->parent[source] = NULL; G->distance[source] = 0; while(!isEmpty(L)) { int u = getFirst(L); /*printf("u: %d\n", u);*/ if(u > G->numVerts) { deleteFirst(L); break; } ListHndl v = G->verts[u]; int prev; moveFirst(v); while(!offEnd(v)) { int E = getCurrent(v); if(prev == E) { break; } /*printf(" v: %d\n", E);*/ if(G->color[E] == WHITE) { G->color[E] = GREY; G->parent[E] = u; if(G->distance[E] < 0) { G->distance[E] = 0; } G->distance[E] = G->distance[u] + 1; insertAtBack(L, E); } prev = E; moveNext(v); } deleteFirst(L); G->color[u] = BLACK; } }
/* for debugging purposes*/ void printTable(HashTable H){ assert (H != NULL); printf("HashTable: Size %d\n", H->tableSize); for(int i = 0; i < H->tableSize; i++){ printf("%d:\n",i); if(isEmpty(H->array[i])) continue; moveFirst(H->array[i]); while(!offEnd(H->array[i])){ HashNode N = getCurrent(H->array[i]); printf("-%s\n",N->name); moveFirst(N->L); while(!offEnd(N->L)){ ListNode temp = getCurrent(N->L); printf("--%d\n",temp->num); moveNext(N->L); } moveNext(H->array[i]); } } }
/*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]); } }
ListHndl lookup(HashTable H, char* key){ assert (H != NULL); ListHndl slot = H->array[hash((unsigned char*)key) % H->tableSize]; if(isEmpty(slot)) return NULL; else moveFirst(slot); while(!offEnd(slot)){ HashNode temp = getCurrent(slot); if(strcmp(key,temp->name) == 0) return temp->L; else moveNext(slot); } return NULL; }
/*! * \brief This method creates a MockDataSet * \return Returns a mocked DataSet that has the required behavior for unittests */ te::da::MockDataSet* createMockDataSet() { te::da::MockDataSet* mockDataSet(new te::da::MockDataSet()); EXPECT_CALL(*mockDataSet, moveNext()).WillRepeatedly(::testing::Return(true)); EXPECT_CALL(*mockDataSet, getAsString(::testing::An<const std::string&>(),::testing::_)).WillRepeatedly(::testing::Return("")); EXPECT_CALL(*mockDataSet, getAsString(::testing::An<std::size_t>(),::testing::_)).WillRepeatedly(::testing::Return("")); EXPECT_CALL(*mockDataSet, isNull(std::string())).WillRepeatedly(::testing::Return(false)); EXPECT_CALL(*mockDataSet, getNumProperties()).WillRepeatedly(::testing::Return(0)); EXPECT_CALL(*mockDataSet, moveFirst()).WillRepeatedly(::testing::Return(true)); return mockDataSet; }
void deleteHashNode(HashTable H, char* name){ assert (H != NULL); ListHndl slot = H->array[hash((unsigned char*)name) % H->tableSize]; if(isEmpty(slot)) return; moveFirst(slot); while(!offEnd(slot)){ HashNode temp = getCurrent(slot); if(strcmp(name, temp->name) == 0){ ListHndl L = temp->L; freeList(&L); deleteCurrent(slot); }else moveNext(slot); } }
/* 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; }
/*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); }
/*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 deleteListNode(HashTable H, char* name, int num){ assert (H != NULL); ListHndl L = lookup(H, name); if(L == NULL) return; moveFirst(L); while(!offEnd(L)){ ListNode temp = getCurrent(L); if(temp->num == num){ deleteCurrent(L); break; } moveNext(L); } /* if library list of book ends up being empty, delete the book */ if(isEmpty(L)) deleteHashNode(H, name); }
/*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 gTreeRow::moveAfter(char *key) { gTreeRow *row; if (!key || !*key) { moveFirst(); return; } row = tree->getRow(key); if (!row) return; if (strcmp(row->parent(), parent())) return; gtk_tree_store_move_after(tree->store, dataiter, row->dataiter); }
void printGraph(GraphRef g){ for(int i=0; i<g->numVertices; i++){ printf("Node %d:\n", i); if(i!=0){ printf("Parent "); printList(g->parent[i]); } printf("\tColor %d, Dist %d", g->color[i], g->distance[i]); printf("\tEdges:"); ListRef index = g->vertices[i]; moveFirst(index); while(!offEnd(index)){ printf("%d, ",getCurrent(index)); moveNext(index); } } }
void joyStickBusyWaitingMonitor( void ) { uint32_t i; while ( 1 ) { if ( ~(LPC_GPIO1->FIOPIN | ~UP ) ) { moveUp(); } else if ( ~(LPC_GPIO1->FIOPIN | ~DOWN) ) { moveDown(); } else if( ~(LPC_GPIO1->FIOPIN | ~FIRST ) ) { moveFirst(); } else if( ~(LPC_GPIO1->FIOPIN | ~LAST ) ) { moveLast(); } for ( i = 0; i < 100000; ++i ) { // Pause } } }
void freeTable(HashTable* H){ assert ((*H) != NULL); for(int i = 0; i < (*H)->tableSize; i++){ /* free all library lists first */ if(!isEmpty((*H)->array[i])){ moveFirst((*H)->array[i]); while(!offEnd((*H)->array[i])){ HashNode N = getCurrent((*H)->array[i]); ListHndl L = N->L; freeList(&L); moveNext((*H)->array[i]); } } /*and then free the slot lists*/ freeList(&((*H)->array[i])); } free((*H)->array); free(*H); (*H) = NULL; }
/* Debug print loop. Used in debug but not needed here. */ void printGraph(GraphHndl g) { for(int i = 1; i < g->vertNums-1; i++) { printf(" For node %d: \n", i); if ( i != 0) { printf("It's parent is "); printList((ListHndl)g->parentPtr[i]); } printf("\tColor %d, Dist %d", g->colorPtr[i], g->distancePtr[i]); printf("\tEdges:"); ListHndl tempList = g->vertices[i]; moveFirst(tempList); while(!offEnd(tempList)) { printf("%d, ", getCurrent(index)); moveNext(tempList); } } }
/* main program*/ int main(){ /* variables */ char inName[128]; char outName[128]; ListHndl array[128]; char tempLine[128]; int customers; int purchases; int tempCust; unsigned long tempBook; FILE *in; FILE *out; /* greeting & prompt for input and output file names */ printf("Welcome! Please enter your input file here: "); scanf("%s",inName); in = fopen(inName, "r"); if(in == NULL){ printf("Error: This input file does not exist.\n"); exit(1); } printf("Please enter your output file here: "); scanf("%s",outName); out = fopen(outName, "w"); /* check numbers of customer and purchased */ do{ if(fgets(tempLine, 128, in) == NULL) invalidInput(); }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &customers) != 1) invalidInput(); do{ if(fgets(tempLine, 128, in) == NULL) invalidInput(); }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &purchases) != 1) invalidInput(); /*makes empty Lists and fills them up*/ for(int i = 0; i<customers; i++) array[i] = newList(); for(int i = 0; i<purchases; i++){ do{ if(fgets(tempLine, 128, in) == NULL){ printf("Error: Number of purchases does not match (too little).\n"); exit(1); } }while(strcmp(tempLine, "\n") == 0); if(sscanf(tempLine,"%d", &tempCust) != 1) invalidInput(); if(sscanf(tempLine,"%*d%lu", &tempBook) != 1) invalidInput(); if(tempCust > customers){ printf("Error: Invalid customer number.\n"); exit(1); } if(isEmpty(array[tempCust-1])){ insertAtBack(array[tempCust-1],tempBook); continue; } moveFirst(array[tempCust-1]); while(!offEnd(array[tempCust-1])){ if(tempBook < (unsigned)getCurrent(array[tempCust-1])){ insertBeforeCurrent(array[tempCust-1], tempBook); break; } moveNext(array[tempCust-1]); } if(offEnd(array[tempCust-1])) insertAtBack(array[tempCust-1],tempBook); } /* checks for any lingering purchases */ for(;;){ if(fgets(tempLine, 128, in) == NULL) break; if(sscanf(tempLine,"%d", &tempCust) != 0){ printf("Error: Number of purchases does not match (too many).\n"); exit(1); } } /*printing store*/ fprintf(out, "customer# books purchased\n"); for(int i = 0; i<customers; i++){ fprintf(out, "%d ",i+1); moveFirst(array[i]); while(!offEnd(array[i])){ fprintf(out, "%lu ", getCurrent(array[i])); moveNext(array[i]); } fprintf(out, "\n"); } /*free everythingggggggggggg*/ for(int i = 0; i<customers; i++) freeList(&array[i]); fclose(in); fclose(out); /* end program */ printf("Done.\n"); return 0; }
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); }