Queue - newell-romario/DSA GitHub Wiki
A queue is a linear data structure that follows the first in first out (FIFO) philosophy where elements are removed according to the insertion order. A queue is reminiscent of cashier lines where the order in which you joined determines the order you're served. A queue has two main operations like its counterpart the stack, mainly enqueue and dequeue. Both of these operations run in constant time or O(1). To access our queue implementation the header file r2_queue.h can be included.
Representation
Our queue is represented the by the structure below:
struct r2_queue{
struct r2_queuenode *front;/*first element in the queue*/
struct r2_queuenode *rear;/*last element in the queue*/
r2_int64 qsize; /*number of elements in the queue*/
r2_cmp cmp;/*A comparison callback function*/
r2_cpy cpy;/*A callback function to copy values*/
r2_fd fd;/*A callback function that release memory*/
};
The only fields that should be accessed or modified are cmp, cpy and fd.
Creation
struct r2_queue *queue = r2_create_queue(cmp, cpy, fd);
Destruction
r2_destroy_queue(queue);
Enqueue
if(r2_queue_enqueue(queue, data) == TRUE)
printf("Enqueued");
Dequeue
if(r2_queue_dequeue(queue) == TRUE)
printf("Dequeued");
Front
//First element in the queue
struct r2_queuenode *front = r2_queue_front(queue);
int *id = front->data;
printf("\nFirst customer: %d", *id);
Rear
//Last element in the queue
struct r2_queuenode *rear = r2_queue_rear(queue);
int *id = rear->data;
printf("\nLast customer: %d", *id);