Exemplo n.º 1
0
void exampleQueue()
{
	// Use pooling for efficiency, if you don't want to use pooling
	// then comment out this line.
	pool_queue(16);

	Queue* Q = newQueue();

	// A queue with strings
	queue_offer(Q, "First");
	queue_offer(Q, "In");
	queue_offer(Q, "First");
	queue_offer(Q, "Out.");
	
	// Peek at the head of the queue
	printf("%s\n", (char*)queue_peek(Q));

	// Traverse through the queue polling each string
	while (!queue_isEmpty(Q))
		printf("%s ", (char*)queue_poll(Q));
	printf("\n");


	// A queue with integers, primitive data types require some trickyness
	queue_clear(Q);
	int x[] = {1, 2};
	int y = 3;
	queue_offer(Q, &x[0]);
	queue_offer(Q, &x[1]);
	queue_offer(Q, &y);
	
	while (!queue_isEmpty(Q))
		// You first need to cast it using (int*) and since its a pointer to
		// an integer you need to get the value of the pointer using *
		// You could similarly use:
		// 	int* z = queue_poll(Q);
		//		printf("%d ", *z);
		printf("%d ", *((int*)queue_poll(Q)));

	printf("\n");
	
	// This will clear the queue of any nodes and pool them and then free
	// the queue itself from memory
	queue_free(Q);
	
	// If you're not using pooling this can be commented out. This will
	// free all pooled nodes from memory. Always call this at the end 
	// of using any Queue.
	unpool_queue();
}
Exemplo n.º 2
0
void arrive_SRTN(int id, int length)
{
	def_task task;
	task.id = id;
	task.length = length;
	if (queue_size(q) == 0)
		running_task = task.id;
	queue_offer(q,task);

	//TODO: sort
	def_task tasks[q->size];
	Element* temp;
	temp = q->head;
	int counter = 0;
	//copy tasks to array
	while(temp != NULL){
		tasks[counter] = temp->task;
		temp = temp->next;
		counter++;
	}
	def_task t_temp;
	//bubble sort array of tasks
	for (int c = 0 ; c < ( q->size - 1 ); c++)
  {
    for (int d = 0 ; d < q->size - c - 1; d++)
    {
      if (tasks[d].length > tasks[d+1].length) /* For decreasing order use < */
      {
        t_temp = tasks[d];
        tasks[d] = tasks[d+1];
        tasks[d+1] = t_temp;
      }
    }
  }
	temp = q->head;
	counter = 0;
	//copy tasks back
	while(temp != NULL){
		temp->task = tasks[counter];
		temp = temp->next;
		counter++;
	}
	running_task = q->head->task.id;
}