Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

circulardeque
julia
communitycreator

How to get the first and last element of CircularDeque in Julia

Programming Bytes

Overview

We can use:

  • first() method to obtain the first element of the CircularDeque .
  • last() method to obtain the last element of the CircularDeque .

    CircularDeque is a double-ended queue implementation using a circular buffer of fixed capacity (while creating the deque, capacity should be provided). It supports insertion and deletion on both ends.

    Syntax

    # syntax for first method
    first(deque_object)
    
    # syntax for last method
    last(deque_object)
    Julia first() and last() function syntax

    Parameter

    Both the first() and last() methods take the CircularDeque object as an argument.

    Return value

    • The first() method returns the first element of the CircularDeque.
    • The last() method returns the last element of the CircularDeque.

      Example

      The code below demonstrates how to get the first and last elements of 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("First element of Deque is ->  $(first(deque))")
      println("Last element of Deque is ->  $(last(deque))")
      Getting the first and last element of the CircularDeque object

      Explanation

      • Line 4: We create a CircularDeque object with the name deque. For this object, we set the capacity to55, meaning it can hold 55elements. Further, we also set the elements of deque to be Int datatype.
      • Lines 6–8: We use the push!() method to add three integer elements 10, 20, 30 to the deque.
      • Line 11: We use the first() method to obtain the first element of the deque() object. In our case, we get 10 as a result.
      • Line 12: We use the last() method to obtain the last element of the deque object. In our case, we get 30 as a result.

      RELATED TAGS

      circulardeque
      julia
      communitycreator
      RELATED COURSES

      View all Courses

      Keep Exploring