int main(int argc, const char * argv[]) { LinkQueue queue; puts("初始化队列 queue"); initQueue(&queue); traversal(queue); puts("队尾依次插入0 1 2 3"); insertQueue(&queue, 0); insertQueue(&queue, 1); insertQueue(&queue, 2); insertQueue(&queue, 3); traversal(queue); puts("先进先出,删除队列从头开始, 0 "); deleteQueue(&queue); traversal(queue); puts("先进先出,删除队列从头开始, 1 "); deleteQueue(&queue); traversal(queue); puts("先进先出,删除队列从头开始, 2 "); deleteQueue(&queue); traversal(queue); puts("先进先出,删除队列从头开始, 3"); deleteQueue(&queue); traversal(queue); destoryQueue(&queue); return 0; }
int main( void ) { first f; last l; if ( ( f = malloc( sizeof( referenceToNodePointer ) ) ) == NULL ) exit( EXIT_FAILURE ); if ( ( l = malloc( sizeof( referenceToNodePointer ) ) ) == NULL ) exit( EXIT_FAILURE ); initQueue( f, l ); insertQueue( 10, f, l ); insertQueue( 20, f, l ); insertQueue( 30, f, l ); printQueue( f ); printf( "------\n\n" ); extractQueue( f ); printQueue( f ); return 0; }
/* * initialize the priority queue with the initial state * explored <-- empty * loop do * if empty(queue) return failure * node <-- queue.pop() * if node is goal, then return solution(node) * add node to explored * for each action in Action(problem, node) do * child node <-- childnode(problem, node, action) * if child is not in queue or expolored, add node to queue * otherwise, if the child.state is in queue and with a higher pathcost, * then replace that node with the child * */ bool UCSearch::doSearch(Problem &problem, std::vector<MapNode *> &path) { MapNode *init = problem.getInitState(); insertQueue(init); while(!_queue->isEmpty()) { MapNode *node = _queue->pop(); if(problem.isGoal(node)) { node->getPathFromRoot(path); return true; } insertExplored(node); std::vector<MovAction_t> & action = problem.getActions(); std::vector<MovAction_t>::iterator it; for(it=action.begin(); it<action.end(); it++) { if(*it == MOV_ACT_NOOP) { continue; } MapNode *child = NULL; int res = 0; res = problem.movAction(node, *it, &child); if(res == OP_OK) { if(!isInQueue(child) && !isInExplored(child)) { insertQueue(child); } else { if(isInQueue(child)) { //get old one MapNode *old = findQueueByKey(child->getKey()); if(child->getPathCost() < old->getPathCost()) { //update; #ifdef HW1_DEBUG std::cout <<"before update, child cost: " << child->getPathCost(); std::cout <<", old cost: " << old->getPathCost(); #endif updateQueue(old, child); #ifdef HW1_DEBUG std::cout <<"after, old cost: " << old->getPathCost(); #endif } } } } } } return false; }
void levelOrderIter (TNODE_p_t root) { if (root == NULL) return; QUEUE_p_t queue = initQueue(root); while (!isEmptyQueue(queue)) { TNODE_p_t temp = deleteQueue(&queue); printf(" %d", temp->data); if (temp->left != NULL) insertQueue(&queue, temp->left); if (temp->right != NULL) insertQueue(&queue, temp->right); } }
void dumpEvents(MDATA *m, LogDATA *l, pthread_mutex_t *mut){ MDATA *mBuffer; LogDATA *lBuffer; int i; long offset; for(i=0; i<sizeofQueue(m); i++){ mBuffer = (MDATA *) removeQueue(m); if(exists(mBuffer->path)) { mBuffer->fp = fopen(mBuffer->path, "r"); fseek(mBuffer->fp, 0, SEEK_END); offset = ftell(mBuffer->fp) - mBuffer->offset; if(offset){ fseek(mBuffer->fp, -offset, SEEK_END); char * buffer = malloc (sizeof(char)*offset); fread(buffer, 1, offset, mBuffer->fp); mBuffer->offset = ftell(mBuffer->fp); if(buffer != NULL){ //printf("Host: %s - ", mBuffer->host); //printf("Path: %s - ", mBuffer->path); //printf("Type: %s - ", mBuffer->sourcetype); //printf("Offset: %d\n", mBuffer->offset); //printf("%d\n", offset); //printf("%s", buffer); lBuffer = malloc(sizeof(LogDATA)); lBuffer->host = mBuffer->host; lBuffer->path = mBuffer->path; lBuffer->sourcetype = mBuffer->sourcetype; lBuffer->readTime = time(NULL); //lBuffer->log = malloc(strlen(buffer)+1); //strcpy(lBuffer->log, buffer); lBuffer->log = buffer; insertQueue(&(*l), lBuffer); } } fclose(mBuffer->fp); } insertQueue(m, mBuffer); } }
void sortStack(pSTACK first , pSTACK second, ppQUEUE queue,int(* cmp)(void *p1, void *p2, void *typeCmp), int cmpKey1, int cmpKey2){ pREGISTRO elem; int cont = 0; /* while ( first && second ) { */ while ( cont < (4096/sizeof(Registro))) { elem = malloc(sizeof(Registro)); if ( (*cmp)(first->registro,second->registro,&cmpKey1) == 0 ){ pop(&first,&elem); //se o primeiro criterio eh igual, tenta com o segundo if ( (*cmp)(first->registro,second->registro,&cmpKey2) == 0 ){ //se o segundo eh igual, insere o first, na proxima vai o second pop(&first,&elem); }else if ( (*cmp)(first->registro,second->registro,&cmpKey2) < 0 ){ pop(&first,&elem); }else{ //se eh maior pop(&second,&elem); } }else if ((*cmp)(first->registro,second->registro,&cmpKey1) < 0 ){ pop(&first,&elem); }else{ //se nao eh maior pop(&second,&elem); } insertQueue(queue, elem); /* printf("elem id eh ======>>>>>> %d\n",elem->id); */ cont++; } /* printf("tam do cont!!!! %d\n",cont); */ }
bool BFSearch::doSearch(Problem &problem, std::vector<MapNode *> &path) { MapNode *init = problem.getInitState(); if(problem.isGoal(init)) { init->getPathFromRoot(path); return true; } insertQueue(init); while(!_queue->isEmpty()) { MapNode * node = getFromQueue(); insertExplored(node); std::vector<MovAction_t> & actions = problem.getActions(); std::vector<MovAction_t>::iterator it; for(it = actions.begin(); it < actions.end(); it++) { if(*it == MOV_ACT_NOOP) { continue; } MapNode * child = NULL; int res = 0; res = problem.movAction(node, *it, &child); if(res == OP_OK) { //check if in the queue if(!isInQueue(child) && !isInExplored(child)) { if(problem.isGoal(child)) { child->getPathFromRoot(path); return true; } insertQueue(child); } } } //for } //while return false; }
//다쓴 프로세스는 프리, 아니면 큐에 다시 삽입한다. void Cpu::outProcess(Process& process){ process.outCpu(); //cpu 사용을 마쳤다고 한다. if (process.getBurstTime() == 0){ //사용이 끝난 프로세스 // Process::totalProcessSize--; process.freeCpu(); } else{ insertQueue(process); } }
void complete(Trie trie) { unsigned char c; int org=0, vers=0, s=0; /* Création de la file */ Queue queue = createQueue(); /* Avoir les transition de la racine */ Trans transitions = avoirTrans(trie, 0, 0); /* Fonction de supp pour les fils de la racine 'sons of the root '*/ while (transitions != NULL) { vers = transitions->vers; transitions = transitions->prochain; /* inserer dans la file */ insertQueue(queue, vers); /* Destination = racine*/ trie->mon_suppl[vers] = 0; } /* Récupérer les element a partir de la file, et exectuer la fonction de supp */ while (queue->taille != 0) { org = recupQueue(queue); transitions = avoirTrans(trie, 1, org); while (transitions != NULL) { org = transitions->origine; c = transitions->letter; vers = transitions->vers; transitions = transitions->prochain; insertQueue(queue, vers); s = trie->mon_suppl[org]; /* Transition no définie */ while (chercher_vers(trie, s, c) == -1) { s = trie->mon_suppl[s]; } trie->mon_suppl[vers] = chercher_vers(trie, s, c); /* Fonction de sortie */ if (trie->finite[trie->mon_suppl[vers]]) { trie->finite[vers] = 1;}}}}
int dataInsertQueue(QUEUE* queue,char* data,unsigned int lenth) { if(NULL == queue) { printf("please create a queue fist\n"); return - 1; } NODE* newNode = cteateNode(data,lenth); if(newNode) { int res =insertQueue(queue,newNode); return res; } return -1; }
////////////////////////////////////////////////////////////////////// // check if the node has been undeleted ////////////////////////////////////////////////////////////////////// void checkUndelete(node* thisNode, node* desc) { node *neigh; scar* neigh_scar; if(desc->deleted) { int i; desc->deleted = 0; for(i=0;i<MAX_DEGREE; i++) { neigh_scar = thisNode->scars[i].backscar; if (neigh_scar) { neigh = GET_NEIGHBOR(neigh_scar); insertQueue(neigh->descendant,currentQueue); } } } }
///////////////////////////////////////////////////////////////////////// // Check if the leaf status has changed, ie. if it was degree one, but // now it isn't, or visa-versa //////////////////////////////////////////////////////////////////////// void checkLeafStatusChange(int old_degree, node* desc) { int i; if (old_degree != desc->degree) if ((desc->degree==1) || (old_degree==1)) { node* neigh; scar* neigh_scar; for(i=0;i<MAX_DEGREE;i++) { neigh_scar = desc->scars[i].backscar; if (neigh_scar) if(GET_NEIGHBOR(neigh_scar->backscar) == desc) { neigh = GET_NEIGHBOR(neigh_scar); insertQueue(neigh,currentQueue); } } } }
/////////////////////////////////////////////////////////////////////// // Initial Run //////////////////////////////////////////////////////////////////////// void initTreeContraction (Queue* q, tree_t* tree) { int i,n; node* v; n = tree->n; currentTree = tree; initClusterList(); oldRootList.head = NULL; newRootList.head = NULL; for (i = 1; i <= n; ++i) { v = tree ->vertexArray + i; deprintf("Enqueueuing %d\n",i); insertQueue(v,q); deprintf("Making descendant copy\n"); v->descendant = copyVertex (v,currentTree->nodeList); } }
MDATA *populateMDATAQueue(){ FILE *fp; char * line = NULL, sourcetype[128], path[128]; size_t len = 0; int length, offset; MDATA *MDATAQueue = (MDATA *)initQueue(); MDATA *MDATANode = malloc(sizeof(MDATA)); if(exists("files.conf")){ fp = fopen("files.conf", "r"); }else{ printf("Especifique os arquivos de coleta em files.conf\n"); return 0; } //while( (length = getline(&line, &len, fp)) != -1 ){ int i=0; while(EOF != fscanf(fp, "%d %s %s", &offset, &sourcetype, &path) ){ //if(length>1){ //printf("%s\n", sourcetype); //line[strlen(line)-1] = '\0'; MDATANode = malloc(sizeof(MDATA)); char hostname[1024]; hostname[1023] = '\0'; gethostname(hostname, 1023); MDATANode->host = hostname; MDATANode->path = strdup(path); //printf("%s\n", MDATANode->path); MDATANode->sourcetype = strdup(sourcetype); MDATANode->offset = offset; //MDATANode->fp = fopen(MDATANode->path, "r"); insertQueue(MDATAQueue, MDATANode); MDATANode = NULL; //} } fclose(fp); if(line) free(line); return MDATAQueue; }
/////////////////////////////////////////////////////////////////////////////// // Queue all the descendant's neighbors since they are affected /////////////////////////////////////////////////////////////////////////////// void queueAllDescNeighbors(node* thisNode,Queue* q) { int i; scar* bscar; node* neigh; node* nd= thisNode->descendant; for(i=0;i<MAX_DEGREE;i++) { bscar = nd->scars[i].backscar; if(bscar && nd->scars[i].cl) { if((bscar->backscar) == &(nd->scars[i])) { neigh = GET_NEIGHBOR(bscar); insertQueue(neigh,q); } } } }
int main(void) { QUEUE_t myQueue; STACK_t myStack; stack_init(&myStack); queue_init(&myQueue); for(int i = 0; i < 10; i++) { insertQueue(&myQueue,(void*) (rand() % 100)); push(&myStack, (void*) (rand() % 50)); } itemS_t* holdS = pop(&myStack); printf("Item popped: %d\n", holdS->keyValue); free(holdS); printf("Pushing 99...\n"); push(&myStack, (void*) 99); holdS = pop(&myStack); printf("Item popped: %d", holdS->keyValue); free(holdS); printQueue(&myQueue); printStack(&myStack); itemQ_t* holdQ = removeQueue(&myQueue); printf("Item removed from queue: %d.\n", holdQ->keyValue); free(holdQ); printQueue(&myQueue); printStack(&myStack); clearQueue(&myQueue); clearStack(&myStack); return 0; }
/////////////////////////////////////////////////////////////////// // basic function that determines what to do with all nodes ////////////////////////////////////////////////////////////////// void runNode(node* thisNode, Queue* q) { int lindex,rindex; scar *lscar, *rscar; node *left=NULL,*right=NULL; deprintf("The degree is %d\n", thisNode->degree); deprintf("The cluster is %p\n",thisNode->data); deprintf("The node is %p\n", thisNode); switch (contractCheck(thisNode)) { case DO_END: setupVertexCluster(thisNode,END_EVENT, currentTree, &oldRootList); //end event the event to end them all! thisNode->vertex->cl->affected = IS_AFFECTED; deleteNode(thisNode); insertCluster(thisNode->vertex->cl,&newRootList); break; case DO_RAKE: morework =1; lindex = thisNode->left; lscar = thisNode->scars[lindex].backscar; left = GET_NEIGHBOR(lscar); doRake(thisNode,left,lindex); insertQueue(left->descendant,q); deleteNode(thisNode); break; case DO_COMPRESS: morework =1; lindex = thisNode->left; rindex = thisNode->right; lscar = thisNode->scars[lindex].backscar; rscar = thisNode->scars[rindex].backscar; left = GET_NEIGHBOR(lscar); right = GET_NEIGHBOR(rscar); doCompress(thisNode, left, right, lindex, rindex); insertQueue(left->descendant,q); insertQueue(right->descendant,q); deleteNode(thisNode); break; case DO_LIVE: { morework = 1; node *desc = thisNode->descendant; int old_degree = desc->degree; doLive(thisNode); // Queue all affected nodes. insertQueue(desc,q); deprintf("Check Undeleted \n"); // check if thisNode is un-deleted, queue neighbors if so checkUndelete(thisNode,desc); deprintf("Leaf status change \n"); // Check if leaf status of the descendant changes and queue neighbors if so checkLeafStatusChange(old_degree,desc); // Allocate a new node for the next round if needed if(!desc->descendant) { deprintf("Allocated new node \n"); desc -> descendant = copyVertex (desc,currentTree->nodeList); } } break; default: printf ("Error in runNode"); exit (-9); } }
/*implements A* algorithm*/ tree astar(int x, int y, int d, int heuristic){ char a = 'X';//X simbolize that no action was taken to get there tree t, t1, t2; char **mat1, **mat2; int i, j; int level; double f; queue_vector aux; int k; //copy the starting matrix mat1 = newMatrix(); for(i = 0; i < n; i++){ for(j = 0; j < m; j++) mat1[i][j] = w[i][j]; } t1 = insertTree(NULL, d, x, y, mat1, a, 0); t = t1; //compute the f value according to the heuristic f = computeHeuristic(x, y, 0, heuristic); insertQueue(t1, f); while(n_queue > 0){ //remove the first element of the queue removeQueue(&aux.t, &aux.f); t1 = aux.t; d = t1 -> dirt; mat1 = t1 -> mat; a = t1 -> action; x = t1 -> x; y = t1 -> y; level = t1 -> level; //increments the number of nodes expanded expanded++; //adds into the queue if(y != 0 && mat1[y-1][x] != '#'){ mat2 = newMatrix(); copyMatrix(mat1, mat2); a = mat1[y-1][x]; mat2[y-1][x] = '@'; mat2[y][x] = '_'; if(a == '*') k = d-1; else k = d; if(checkDuplicate(t, mat2, hashFunction(x, y-1), k) == 0){ //inserts in lowercase if there is dirt if(a == '*'){ removeListDirt(x, y-1); t2 = insertTree(t1, d-1, x, y-1, mat2, 'n', level + 1); if(d-1 == 0) return t2; } else t2 = insertTree(t1, d, x, y-1, mat2, 'N', level + 1); t1 -> N = t2; //compute the f value according to the heuristic f = computeHeuristic(x, y-1, level+1, heuristic); insertQueue(t2, f); generated++; } } if(y != n - 1 && mat1[y+1][x] != '#'){ mat2 = newMatrix(); copyMatrix(mat1, mat2); a = mat1[y+1][x]; mat2[y+1][x] = '@'; mat2[y][x] = '_'; if(a == '*') k = d-1; else k = d; if(checkDuplicate(t, mat2, hashFunction(x, y+1), k) == 0){ //inserts in lowercase if there is dirt if(a == '*'){ removeListDirt(x, y+1); t2 = insertTree(t1, d-1, x, y+1, mat2, 's', level + 1); if(d-1 == 0) return t2; } else t2 = insertTree(t1, d, x, y+1, mat2, 'S', level + 1); t1 -> S = t2; //compute the f value according to the heuristic f = computeHeuristic(x, y-1, level+1, heuristic); insertQueue(t2, f); generated++; } } if(x != 0 && mat1[y][x-1] != '#'){ mat2 = newMatrix(); copyMatrix(mat1, mat2); a = mat1[y][x-1]; mat2[y][x-1] = '@'; mat2[y][x] = '_'; if(a == '*') k = d-1; else k = d; if(checkDuplicate(t, mat2, hashFunction(x - 1, y), k) == 0){ //inserts in lowercase if there is dirt if(a == '*'){ removeListDirt(x-1, y); t2 = insertTree(t1, d-1, x-1, y, mat2, 'w', level + 1); if(d-1 == 0) return t2; } else t2 = insertTree(t1, d, x-1, y, mat2, 'W', level + 1); t1 -> W = t2; //compute the f value according to the heuristic f = computeHeuristic(x, y-1, level+1, heuristic); insertQueue(t2, f); generated++; } } if(x != m - 1 && mat1[y][x+1] != '#'){ mat2 = newMatrix(); copyMatrix(mat1, mat2); a = mat1[y][x+1]; mat2[y][x+1] = '@'; mat2[y][x] = '_'; if(a == '*') k = d-1; else k = d; if(checkDuplicate(t, mat2, hashFunction(x + 1, y), k) == 0){ //inserts in lowercase if there is dirt if(a == '*'){ removeListDirt(x+1, y); t2 = insertTree(t1, d-1, x+1, y, mat2, 'e', level + 1); if(d-1 == 0) return t2; } else t2 = insertTree(t1, d, x+1, y, mat2, 'E', level + 1); t1 -> E = t2; //compute the f value according to the heuristic f = computeHeuristic(x, y-1, level+1, heuristic); insertQueue(t2, f); generated++; } } orderQueue(); } return NULL; }