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: A CircularDeque is 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 elements
deque = 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 CircularDeque object with the name deque. For this object, we set the capacity as 5, meaning that it can hold 5 elements. Also, we set the elements of deque to be the int datatype.
  • Lines 6–8: We add three integer elements 10, 20, and 30 to deque using the push! method.
  • Line 11: We use the length method to get the number of elements present in the deque object. In our case, we'll get 3 as a result.

Free Resources