Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dart
datastructure
communitycreator

What is the DoubleLinkedQueue.removeLast() method in Dart?

Surabhi Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The dart:collection library provides advanced collection support for the Dart language. It also contains the DoubleLinkedQueue<E> class which implements the abstract Queue<E> class, using a doubly-linked list.

The removeLast() method

The DoubleLinkedQueue<E> class contains the removeLast() method which removes and returns the last element of the queue.

A queue is a FIFO (First In First Out) data structure. In a queue, the element which is added first will be deleted first.

The removeLast() method modifies the state of the queue. It also throws an unhandled exception if it is called on an empty queue.

The illustration below shows how the removeLast() method works.

Removing the last element of the queue using the removeLast() method

Syntax

The prototype of the removeLast() method is shown below.

E removeLast()

Arguments

The removeLast() method does not take any arguments.

Return values

  • The removeLast() method removes and returns the last element of the queue.
  • The removeLast() method is a constant-time operation.

Code

The code below shows how the removeLast() method can be used in Dart.

import 'dart:collection';
void main() {
var quadNations = DoubleLinkedQueue<String>();
quadNations.add("India");
quadNations.add("US");
quadNations.add("Australia");
quadNations.add("Japan");
print('Printing Queue Elements');
print(quadNations);
var elem1 = quadNations.removeLast();
print('Removing ${elem1} from Queue using removeLast()');
print('Printing Queue Elements');
print(quadNations);
}

Explanation

The code above performs the following actions:

  • An instance of the DoubleLinkedQueue class of type String is initialized in line 3.
  • Next, a few strings, "India", "US", "Australia", and "Japan", are added to the queue using the add() method.
  • The queue elements are displayed as {India, US, Australia, Japan}. All the elements are added to the end of the queue, as the queue is a FIFO data structure.
  • The removeLast() method in line 12 removes and returns the last value in the list.
  • Upon printing all the queue elements, it can be observed that "Japan" has been removed from the queue.

RELATED TAGS

dart
datastructure
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring