Beispiel #1
0
void exampleArrayQueue()
{
	ArrayQueue* Q = newArrayQueue(8);

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

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

	// A queue with integers, primitive data types require some trickyness
	aqueue_clear(Q);
	int x[] = {1, 2};
	int y = 3;
	aqueue_offer(Q, &x[0]);
	aqueue_offer(Q, &x[1]);
	aqueue_offer(Q, &y);
	
	while (!aqueue_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*)aqueue_poll(Q)));

	printf("\n");
	
	// Fill it up and resize it if an offer has failed
	char* letters[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"};
	for (y = 0; y < 11; y++)
		aqueue_offerf(Q, letters[y]);

	while (!aqueue_isEmpty(Q))
		printf("%s ", (char*)aqueue_poll(Q));
	printf("\n");

	// This will clear the queue of any nodes and pool them and then free
	// the queue itself from memory
	aqueue_free(Q);
}
Beispiel #2
0
/*
 * Realizes the queue ADT with an Array and then a LinkedList
 */
int main(void) {
    Queue queue;

    printf("Array Queue Tests\n");
    newArrayQueue(&queue);
    testQueue(&queue);
    deleteArrayQueue(&queue);

    printf("\n");
    printf("Linked Queue Tests\n");
    newLinkedQueue(&queue);
    testQueue(&queue);
    deleteLinkedQueue(&queue);
    return 0;
}