In Julia, the Queue
data structure follows the FIFO (First In First Out) method, which means that the item first entered into the queue will be removed first.
Let’s look at a real-life example: a line of people waiting at a concert. A new person arriving would join the end of the line, while the person at the front of the line would leave the line and enter the venue.
Queue
mainly includes two operations:
enqueue
: It adds an element at the end of the Queue
.dequeue
: It removes the first element of the Queue
.Below is a sample code for using a Queue
in Julia:
using DataStructures# create a new Queuequeue = Queue{Int}()# enqueue an item to the top of the queueenqueue!(queue, 1)enqueue!(queue, 2)enqueue!(queue, 3)println("The queue is $(queue)")println("\nThe Length of queue is :", length(queue) )println("\nThe First element of queue is :", first(queue) )println("\nRemoving first element of queue is :", dequeue!(queue) )println("\nThe queue is: $(queue)")# emptying the queueempty!(queue)println("After Emptying queue is: $(queue)")
Queue
object with the name queue
.enqueue!()
method to insert three elements into the queue
.length()
method to get the number of elements of the queue
.first()
method to get the first element of the queue
.dequeue!()
method to remove the first element of the queue
.empty()
method to remove all elements of the queue
.Free Resources