static void merge(int s[], int low, int middle, int high) { int i; queue buffer1, buffer2; /* buffers to hold elements for merging*/ init_queue(&buffer1); init_queue(&buffer2); for (i = low; i <= middle; i++) en_queue(&buffer1, &s[i], sizeof (int)); for (i = middle + 1; i <= high; i++) en_queue(&buffer2, &s[i], sizeof (int)); i = low; while (!(empty_queue(&buffer1) || empty_queue(&buffer2))) { if (*(int*) head_queue(&buffer1) <= *(int*) head_queue(&buffer2)) s[i++] = deq_buffer(&buffer1); else s[i++] = deq_buffer(&buffer2); } while (!empty_queue(&buffer1)) s[i++] = deq_buffer(&buffer1); while (!empty_queue(&buffer2)) s[i++] = deq_buffer(&buffer2); clear_queue(&buffer1); clear_queue(&buffer2); }
void init_system_queues(FILE *ptr_file, queue *q_ready, queue *q_process) { init_ready_queue(ptr_file, q_ready); if (empty_queue(q_ready)) { perror("No jobs were submitted for processing.\nExiting...\n"); exit(EXIT_FAILURE); } process *curr_proc = NULL; while (!(empty_queue(q_ready))) { /* move all processes in the ready queue to the processes queue */ curr_proc = (process *) dequeue(q_ready); enqueue(q_process, curr_proc); } curr_proc = NULL; matrix_t i; for (i = 0; i < NPROC; i++) { /* Move NPROC processes from the processes queue to the ready state */ curr_proc = (process *) dequeue(q_process); curr_proc->row_index = i; /* ensure to give the ready state processes proper indices into the matrices */ increment_row_vector(curr_proc->max_need_vector, claim_matrix[curr_proc->row_index]); enqueue(q_ready, curr_proc); } return; }
void merge(int *p, int low, int middle, int high) { int i; queue_t *q1 = &queue1; queue_t *q2 = &queue2; queue_init(q1); queue_init(q2); for (i = low; i <= middle; i++) en_queue(q1, p[i]); for (i = middle + 1; i <= high; i++) en_queue(q2, p[i]); i = low; while (!(empty_queue(q1) || empty_queue(q2))) { if (head_queue(q1) > head_queue(q2)) p[low++] = de_queue(q1); else p[low++] = de_queue(q2); } while (!(empty_queue(q1))) p[low++] = de_queue(q1); while (!(empty_queue(q2))) p[low++] = de_queue(q2); }
void rr_dev_reset_lineno (rr_dev dev) { dev->lineno = 0; /* we invalidate all these line numbers */ empty_queue (dev, RR_PRIO_RESEND); empty_queue (dev, RR_PRIO_SENTCACHE); rr_dev_enqueue_internal (dev, RR_PRIO_HIGH, "M110", 4, -1); dev->init_send_count = dev->dev_cmdqueue_size - 1; }
int main(int argc, char const *argv[]) { int data[]={1,2,3,4,5,6,7,8,9,10}; int size=sizeof(data)/sizeof(int); int i=0; while(i<6) enqueue(data[i++]); while(!empty_queue()) printf("%d\n",dequeue()); while(i<size) enqueue(data[i++]); while(!empty_queue()) printf("%d\n",dequeue()); return 0; }
static void empty_buffers (rr_dev dev) { unsigned int i; for (i = 0; i < RR_PRIO_ALL_QUEUES; ++i) empty_queue (dev, i); }
static int load_from_wait(queue *q_wait, queue *q_ready) { if (empty_queue(q_wait)) { /* cannot load from an empty queue */ return FALSE; } /* attempt to load a process if it has a valid request vector and does not cause deadlock */ process *check_proc = (process *) peek(q_wait); if (!(is_valid_request(check_proc) && is_no_deadlock(check_proc))) { return FALSE; } process *proc = (process *) dequeue(q_wait); /* necessary matrix and vector updates ORDER IS IMPORTANT */ increment_row_vector(proc->max_need_vector, claim_matrix[proc->row_index]); increment_row_vector(proc->alloc_vector, allocation_matrix[proc->row_index]); decrement_row_vector(proc->alloc_vector, available_vector); clear_row_vector(proc->alloc_vector); matrix_difference(claim_matrix, allocation_matrix, difference_matrix); enqueue(q_ready, proc); return TRUE; }
/* Remove node *node from queue */ void remove_node(command_queue_t *q, command_queue_node_t *node) { if (empty_queue(q)) { return; } command_queue_node_t *current = q->head; if (node == first_node(q)) { q->head = node->next; current = q->head; } else { while (current->next != node) { if (current->next == NULL) { /* Node not in queue */ return; } current = current->next; } current->next = node->next; } if (node == last_node(q)) { q->last = current; } }
void free_queue(struct Queue *q) { if (q != NULL) { empty_queue(q); free(q); } }
void bfs(graph *g, int start) { queue q; /* queue of vertices to visit */ int v; /* current vertex */ int y; /* successor vertex */ edgenode *p; /* temporary pointer */ init_queue(&q); enqueue(&q,start); discovered[start] = TRUE; while (empty_queue(&q) == FALSE) { v = dequeue(&q); processed[v] = TRUE; p = g->edges[v]; while (p != NULL) { y = p->y; if ((processed[y] == FALSE) || g->directed) process_edge(v,y); if (discovered[y] == FALSE) { enqueue(&q,y); discovered[y] = TRUE; parent[y] = v; } p = p->next; } } }
END_TEST START_TEST(test_empty_queue) { queue *qptr = init_queue_with(NULL); ck_assert_int_eq(1, empty_queue(qptr)); }
END_TEST START_TEST(test_non_empty_queue) { int items[] = { 5 }; queue *qptr = init_queue_with(items); ck_assert_int_eq(0, empty_queue(qptr)); }
person_type queue_front(cir_queue *q)//访问队头元素,并返回 { if (empty_queue(q))//判断是否为空 { printf("队列为空,操作失败!"); exit(-1); } return q->qBase[q->front]; }
status_t deq_queue(QUEUE *ptr, DATA_T *data) { if (empty_queue(ptr)) return FAIL; *data = ptr->que_data[ptr->front]; ptr->front = (ptr->front + 1) % QUEUEMAX; return OK; }
void main() { int choice, n, flag = 0; char ch; for (int i = 0; i < 100; i++) { printf("MENU\n"); printf("Enter 1 to INSERT an element in the queue\n"); printf("Enter 2 to DELETE an element in the queue\n"); printf("Enter 3 to DISPLAY the elements of the queue\n"); printf("Enter 4 to CHECK if the queue is EMPTY\n"); printf("Enter 5 to KNOW the FIRST element of the queue\n"); printf("Enter 6 to KNOW the queue SIZE\n"); printf("Enter 7 to Destroy the Queue\n"); printf("Enter 8 to EXIT the program\n"); printf("Enter your Choice:"); choice = i % 7 + 1; switch(choice) { case 1: insert_rear(); break; case 2: delete_front(); break; case 3: display_queue(); break; case 4: empty_queue(); break; case 5: front_ele(); break; case 6: n = queue_size(); printf("\nthe queue size is: %d", n); break; case 7: destroy(); flag = 1; break; case 8: exit(0); break; default: printf("WRONG CHOICE\n"); } } if (flag == 0) { destroy(); } }
/* 将数据出队,并将其保存在out_data指针指向的位置 */ bool out_queue(PQUEUE pQueue,int *out_data) { if(empty_queue(pQueue)) return false; else { *out_data = pBase[pQueue->front]; pQueue->front = (pQueue->front+1)%(len+1); return true; } }
bool del_queue(QUEUE *pQ,int *pVal) { if(empty_queue(pQ)) return false; else { *pVal=pQ->pBase[pQ->front]; pQ->front=(pQ->front+1)%6; return true; } }
static void merge(item_type s[], int low, int middle, int high) { int i; /* counter */ queue buffer1, buffer2; /* buffers to hold elements for merging */ init_queue(&buffer1); init_queue(&buffer2); for(i = low; i <= middle; i++) enqueue(&buffer1, s[i]); for(i = middle + 1; i <= high; i++) enqueue(&buffer2, s[i]); i = low; while(!(empty_queue(&buffer1) || empty_queue(&buffer2))) { if(headq(&buffer1) <= headq(&buffer2)) s[i++] = dequeue(&buffer1); else s[i++] = dequeue(&buffer2); } while(!empty_queue(&buffer1)) s[i++] = dequeue(&buffer1); while(!empty_queue(&buffer2)) s[i++] = dequeue(&buffer2); }
void tts_s(void) { int rc; ilctts_stop_request(st); stop_requested = 1; rc = espeak_Cancel(); pthread_mutex_lock(&queue_guard_mutex); /* flush the queue */ empty_queue(); pthread_mutex_unlock(&queue_guard_mutex); return; } /* end tts_s */
void merge(int s[], int l, int m, int h) { int i; queue_t buffer1, buffer2; init_queue(&buffer1); init_queue(&buffer2); for(i = l; i <= m; ++i) enqueue(&buffer1, s[i]); for(i = m+1; i <= h; ++i) enqueue(&buffer2, s[i]); i = l; while(!(empty_queue(&buffer1) || empty_queue(&buffer2))) { if(headq(&buffer1) <= headq(&buffer2)) s[i++] = dequeue(&buffer1); else s[i++] = dequeue(&buffer2); } while(!empty_queue(&buffer1)) s[i++] = dequeue(&buffer1); while(!empty_queue(&buffer2)) s[i++] = dequeue(&buffer2); }
int queue(struct jitter_buffer *q, rtp_msg_t *pk) { if (q->size == q->capacity) { printf("buffer full, emptying buffer...\n"); empty_queue(q); return 0; } if (q->size > 8) q->queue_ready = 1; ++q->size; ++q->rear; if (q->rear == q->capacity) q->rear = 0; q->queue[q->rear] = pk; int a; int b; int j; a = q->rear; for (j = 0; j < q->size - 1; ++j) { b = a - 1; if (b < 0) b += q->capacity; if (sequence_number_older(q->queue[b]->_header->_sequence_number, q->queue[a]->_header->_sequence_number, q->queue[b]->_header->_timestamp, q->queue[a]->_header->_timestamp)) { rtp_msg_t *temp; temp = q->queue[a]; q->queue[a] = q->queue[b]; q->queue[b] = temp; printf("had to swap\n"); } else { break; } a -= 1; if (a < 0) a += q->capacity; } if (pk) return 1; return 0; }
void email_asciifile_tail( FILE* output, const char* file, int lines ) { FILE *input; int ch, last_ch; long loc; int first_line = TRUE; TAIL_QUEUE queue, *q = &queue; if( !file ) { return; } if( (input=safe_fopen_wrapper_follow(file,"r",0644)) == NULL ) { // try the .old file in the off shoot case we hit this during the transition. std::string szTmp = file; szTmp += ".old"; if( (input=safe_fopen_wrapper_follow(szTmp.c_str(),"r",0644)) == NULL ) { dprintf( D_FULLDEBUG, "Failed to email %s: cannot open file\n", file ); return; } } init_queue( q, lines ); last_ch = '\n'; while( (ch=getc(input)) != EOF ) { if( last_ch == '\n' && ch != '\n' ) { insert_queue( q, ftell(input) - 1 ); } last_ch = ch; } while( !empty_queue( q ) ) { loc = delete_queue( q ); /* If this is the first line, print header */ if ( first_line ) { first_line = FALSE; fprintf(output,"\n*** Last %d line(s) of file %s:\n", lines, file); } /* Now print the line */ display_line( loc, input, output ); } (void)fclose( input ); /* if we printed any of the file, print a footer */ if ( first_line == FALSE ) { fprintf(output,"*** End of file %s\n\n", condor_basename(file)); } }
int queue(struct jitter_buffer *q, RTPMessage *pk) { if (q->size == q->capacity) { /* Full, empty queue */ empty_queue(q); /*rtp_free_msg(NULL, pk);*/ return 0; } if (q->size > 8) q->queue_ready = 1; ++q->size; ++q->rear; if (q->rear == q->capacity) q->rear = 0; q->queue[q->rear] = pk; int a; int b; int j; a = q->rear; for (j = 0; j < q->size - 1; ++j) { b = a - 1; if (b < 0) b += q->capacity; if (sequence_number_older(q->queue[b]->header->sequnum, q->queue[a]->header->sequnum, q->queue[b]->header->timestamp, q->queue[a]->header->timestamp)) { RTPMessage *temp; temp = q->queue[a]; q->queue[a] = q->queue[b]; q->queue[b] = temp; /*printf("had to swap\n");*/ } else { break; } a -= 1; if (a < 0) a += q->capacity; } if (pk) return 1; return 0; }
/* Add new node to end of queue */ void put_queue(command_queue_t *q, command_queue_node_t *node) { if (empty_queue(q)) { q->head = node; q->last = node; } else { q->last->next = node; q->last = node; } }
bool out_queue(QUEUE * pQ,int * pVal) { if(empty_queue(pQ)) { return false; } else { *pVal=pQ->pBase[pQ->front]; pQ->front=(pQ->front+1)%6; printf("出队元素%d\n",*pVal); return true; } }
person_type de_queue(cir_queue *q)//出队函数,用p返回出队值 { person_type p; if (empty_queue(q))//判断是否为空 { printf("队列为空,无法实现出队操作,程序终止!"); exit(-1); } else //不为空进行操作 { p = q->qBase[q->front]; q->front = (q->front + 1) % queuesize; return p; } }
void *main_tsp(void *arg){ unsigned long long perf; struct timespec t1, t2; uint64_t vpres=0; long long int myCuts = 0; clock_gettime (CLOCK_REALTIME, &t1); /* calculer chacun des travaux */ tsp_path_t solution; memset (solution, -1, MAX_TOWNS * sizeof (int)); solution[0] = 0; pthread_mutex_lock(&q_mutex); while (!empty_queue (&q, &q_mutex)) { int hops = 0, len = 0; get_job (&q, solution, &hops, &len, &vpres, &q_mutex); pthread_mutex_unlock(&q_mutex); //printf("Thread %ld\n", syscall(SYS_gettid)); // le noeud est moins bon que la solution courante if (minimum < INT_MAX && (nb_towns - hops) > 10 && ( (lower_bound_using_hk(solution, hops, len, vpres)) >= minimum || (lower_bound_using_lp(solution, hops, len, vpres)) >= minimum)){ pthread_mutex_lock(&q_mutex); continue; } tsp (hops, len, vpres, solution, &myCuts, sol, &sol_len); pthread_mutex_lock(&q_mutex); } pthread_mutex_unlock(&q_mutex); /* update cuts */ pthread_mutex_lock(&cuts_mutex); cuts += myCuts; pthread_mutex_unlock(&cuts_mutex); clock_gettime (CLOCK_REALTIME, &t2); perf = TIME_DIFF (t1,t2); printf("Son thread %ld finished after %lld.%03lld ms\n\n", syscall(SYS_gettid), perf/1000000ll, perf%1000000ll); return 0 ; }
int main(void) { queue q; // Discovered vertices yet to be processed int v; // Current Vertex int x[] = {1,2,3,4,5,6,7,8}; init_queue(&q); for (int i = 0; i < 8; i++) { enqueue(&q, x[i]); } while (!empty_queue(&q)) { v = dequeue(&q); printf("vertex.. %d\n", v); } free_queue(&q); }
//头部删除元素 Status dequeue(QueueLink *Q, ElemType *e){ if (empty_queue(Q)){ return ERROR; } Node *p = Q->front; Q->front = p->next; if (Q->front == NULL){ Q->rear = Q->front; } *e = p->data; free(p); return OK; }
int mst_prim(int n) { int i; //int m; //m=n+1; for(i=0;i<n;i++) { queue[0][i]=i+1; queue[1][i]=100; queue[2][i]=1; parent[i]=NULL; } queue[1][0]=0; // make weight of root node is 0 and first node is always root....:) //printf("preprocessing is done\n"); int m; int j=0; while(empty_queue(n)!=1) { extract_min(n); //printf(" node extracted is %d\n",u); queue[2][u-1]=0; //printf("parent of %d=%d\n\n",u,parent[u]); if(parent[u]!=NULL) { edge[j++]=parent[u]*10+u; } p=g[u]; while(p!=NULL) { //printf("Enter in while p=%d\n",p->data); m=(p->data)-1; if(queue[2][m]==1 && p->w <queue[1][m]) { parent[p->data]=u; queue[1][m]=p->w; } //printf("weight of %d=%d\n",p->data,queue[1][m]); p=p->next; } } //printf("mst end\n"); }