void initTimeKeeping(void) { gheap = newHeap(10, (ComparisonFunction)bLocalTimeCompare); dheap = newHeap(10, (ComparisonFunction)bLastSimTimeCompare); globalTimeStamp = 0; }
int main(int argc, char *argv[]) { // Check arguments if (argc < 2) { printf("Wrong arguments\n"); return EXIT_FAILURE; } int maxnum = atoi(argv[1]); char *filename = argv[2]; // Create new heap heap *theHeap = newHeap(maxnum); // Read lines of file FILE *fp = fopen(filename, "rt"); while(!feof(fp)) { char buffer[256]; fgets(buffer, 256, fp); if (feof(fp)) break; int value = atoi(buffer); insertHeap(theHeap, value); if (isFullHeap(theHeap)) { removeHeap(theHeap); } } // Show contents while(!isEmptyHeap(theHeap)) { int value = removeHeap(theHeap); printf("%d\n", value); } // Delete heap deleteHeap(theHeap); return EXIT_SUCCESS; }
t_graph* prim(t_graph *g, int s){ if(g==NULL||s<0 || s>g->maxv) return NULL; int i; t_graph* ret = new_graph(g->maxv); t_edge* temp = (t_edge*)malloc(sizeof(t_edge)); t_heap* theHeap = newHeap(g->maxv); for(i=0;i<g->maxv;i++) g->vertices[i].visited = FALSE; g->vertices[s].visited = TRUE; printf("%s\n",g->vertices[s].name); for(i=0;i<g->maxv;i++){ if(g->am[s][i]!=0){ add_edge(ret,s,i,g->am[s][i]); addMinElement(theHeap,(void*)new_edge(s,i,g->am[s][i]),&eecomp); } } while(theHeap->current_size>0){ temp = (t_edge*)popMinHeap(theHeap,&eecomp); g->vertices[temp->dst].visited = TRUE; printf("%s\n",g->vertices[temp->dst].name); for(i=0;i<(g->maxv);i++){ if(g->am[temp->dst][i]!=0&&g->vertices[i].visited ==FALSE){ addMinElement(theHeap,(void*)new_edge(temp->dst,i,g->am[temp->dst][i]),&eecomp); add_edge(ret,temp->dst,i,g->am[temp->dst][i]); } } } return ret; }
/*Here we build a heap using an array, the current number, and the maximum size of the array*/ heapRef buildHeap (int maxSize, int data[], int numData){ heapRef H = newHeap(maxSize); int i; for(i = 0; i < numData; i++){ insert(H, data[i]); } return H; }
void heapSort(int keys[], int numKeys){ if(numKeys == 0) return; heapHndl H = newHeap(numKeys); for(int i = 0; i < numKeys; i++) insert(H, keys[i]); for(int i = numKeys-1; i > -1; i--){ keys[i] = maxValue(H); deleteMax(H); } freeHeap(&H); }
Heap setupUnorderedHeap() { Node nodes[10] = { /*1*/ newNode(10), /*2*/ newNode(1), /*3*/ newNode(7), /*4*/ newNode(4), /*5*/ newNode(3), /*6*/ newNode(6), /*7*/ newNode(9), /*8*/ newNode(2), /*9*/ newNode(5), /*10*/ newNode(8) }; return newHeap(10, nodes); }
void testNewHeap(void) { printf("Running newHeap test\n"); Node nodes[10] = { newNode(1), newNode(2), newNode(3), newNode(4), newNode(5), newNode(6), newNode(7), newNode(8), newNode(9), newNode(10) }; Heap heap = newHeap(10, nodes); Node *sortedNodes = getNodes(heap); for(int i = 0; i<10; i++) { int expected = i + 1; if(nodeAt(sortedNodes, i)->data != expected) { char message[50]; sprintf(message, "expected data=%d, got data=%d", expected, (*(sortedNodes + i))->data); printFailed(message); return; } } printPassed(); }
void heapSort(int keys[], int numKeys){ heap h = newHeap(numKeys); for(int iter = 0; iter<numKeys; iter++){ insert(h, keys[iter]); } int tmp[numKeys]; for(int i=0; i<numKeys; i++){ tmp[i] = maxValue(h); deleteMax(h); } for(int i=0; i<numKeys; i++){ keys[numKeys-i-1]=tmp[i]; } delete(h); }
// ------------------------------------------------------------------------- int main (int argc, const char * argv[]) { int* key; char command; bool quit=false; printf( "Welcome to heap test program\n" ); Heap h = newHeap( comp ); do { printf( "Command: ('h' for help): " ); scanf( " %c", &command ); switch( toupper(command) ) { case 'H': /* help */ printf( "Commands:\n" " i num - insert number into heap\n" " d - delete minimum element from heap\n" " p - print heap\n" " q - quit\n" ); break; case 'I': /* insert */ key = safe_malloc( sizeof *key ); scanf( "%d", key ); insertHeap( h, key ); break; case 'D': /* delete_min */ if (isEmptyHeap( h )) { printf( "Heap is empty\n" ); } else { key = deleteMinHeap( h ); printf( "%d\n", *key ); free( key ); } break; case 'P': /* print */ mapHeap( print_element, h, NULL ); printf( "\n" ); break; case 'Q': quit=true; } } while( !quit ); freeHeap( h ); printf( "Goodbye!\n" ); }
int main (int argc, char **argv){ printf("Welcome to the heap driver program!\n"); //testing insertion into a heap and testing printheap printf("Inserting several nodesand printing the heap\n"); printf("Result should be 45135, 2534, 325, 50, 211, 25, 3, 294, 1.\n"); HeapRef heap = newHeap(10); insert(heap, 50); insert(heap, 211); insert(heap, 45135); insert(heap, 3); insert(heap, 325); insert(heap, 2344); insert(heap, 25); insert(heap, 2534); insert(heap, 294); insert(heap, 1); printHeap(heap); //testing maxValue printf("\nPrinting max of heap, result should be 45135\n"); printf("%d\n", maxValue(heap)); printf("\n"); //testing deleting the max nodes from a heap printf("Deleting 2 max nodes and reheapifying\n"); printf("Result should be 2344, 325, 211, 294, 50, 1, 25, 3.\n"); deleteMax(heap); deleteMax(heap); printHeap(heap); printf("\n"); //testing heap sort function printf("Testing heap sort. Result should be 1, 3, 25, 42, 50, 211, 294, 325, 543, 2311, 2344, 2534, 45135.\n\n"); int keys [] = {50, 211, 45135, 3, 325, 2344, 25, 2534, 294, 1, 42, 543, 2311}; heapSort(keys, 13); for(int i = 0; i < 13; i++){ printf("%d\n", keys[i]); } printf("The heap driver is now done!\n"); return 0; }
Heap* getHeap(serverstate ss) { Heap* h; LOCK_LOCK(STACKPOOLMUTEX); if ( heapPoolIndex ) { // Sound as heapPoolIndex != 0 --> heapPool != NULL h = heapPool[--heapPoolIndex]; LOCK_UNLOCK(STACKPOOLMUTEX); } else // allocate new heap { int hid = heapid_counter++; LOCK_UNLOCK(STACKPOOLMUTEX); h = newHeap(ss); h->heapid = hid; } return h; }
int main(int argv,char** args){ h = newHeap(HEAP_SIZE); openFile(args[1]); delete(h); }