How to get the number of elements in CircularDeque in Julia
Overview
We can use the length method to get the number of elements present in the CircularDeque.
Note: ACircularDequeis a double-ended queue implementation using a circular buffer of fixed capacity (while creating the deque, the capacity should be provided). It supports insertion and deletion on both ends.
Syntax
length(deque_object)
Parameters
This method takes the CircularDeque object as an argument.
Return value
This method returns the number of elements present in the CircularDeque.
Example
The code below demonstrates how to get the number of elements present in the CircularDeque object.
using DataStructures#create a new CircularDeque object for 5 elementsdeque = CircularDeque{Int}(5);push!(deque,10);push!(deque, 20);push!(deque, 30);println("Deque -> $(deque)")println("Number of elements in the deque $(length(deque))")
Explanation
- Line 4: We create a
CircularDequeobject with the namedeque. For this object, we set the capacity as 5, meaning that it can hold 5 elements. Also, we set the elements ofdequeto be theintdatatype. - Lines 6–8: We add three integer elements
10,20, and30todequeusing thepush!method. - Line 11: We use the
lengthmethod to get the number of elements present in thedequeobject. In our case, we'll get3as a result.