How to use the pushfirst! method of CircularDeque in Julia
Overview
The pushfirst! method is used to add an element at the beginning of the CircularDeque object.
Note:CircularDequeis 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
pushfirst!(deque_object, items...) -> collection
Parameter
This method takes an CircularDeque object and the values to be inserted as arguments.
Return value
This method returns the CircularDeque object, which is passed in the arguments.
Example
using DataStructures#create a new CircularDeque object for 5 elementsdeque = CircularDeque{Int}(5);push!(deque,2);push!(deque, 3);push!(deque, 4);println("\nDeque -> $(deque)")# insert 0 and 1 at the beginning of the dequepushfirst!(deque, 0, 1);println("\nDeque -> $(deque)")
Explanation
- Line 4: We create a
CircularDequeobject with the namedeque. For this object, we set the capacity as5, meaning it can hold 5 elements. Also, we set the elements ofdequeto be an integer datatype. - Lines 6–8: We add three integer elements,
2,3, and4, todequeusing thepush!method. - Line 13: We use the
pushfirst!method to add two elements,0,1, at the beginning of thedequeobject. After calling this method, the elements ofdequewill be0,1,2,3,4.