// Driver program to test above functions int main() { struct node* root1=NULL,*root2=NULL; struct Queue* queue1=createQueue(SIZE),*queue2 = createQueue(SIZE); int i; for(i = 1; i <= 12; ++i) insert(&root1,i, queue1); for(i = 1; i <= 12; ++i) insert(&root2,i, queue2); mirror(root2); printf("The tree_1 elements are: "); levelOrder(root1); printf("\nThe tree_2 elements are: "); levelOrder(root2); printf("\n"); if(checkmirror(root1,root2)) printf("\nThe trees are mirror image"); else printf("\nThe trees are not mirror image"); return 0; }
void testCombine() { printf("\ntesting combine...\n"); int success = 0; printf("Case 1: queues are the same size\n"); Queue a = createQueue(4); enqueue(0,a); enqueue(1,a); enqueue(2,a); enqueue(3,a); printf("Queue a = "); printQueue(a); Queue b = createQueue(4); enqueue(0,b); enqueue(1,b); enqueue(2,b); enqueue(3,b); printf("Queue b = "); printQueue(b); Queue merged = combine(a,b); printf("Combination = "); printQueue(merged); success = merged->length = 8; printf("\nCase 2: b is larger\n"); printf("Queue a = "); printQueue(a); freeQueue(b); b = createQueue(20); enqueue(0,b); enqueue(1,b); enqueue(2,b); enqueue(3,b); enqueue(4,b); enqueue(5,b); printf("Queue b = "); printQueue(b); freeQueue(merged); merged= combine(a,b); printf("Combination = "); printQueue(merged); success = success && merged->length == 8; printf("\nCase 3: a is larger\n"); freeQueue(merged); merged= combine(b,a); printf("Combination = "); printQueue(merged); success = success && merged->length == 8; printf("\nCase 4: queue is empty\n"); freeQueue(a); a = createQueue(2); freeQueue(merged); merged = combine(a,b); printf("Queue a = "); printQueue(a); printf("Queue b = "); printQueue(b); printf("Combination = "); printQueue(merged); success = success && merged->length == 0; if(success) printf(" ☺ success.\n"); else printf(" ☹ failure.\n"); }
/************************* main *******************************/ int main() { new_packet = (unsigned char *)malloc(MTU); if(new_packet == NULL) { printf("Malloc failed\n"); exit(EXIT_FAILURE); } struct nfq_handle *h = get_handle(); fd_set rfds; // start the downstream code, later move to a thread initializeRabin(powers); int max_fd = 0; int down_fd = createQueue(h, DOWN_MOBILE_QUEUE, &cbDown); if(down_fd > max_fd) max_fd = down_fd; int up_fd = createQueue(h, UP_MOBILE_QUEUE, &cbUp); if(up_fd > max_fd) max_fd = up_fd; printlog(logfile, system_loglevel, LOG_DEBUG, "Queue packet descriptors, down_fd: %d, up_fd: %d\n", down_fd, up_fd); int n = 0, rv = 0; char buf[4096] __attribute__ ((aligned)); nfnl_rcvbufsiz(nfq_nfnlh(h), 4096 * 4096); while(true) { FD_ZERO(&rfds); FD_SET(down_fd, &rfds); FD_SET(up_fd, &rfds); n = select(max_fd + 1, &rfds, NULL, NULL, NULL); if(n == -1) { printlog(logfile, system_loglevel, LOG_CRITICAL, "Select returned error: %s\n", strerror(errno)); } if(FD_ISSET(down_fd, &rfds)) { rv = recv(down_fd, buf, sizeof(buf), 0); if(rv < 0) { printlog(logfile, system_loglevel, LOG_CRITICAL, "recv call failed: %s\n", strerror(errno)); } else { nfq_handle_packet(h, buf, rv); } } if(FD_ISSET(up_fd, &rfds)) { rv = recv(up_fd, buf, sizeof(buf), 0); if(rv < 0) { printlog(logfile, system_loglevel, LOG_CRITICAL, "recv call failed: %s\n", strerror(errno)); } else { nfq_handle_packet(h, buf, rv); } } } // start the upstream code, later move to a thread return 0; }
int main() { struct queue *q3 = NULL, *q5 = NULL, *q7 = NULL; q3 = createQueue(); if (!q3) { printf("Failed to allocate memory for Queue\n"); return 1; } q5 = createQueue(); if (!q5) { printf("Failed to allocate memory for Queue\n"); return 1; } q7 = createQueue(); if (!q7) { printf("Failed to allocate memory for Queue\n"); return 1; } int num = 1, count = 1, k = 50; /* initialize Queue */ enqueue(q3,num*3); enqueue(q5,num*5); enqueue(q7,num*7); while (count<k) { num = getMinofQueues(q3,q5,q7); if (num < 0) { num = 1<<32 break; } if (num == peekqueue(q3)) { num = dequeue(q3); enqueue(q3,num*3); enqueue(q5,num*5); enqueue(q7,num*7); } else if (num == peekqueue(q5)) { num = dequeue(q5); enqueue(q5,num*5); enqueue(q7,num*7); } if (num == peekqueue(q7)) { num = dequeue(q7); enqueue(q7,num*7); } count++; printQueue(q3); printQueue(q5); printQueue(q7); //printf("%d,",num); }
int main (int argc, char *argv[]) { int i; Item it; Queue q1,q2; printf("Test 1: Create queues\n"); q1 = createQueue(); q2 = createQueue(); assert(q1 != NULL); assert(q2 != NULL); assert(queueLength(q1) == 0); assert(queueLength(q2) == 0); printf("Passed\n"); printf("Test 2: Add to queues\n"); for (i = 1; i <= MAX; i++) { enterQueue(q2,i); enterQueue(q1,i); assert(queueLength(q2) == i); assert(queueLength(q1) == i); } printf("Final q1: "); showQueue(q1); printf("Final q2: "); showQueue(q2); printf("Passed\n"); printf("Test 3: Remove from queues\n"); for (i = 1; i <= MAX; i++) { it = leaveQueue(q1); assert(queueLength(q1) == MAX-i); assert(i == it); it = leaveQueue(q2); assert(queueLength(q2) == MAX-i); assert(i == it); } printf("Passed\n"); printf("Test 4: Destroy queues\n"); dropQueue(q1); dropQueue(q2); printf("Passed\n"); printf("Test 5: Remove from emoty queue\n"); printf("This test should fail an assertion\n"); q1 = createQueue(); it = leaveQueue(q1); printf("Passed\n"); return 0; }
/*最短增广路径,计算路径*/ void getShortestIncrePath(Index S, Table T, Graph G) { Queue Q = createQueue(); enqueue(S, Q); /*初始化入度路径计算表时,发出节点被置0, 我们在这里的目的是计算路径最小流*/ T[S].dist = Infinity; Index V,W; while(!isEmpty(Q)) { V = dequeue(Q); T[V].known = true; Edge edge = G->TheCells[V].next; while(edge !=NULL) { W = edge->vertexIndex; if(T[W].known == false) { T[W].dist = MIN(T[V].dist, edge->weight); T[W].path = V; enqueue(W,Q); } edge = edge->next; } } disposeQueue(Q); }
int main(int argc, char *argv[]) { char* names = argv[1]; char* name, **name_p; Queue* queue; struct Linked_list *iterator; queue = createQueue(sizeof(char*)); name = strtok(names, ","); while (name != NULL) { printf("enqueueing %s - %i\n", name, QueueSize(queue)); Enqueue(queue, name); name = strtok(NULL, ","); } for(iterator = queue->front->next; iterator != NULL; iterator = iterator->next) { printf("element %s\n", (char*)(iterator->value)); } // printf("front %s\n", (char*)((Linked_list*)(front(queue)))->value); while(!QueueIsEmpty(queue)) { printf("dequeued "); fflush(stdout); DequeueElement(queue, (void**)&name); // name = *name_p; printf("%s - %i\n", name, QueueSize(queue)); } }
static void _traverseBFT (BSTNODE* root, void (*process) (PACKAGE* package) ) { BSTNODE* currentNode; BSTNODE* tempNode; QUEUE* queue; queue = createQueue(); if(!queue) { printf("\nMemory Overflow!\n"); exit(201); } currentNode = root; while(currentNode != NULL) { process ( currentNode->ptrPackage ); if(currentNode->left != NULL) enqueue(queue, currentNode->left); if(currentNode->right != NULL) enqueue(queue, currentNode->right); if(!emptyQueue(queue)) { dequeue(queue, (void**)&tempNode); currentNode = tempNode; } else currentNode = NULL; } queue = destroyQueue(queue); return; }// _traverseBFT
void doBFS(maze_t* myMaze, adList_t* adList){ queue_t* queue= createQueue(myMaze->roomNumber);; int start=getStartRoom(myMaze); //printf("%d\n",start); object_t* object; edge_t* edge; room_t* room; int largest=0; adList->object[start].room->depth=0; enqueue(queue,&(adList->object[start])); while (!empty(queue)) { //printf("here\n"); object=dequeue(queue); //printf("dequeue room: %d ,depth:%d,%d",object->room->room,object->depth,adList->object[object->room->room].depth); edge=object->firstEdge; if (edge!=NULL) //printf("firstEdge: %d ",edge->number.number); for (;edge!=NULL;edge=edge->nextEdge) { if (edge->endRoom->room->depth==-1){ room=edge->endRoom->room; enqueue(queue,&adList->object[edge->endRoom->room->room]); room->parent=object->room->room; //adList->object[edge->endRoom->room->room].room->parent=object->room->room; room->edge=edge->number.number; //adList->object[edge->endRoom->room->room].room->edge=edge->number.number; room->depth=object->room->depth+1; //adList->object[edge->endRoom->room->room].depth=adList->object[object->room->room].depth+1; //printf("enqueue room: %d ,depth:%d,object room:%d",edge->endRoom->room->room,adList->object[16].depth,object->room->room); largest=room->depth; } } } myMaze->maxDepth=largest; free(queue); }
int checkStackPairwiseOrder(struct sNode *top) { struct Queue *q = createQueue(); struct sNode *s = top; int pairwiseOrdered = 1; while (!isEmpty(s)) enQueue(q, pop(&s)); while (!queueIsEmpty(q)) push(&s, deQueue(q)->key); while (!isEmpty(s)) { int n = pop(&s); enQueue(q, n); if (!isEmpty(s)) { int m = pop(&s); enQueue(q, m); if(abs(n - m) != 1) { pairwiseOrdered = 0; } } } while (!queueIsEmpty(q)) push(&s, deQueue(q)->key); return pairwiseOrdered; }
int main(int argc, char **argv) { int readInts; struct node *root = NULL; struct Queue *queue = createQueue(SIZE); FILE *fp; fp = fopen(argv[1],"r"); if (fp == NULL) printf("NO SUCH FILE EXISTS. PLEASE TRY AGAIN."); while (fscanf(fp, "%d", &readInts) != EOF) { //printf("Inserting %d into tree...\n", readInts); treeInsert(&root, readInts, queue); } fclose(fp); //for (int i = 0; i <= 12; ++i) // treeInsert(&root, i, queue); //maxHeapify(root); levelOrder(root); printf("SIZE is %d\n", SIZE); return 0; }
void test_to_enter_char_element_in_Queue_10(){ Queue* actual = createQueue(2,sizeof(char)); char value = '2'; char* data = (char*)actual->elements; ASSERT(1==enQueue(actual,&value)); free(actual); }
void test_to_enter_double_element_in_Queue_8(){ Queue* actual = createQueue(2,sizeof(double)); double value = 2.00000; double* data = (double*)actual->elements; ASSERT(1==enQueue(actual,&value)); free(actual); }
void test_to_enter_float_element_in_Queue_7(){ Queue* actual = createQueue(2,sizeof(float)); float value = 2.0; float* data = (float*)actual->elements; ASSERT(1==enQueue(actual,&value)); free(actual); }
void test_to_enter_int_element_in_queue_6(){ Queue* actual = createQueue(2,sizeof(int)); int value = 2; int* data = (int*)actual->elements; ASSERT(1==enQueue(actual,&value)); free(actual); }
/*to calculate the shortest paths to all cities from a givern vertex @param mySource the name of the starting city, prints error message if not present */ void shortestPath(char *mySource) { int index; int source_present = FALSE; queue myQueue = createQueue(); for(index = 0; index < myGraph->num_vertexes; index++) { if(strcmp(mySource, myGraph->allVertexes[index]->name) == 0) { myGraph->allVertexes[index]->cost = 0; strcpy(myGraph->allVertexes[index]->cheap_source, myGraph->allVertexes[index]->name); source_present = TRUE; } addToQueue(myQueue, createNode(myGraph->allVertexes[index])); } if(!source_present) { printf("\nThat city doesn't exist!"); return; } while(!isEmpty(myQueue)) { node temp = pop(myQueue); if(temp->myVertex->cost == INT_MAX) break; for(index = 0; index < temp->myVertex->num_edges; index++) { if(temp->myVertex->myEdges[index]->cost + temp->myVertex->cost < temp->myVertex->myEdges[index]->destination->cost) { temp->myVertex->myEdges[index]->destination->cost = temp->myVertex->myEdges[index]->cost + temp->myVertex->cost; strcpy(temp->myVertex->myEdges[index]->destination->cheap_source, temp->myVertex->name); reorderQueue(myQueue, temp->myVertex->myEdges[index]->destination); } } free(temp); } for(index = 0; index < myGraph->num_vertexes; index++) { printf("\nthe cheapest path to %s is %d from %s", myGraph->allVertexes[index]->name, myGraph->allVertexes[index]->cost, myGraph->allVertexes[index]->cheap_source); } free(myQueue); }
int main(int argc, char const *argv[]) { system("clear"); printf("\t ██╗ ██████╗ ██████╗ ██████╗ ██████╗ ██████╗ █████╗ \n"); printf("\t ██║██╔═══██╗██╔════╝ ██╔═══██╗ ██╔══██╗██╔═══██╗ ██╔══██╗ \n"); printf("\t ██║██║ ██║██║ ███╗██║ ██║ ██║ ██║██║ ██║ ╚█████╔╝ \n"); printf("\t ██ ██║██║ ██║██║ ██║██║ ██║ ██║ ██║██║ ██║ ██╔══██╗ \n"); printf("\t ██ ██║██║ ██║██║ ██║██║ ██║ ██║ ██║██║ ██║ ██╔══██╗ \n"); printf("\t ╚█████╔╝╚██████╔╝╚██████╔╝╚██████╔╝ ██████╔╝╚██████╔╝ ╚█████╔╝ \n"); printf("\t ╚════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚════╝ \n"); // Entrada BOARD *board = (BOARD*)malloc(sizeof(BOARD)); STATE *game = createState(); createBoard(&board); readBoard(&board); game->current = board; // Fila inicial PRIORITY_QUEUE *queue = createQueue(); insert(queue, game); // Loop de jogo gameLoop(queue); return 0; }
int main() { char *array[] = {"mona", "is", "the", "best", "yes", "and", "that", "is", "the", "total", "truth", "ikram", "a3"}; queueNode *myQueue; /*creates a queue*/ int i; /*interator to add elements to the queue*/ myQueue = createQueue(compareTest, copy, destroy); for (i = 0; i < 13; i++) enqueue(myQueue, array[i]); printf("\nafter elements added...\n"); printQueue(myQueue, printTest); dequeue(myQueue); dequeue(myQueue); dequeue(myQueue); dequeue(myQueue); dequeue(myQueue); printf("\nafter 4 elements deleted...\n"); printQueue(myQueue, printTest); destroyQueue(myQueue); return 0; }
int main (int argc, char* argv[]) { //create an array of processes called queue. ticks = 0; loop = 0; queue q; createQueue(q); while (loop == 0) { ticks++; printf("%d",ticks); int i; for (i = 1; i < getQueueSize(q); i++) { if (ticks - q.items[i].timeStamp > ticks) { if (getQueueSize(q) > 0) { scheduleProcess(q); } } } getUserInput(q); if (ticks % 10 == 0) { for (i = 1; i < getQueueSize(q); i++) { q.items[i].priority = q.items[i].priority + 1; } } } }
/** * @brief Create an SQS queue. * * This convenience overload simply allows callers to skip creating the required * SqsCreateQueueRequest object. * * @param queueName Name of the queue to create. * @param attributes Optional attributes to include with the request. See the * SqsCreateQueueRequest documentation for valid attributes. * * @return A pointer to a related response object. * * @note The caller is to take responsbility for the resulting pointer. * * @see SqsCreateQueueRequest */ SqsCreateQueueResponse * SqsClient::createQueue(const QString &queueName, const QVariantMap &attributes) { SqsCreateQueueRequest request(queueName); request.setAttributes(attributes); return createQueue(request); }
void OpenCL::setup(int clDeviceType_, int deviceNumber) { ofLog(OF_LOG_VERBOSE, "OpenCL::setup " + ofToString(clDeviceType_)); if(isSetup) { ofLog(OF_LOG_VERBOSE, "... already setup. returning"); return; } if(deviceInfo.size() == 0) getDeviceInfos(clDeviceType_); deviceNumber = (deviceNumber + getNumDevices()) % getNumDevices(); clDevice = deviceInfo[deviceNumber].clDeviceId; cl_int err; clContext = clCreateContext(NULL, 1, &clDevice, NULL, NULL, &err); if(clContext == NULL) { ofLog(OF_LOG_ERROR, "Error creating clContext."); assert(err != CL_INVALID_PLATFORM); assert(err != CL_INVALID_VALUE); assert(err != CL_INVALID_DEVICE); assert(err != CL_INVALID_DEVICE_TYPE); assert(err != CL_DEVICE_NOT_AVAILABLE); assert(err != CL_DEVICE_NOT_FOUND); assert(err != CL_OUT_OF_HOST_MEMORY); assert(false); } createQueue(); }
// Driver program to test above functions int main() { // Let cache can hold 4 pages Queue* q = createQueue( 4 ); // Let 10 different pages can be requested (pages to be // referenced are numbered from 0 to 9 Hash* hash = createHash( 10 ); // Let us refer pages 1, 2, 3, 1, 4, 5 ReferencePage( q, hash, 1); ReferencePage( q, hash, 2); ReferencePage( q, hash, 3); ReferencePage( q, hash, 1); ReferencePage( q, hash, 4); ReferencePage( q, hash, 5); // Let us print cache frames after the above referenced pages printf ("%d ", q->front->pageNumber); printf ("%d ", q->front->next->pageNumber); printf ("%d ", q->front->next->next->pageNumber); printf ("%d ", q->front->next->next->next->pageNumber); return 0; }
queue *HuffmanTree(FILE *fin, uint32_t *numberOfChars){ unsigned char c; int v[256] = {0}; int i; struct huffman Huffman[256]; /* Read all characters from file */ while(fscanf(fin, "%c", &c) && !feof(fin)){ v[c]++; (*numberOfChars)++; } /* Exception * NULL counted more than once */ if(v[0] > 0) v[0]--; /* Create node for each character */ int countNodes = 0; for(i = 0; i < 256; i++){ if(v[i] != 0){ Huffman[countNodes].caracter = i; Huffman[countNodes].frequency = (float)(v[i])/(float)(*numberOfChars); countNodes++; } } /* Priority queue */ queue *head = createQueue(Huffman, countNodes); /* Create HuffmanTree */ queue *root = createHuffmanTree(head); return root; }
int main() { struct queue *q = NULL; q = createQueue(); if (!q) { printf("Failed to allocate memory for Queue\n"); return 1; } enqueue(q,10); enqueue(q,20); enqueue(q,30); printQueue(q); printf("Queue Peek: %d\n",peekqueue(q)); dequeue(q); enqueue(q,40); printQueue(q); printf("Queue Peek: %d\n",peekqueue(q)); dequeue(q); printQueue(q); dequeue(q); dequeue(q); dequeue(q); printf("Queue Peek: %d\n",peekqueue(q)); printQueue(q); enqueue(q,50); printQueue(q); freeQueue(q); return 0; }
bool isBipartite(int G[][V], int src) { int colorMatrix[V], color, temp, u; initColorMatrix(colorMatrix, V); struct Queue* queue = createQueue(V); color = 1; colorMatrix[src] = color; enQueue(queue, src); while (!isEmpty(queue)) { temp = deQueue(queue); // assign alternate color to its neighbor color = 1 - colorMatrix[temp]; for (u = 0; u < V; ++u) { // an edge exists and destination not colored if (G[temp][u] && colorMatrix[u] == -1) { colorMatrix[u] = color; enQueue(queue, u); } else if (G[temp][u] && colorMatrix[u] == colorMatrix[temp]) return false; } } return true; }
int auction_omp_search(int *pr, int *P, int (*a)[2], int nodes, int arcs, int s, int t) { int result; int item; omp_lock_t pmux[Nodes]; Queue queue = createQueue(); int finalResult = -1; //#pragma omp parallel for for(int i = 0; i < NODES; ++i) { fpr[i] = INF; } //inicjalizacja kolejki for(int i = 0; i < NODES; ++i) { queue.push(&queue, i); } #pragma omp parallel do private(result, item, pr, P) shared(pmux, fpr) do { //dodac synchronizacje kolejki item = queue.pop(&queue); result = auction_search(pr, P, a, fpr, pmux, nodex, arcs, s, item); if (result == -2) { //wstaw na koniec kolejki gdy jest blokada queue.push(&queue, item); } else if (result == -1) { //usun z kolejki, usuwamy z kolejki robiac pop wiec nic tutaj nie trzeba robic //nothing to write } else if (result == 0) { //s==t czyli usuwam z kolejki, usuwamy z kolejki robiac pop wiec nic tutaj nie trzeba robic //nothing to write } else { //wstaw wynik do tablicy fpr fpr[item] = result; if (item == t) { finalResult = result; break; } } } while (queue.size != 0); queue.clear(&queue); return finalResult; }
void test_copy_element(void) { queues[1] = 2; queues[2] = 3; int *new_queues = createQueue(6); copy_element(1, 3, 0, queues, new_queues); TEST_ASSERT_EQUAL(2, new_queues[0]); TEST_ASSERT_EQUAL(3, new_queues[1]); }
void startqueue() { gameclock = 0; rulesque = createQueue(rulesque, 100); queue = createQueue(queue , 100); int iret; iret = pthread_create(&enterPhyThread,NULL,enterPhyData,NULL); if(iret !=0) { printf("error"); } iret = pthread_create(&enterNetThread,NULL,enterNetData,NULL); if(iret !=0) { printf("error"); } }
int main(){ Queue *fila; int i, val; fila = createQueue(); initializeQueue(fila); srand(time(NULL)); for (i=10; i>0; i--) { if (enqueue(fila, rand()%30), &IntCmp) { printf("O valor foi inserido\n"); } else { printf("Não há mais espaço na fila\n"); } } for (i=0; i<4; i++) { if (dequeue(fila, &val)) { printf("--%d\n", val); } else { printf("Não há nada na fila\n"); } } if (peek(fila, &val)) { printf("peek: %d\n", val); } else { printf("Não há nada na fila\n"); } printf("Tamanho da fila: %d\n", sizeQueue(fila)); if (isEmptyQueue(fila)) { printf("A fila está vazia\n"); } else { printf("A fila não está vazia\n"); } printQueue(fila); return(0); }
struct TreeNode* newTreeNode(struct Graph* state, int type, int moveVal, int lev) { struct TreeNode* newNode = (struct TreeNode*) malloc(sizeof(struct TreeNode)); newNode->state = state; newNode->children = createQueue(); newNode->moveType = type; newNode->moveValue = moveVal; newNode->level = lev; newNode->evaluation = NULL; }