dsa in dart
Data Structure & Algorithm in dart

Introduction: Building a robust & efficient solutions to real-world programs we can combine Dart Programming language with Data Structures & Algorithms (DSA).

Creating a Dart project for DSA integration

Dart project for integrating with DSA:

  • Install Dart SDK (if not already installed).
  • Create a new Directory for Dart project.
  • open the folder into terminal
  • Now in terminal RUN ‘dart create my_first_dart_dsa_project to create a new dart project into the opened folder/directory.
  • Navigate into the project ‘my_first_dart_dsa_project‘ & start code.

Implementing Data Structure in dart language

You all know that Implementing a good Data Structure will give your dart code a better backbone. Let’s implement essential data structure like lists, Stacks, Queue, Trees etc. that to by using Dart Programming Language.

Below is an example to create Stack in dart:

class Stack<T>{
    List<T> _items = [];
  
    void push(T value){
      _items.add(value);
    }
  
   T pop(){
     if(_items.isEmpty) throw Exception('Stack is Empty');
     return _items.removeLast();
   }
  
  
  bool get isEmpty => _items.isEmpty;

 // Optional: Peek at the top item without removing it
  T get top {
    if (_items.isEmpty) throw Exception('Stack is Empty');
    return _items.last;
  }

}
void main() {
  // Create a stack of integers
  Stack<int> intStack = Stack<int>();

  // Push data onto the stack
  intStack.push(10);
  intStack.push(20);
  intStack.push(30);

  print('Popped: ${intStack.pop()}'); // Should print: Popped: 30
  print('Popped: ${intStack.pop()}'); // Should print: Popped: 20

  // Check if the stack is empty
  print('Is the stack empty: ${intStack.isEmpty}'); // Should print: Is the stack empty? false

  // Push more data onto the stack
  intStack.push(40);
  intStack.push(50);

  // Peek at the top item
  print('Top item: ${intStack.top}'); // Should print: Top item: 50

  // Pop the remaining items
  print('Popped: ${intStack.pop()}'); // Should print: Popped: 50
  print('Popped: ${intStack.pop()}'); // Should print: Popped: 40
  print('Popped: ${intStack.pop()}'); // Should print: Popped: 10

  // Check if the stack is empty again
  print('Is the stack empty? ${intStack.isEmpty}'); // Should print: Is the stack empty? true

  // Attempt to pop from an empty stack (will throw an exception)
  try {
    intStack.pop();
  } catch (e) {
    print('Error: ${e.toString()}'); // Should print: Error: Exception: Stack is Empty
  }
  
}

Output

Popped: 30
Popped: 20
Is the stack empty? false
Top item: 50
Popped: 50
Popped: 40
Popped: 10
Is the stack empty? true
Error: Exception: Stack is Empty

Implementing of Algorithms

Algorithms are specially designed to logically manipulate & process the data efficiently.

Dart Code Example on implement Binary Search algorithm:

// Function to perform binary search on a sorted list
int binarySearch(List<int> list, int searchKey) {
  int low = 0;
  int high = list.length - 1;
  
  while (low <= high) {
    // Calculate the middle index
    int mid = low + ((high - low) ~/ 2);
    
    // Check if the searchKey is present at mid
    if (list[mid] == searchKey) {
      return mid;
    }
    // If searchKey is greater, ignore the left half
    else if (list[mid] < searchKey) {
      low = mid + 1;
    }
    // If searchKey is smaller, ignore the right half
    else {
      high = mid - 1;
    }
  }
  
  // If the searchKey is not present in the list
  return -1;
}
void main() {
  

  List<int> sortedList = [2, 3, 4, 10, 40];
  int searchKey = 10;
  
  int result = binarySearch(sortedList, searchKey);
  
  if (result != -1) {
    print("Element found at index: $result");
  } else {
    print("Element not found in the list");
  }
}

Output

Element found at index: 3


Conclusion

You’ve have successfully build a dart code that utilize Data Structure & Algorithms (DSA). By implement DSA in Dart we can create a super powerful dart application that can solve complex problems very efficiently.