Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Null Safety: Fix #176 #247

Closed
wants to merge 15 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ doc/api/
*.js_
*.js.deps
*.js.map
*.json
2 changes: 0 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
* [Cycle In Linked List](https://github.com/TheAlgorithms/Dart/blob/master/data_structures/linked_list/cycle_in_linked_list.dart)
* [Linked List](https://github.com/TheAlgorithms/Dart/blob/master/data_structures/linked_list/linked_list.dart)
* [Merge Sorted List](https://github.com/TheAlgorithms/Dart/blob/master/data_structures/linked_list/merge_sorted_list.dart)
* Quad Tree
* [Quad Tree](https://github.com/TheAlgorithms/Dart/blob/master/data_structures/quad_tree/quad_tree.dart)
* Queue
* [Circular Queue](https://github.com/TheAlgorithms/Dart/blob/master/data_structures/Queue/Circular_Queue.dart)
* [List Queue](https://github.com/TheAlgorithms/Dart/blob/master/data_structures/Queue/List_Queue.dart)
Expand Down
2 changes: 1 addition & 1 deletion array/car_pool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Solution Explanation: https://leetcode.com/problems/car-pooling/solutions/3252690/dart-time-o-n-o-1-space-solution/

import 'dart:math';
import 'package:test/test.dart';
import "package:test/test.dart";

bool carPooling(List<List<int>> trips, int capacity) {
List<int> passengerTimelineCount = List.filled(1001, 0);
Expand Down
7 changes: 4 additions & 3 deletions conversions/Decimal_To_Any.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ String decimalToAny(int value, int base) {
while (value > 0) {
int remainder = value % base;
value = value ~/ base;
output =
(remainder < 10 ? remainder.toString() : ALPHABET_VALUES[remainder]) +
output;
output = (remainder < 10
? remainder.toString()
: ALPHABET_VALUES[remainder] ?? '0') +
output;
}

return negative ? '-' + output : output;
Expand Down
2 changes: 1 addition & 1 deletion conversions/Decimal_to_Hexadecimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ String decimal_to_hexadecimal(int decimal_val) {
int remainder = decimal_val % 16;
decimal_val = decimal_val ~/ 16;
if (hex_table.containsKey(remainder.toString())) {
hex_val = hex_table[remainder.toString()];
hex_val = hex_table[remainder.toString()] ?? '';
} else {
hex_val = remainder.toString();
}
Expand Down
2 changes: 1 addition & 1 deletion conversions/Integer_To_Roman.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ List<String> RomanNumbers = [

List<String> integer_to_roman(int num) {
if (num < 0) {
return null;
return [];
}

List<String> result = [];
Expand Down
6 changes: 3 additions & 3 deletions conversions/binary_to_decimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:test/test.dart';

int binaryToDecimal(String binaryString) {
binaryString = binaryString.trim();
if (binaryString == null || binaryString == "") {
if (binaryString == "") {
throw FormatException("An empty value was passed to the function");
}
bool isNegative = binaryString[0] == "-";
Expand All @@ -14,8 +14,8 @@ int binaryToDecimal(String binaryString) {
if ("01".contains(binaryString[i]) == false) {
throw FormatException("Non-binary value was passed to the function");
} else {
decimalValue +=
pow(2, binaryString.length - i - 1) * int.parse((binaryString[i]));
decimalValue += (pow(2, binaryString.length - i - 1).toInt() *
(int.tryParse(binaryString[i]) ?? 0));
}
}
return isNegative ? -1 * decimalValue : decimalValue;
Expand Down
4 changes: 2 additions & 2 deletions conversions/binary_to_hexadecimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Map<String, String> hexTable = {
String binaryToHexadecimal(String binaryString) {
// checking for unexpected values
binaryString = binaryString.trim();
if (binaryString == null || binaryString == "") {
if (binaryString.isEmpty) {
throw new FormatException("An empty value was passed to the function");
}
try {
Expand All @@ -47,7 +47,7 @@ String binaryToHexadecimal(String binaryString) {
int i = 0;
while (i != binaryString.length) {
String bin_curr = binaryString.substring(i, i + 4);
hexValue += hexTable[bin_curr];
hexValue += hexTable[bin_curr] ?? '';
i += 4;
}

Expand Down
2 changes: 1 addition & 1 deletion conversions/binary_to_octal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {

String binaryToOctal(String binaryString) {
binaryString = binaryString.trim();
if (binaryString == null || binaryString == "") {
if (binaryString.isEmpty) {
throw new FormatException("An empty value was passed to the function");
}
bool isNegative = binaryString[0] == "-";
Expand Down
4 changes: 2 additions & 2 deletions conversions/hexadecimal_to_binary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Map<String, String> bin_table = {
String hexadecimal_to_binary(String hex_value) {
// checking for unexpected values
hex_value = hex_value.trim();
if (hex_value == null || hex_value == "") {
if (hex_value.isEmpty) {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -40,7 +40,7 @@ String hexadecimal_to_binary(String hex_value) {
if (!bin_table.containsKey(hex_cur)) {
throw new FormatException("An invalid value was passed to the function");
}
bin_val += bin_table[hex_cur];
bin_val += bin_table[hex_cur] ?? '';
i++;
}

Expand Down
8 changes: 4 additions & 4 deletions conversions/hexadecimal_to_decimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ void main() {

int hexadecimal_to_decimal(String hex_string) {
hex_string = hex_string.trim().toUpperCase();
if (hex_string == null || hex_string == "") {
if (hex_string == "") {
throw Exception("An empty value was passed to the function");
}
bool is_negative = hex_string[0] == "-";
if (is_negative) hex_string = hex_string.substring(1);
int decimal_val = 0;
for (int i = 0; i < hex_string.length; i++) {
if (int.parse(hex_string[i], onError: (e) => null) == null &&
if (int.tryParse(hex_string[i]) == null &&
hex_table.containsKey(hex_string[i]) == false) {
throw Exception("Non-hex value was passed to the function");
} else {
decimal_val += pow(16, hex_string.length - i - 1) *
int.parse((hex_string[i]), onError: (e) => hex_table[hex_string[i]]);
decimal_val += pow(16, hex_string.length - i - 1).toInt() *
(int.tryParse(hex_string[i]) ?? hex_table[hex_string[i]] ?? 0);
}
}
return is_negative ? -1 * decimal_val : decimal_val;
Expand Down
17 changes: 8 additions & 9 deletions conversions/hexadecimal_to_octal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ String hexadecimal_to_octal(String hex_val) {
int dec = 0;

// checking for null string passed to function
if (hex_val == null || hex_val == "") {
if (hex_val == "") {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -29,43 +29,42 @@ String hexadecimal_to_octal(String hex_val) {
case '7':
case '8':
case '9':
dec = dec + int.parse(ch) * pow(16, c);
dec = dec + (int.tryParse(ch) ?? 0) * pow(16, c).toInt();
c--;
break;
case 'a':
case 'A':
dec = dec + 10 * pow(16, c);
dec = dec + 10 * pow(16, c).toInt();
c--;
break;
case 'b':
case 'B':
dec = dec + 11 * pow(16, c);
dec = dec + 11 * pow(16, c).toInt();
c--;
break;
case 'c':
case 'C':
dec = dec + 12 * pow(16, c);
dec = dec + 12 * pow(16, c).toInt();
c--;
break;
case 'd':
case 'D':
dec = dec + 13 * pow(16, c);
dec = dec + 13 * pow(16, c).toInt();
c--;
break;
case 'e':
case 'E':
dec = dec + 14 * pow(16, c);
dec = dec + 14 * pow(16, c).toInt();
c--;
break;
case 'f':
case 'F':
dec = dec + 15 * pow(16, c);
dec = dec + 15 * pow(16, c).toInt();
c--;
break;
default:
throw new FormatException(
"An invalid value was passed to the function");
break;
}
}

Expand Down
4 changes: 2 additions & 2 deletions conversions/octal_to_binary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:test/test.dart';
String ocatal_to_binary(String oct_val) {
// checking for unexpected values
oct_val = oct_val.trim();
if (oct_val == null || oct_val == "") {
if (oct_val.isEmpty) {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -31,7 +31,7 @@ String ocatal_to_binary(String oct_val) {
// converting octal to decimal
int dec_val = 0, i = 0, bin_val = 0;
while (oct != 0) {
dec_val = dec_val + ((oct % 10) * pow(8, i));
dec_val = dec_val + ((oct % 10) * pow(8, i).toInt());
i++;
oct = oct ~/ 10;
}
Expand Down
4 changes: 2 additions & 2 deletions conversions/octal_to_decimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:test/test.dart';
String ocatal_to_decimal(String oct_val) {
// checking for unexpected values
oct_val = oct_val.trim();
if (oct_val == null || oct_val == "") {
if (oct_val.isEmpty) {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -33,7 +33,7 @@ String ocatal_to_decimal(String oct_val) {
// converting octal to decimal
int dec_val = 0, i = 0;
while (oct != 0) {
dec_val = dec_val + ((oct % 10) * pow(8, i));
dec_val = dec_val + ((oct % 10) * pow(8, i).toInt());
i++;
oct = oct ~/ 10;
}
Expand Down
6 changes: 3 additions & 3 deletions conversions/octal_to_hexadecimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Map<String, String> hex_table = {
String ocatal_to_hex(String oct_val) {
// checking for unexpected values
oct_val = oct_val.trim();
if (oct_val == null || oct_val == "") {
if (oct_val == "") {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -41,7 +41,7 @@ String ocatal_to_hex(String oct_val) {
// converting octal to decimal
int dec_val = 0, i = 0;
while (oct != 0) {
dec_val = dec_val + ((oct % 10) * pow(8, i));
dec_val = dec_val + ((oct % 10) * pow(8, i).toInt());
i++;
oct = oct ~/ 10;
}
Expand All @@ -56,7 +56,7 @@ String ocatal_to_hex(String oct_val) {
int remainder = dec_val % 16;
dec_val = dec_val ~/ 16;
if (hex_table.containsKey(remainder.toString())) {
hex_val = hex_table[remainder.toString()];
hex_val = hex_table[remainder.toString()] ?? '';
} else {
hex_val = remainder.toString();
}
Expand Down
29 changes: 14 additions & 15 deletions data_structures/HashMap/Hashing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//Email:[email protected]

class Node {
int data;
Node next;
int? data;
Node? next;

Node(int data) {
this.data = data;
Expand All @@ -12,12 +12,11 @@ class Node {
}

class LinkedList {
Node head;
Node? head;
int size;

LinkedList() {
LinkedList({this.size = 0}) {
head = null;
size = 0;
}

void insert(int data) {
Expand All @@ -38,15 +37,15 @@ class LinkedList {
print("underFlow!");
return;
} else {
Node curr = head;
if (curr.data == data) {
head = curr.next;
Node? curr = head;
if (curr?.data == data) {
head = curr?.next;
size--;
return;
} else {
while (curr.next.next != null) {
if (curr.next.data == data) {
curr.next = curr.next.next;
while (curr?.next?.next != null) {
if (curr?.next?.data == data) {
curr?.next = curr.next?.next;
return;
}
}
Expand All @@ -56,7 +55,7 @@ class LinkedList {
}

void display() {
Node temp = head;
Node? temp = head;
while (temp != null) {
print(temp.data.toString());
temp = temp.next;
Expand All @@ -69,8 +68,8 @@ class HashMap {
int hsize;
List<LinkedList> buckets;

HashMap(int hsize) {
buckets = new List<LinkedList>(hsize);
HashMap({this.hsize = 0, this.buckets = const []}) {
buckets = new List.generate(hsize, (a) => LinkedList());
for (int i = 0; i < hsize; i++) {
buckets[i] = new LinkedList();
}
Expand Down Expand Up @@ -104,7 +103,7 @@ class HashMap {
}

void main() {
HashMap h = new HashMap(7);
HashMap h = new HashMap(hsize: 7);

print("Add key 5");
h.insertHash(5);
Expand Down
6 changes: 3 additions & 3 deletions data_structures/Heap/Binary_Heap/Max_heap.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:test/test.dart';

class MaxHeap {
List<int> heap;
List<int> heap = [];

void buildHeap(List<int> array) {
this.heap = _heapify(array);
Expand All @@ -15,7 +15,7 @@ class MaxHeap {
return array;
}

int peek() {
int? peek() {
if (!isEmpty()) {
return this.heap[0];
}
Expand Down Expand Up @@ -65,7 +65,7 @@ class MaxHeap {
_siftUp(this.heap.length - 1);
}

int remove() {
int? remove() {
if (!isEmpty()) {
_swap(0, this.heap.length - 1, this.heap);
int maxElement = this.heap.removeLast();
Expand Down
6 changes: 3 additions & 3 deletions data_structures/Heap/Binary_Heap/min_heap_two.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:test/test.dart';

class MinHeap {
List<int> heap;
List<int> heap = [];

void buildHeap(List<int> array) {
this.heap = _heapify(array);
Expand All @@ -15,7 +15,7 @@ class MinHeap {
return array;
}

int peek() {
int? peek() {
if (!isEmpty()) {
return this.heap[0];
}
Expand Down Expand Up @@ -65,7 +65,7 @@ class MinHeap {
_siftUp(this.heap.length - 1);
}

int remove() {
int? remove() {
if (!isEmpty()) {
_swap(0, this.heap.length - 1, this.heap);
int minElement = this.heap.removeLast();
Expand Down
Loading
Loading