diff --git a/src/main/cpp/algorithms/datastructures/linkedlist/DoublyLinkedList.h b/src/main/cpp/algorithms/datastructures/linkedlist/DoublyLinkedList.h index 66414c5..b2fc545 100644 --- a/src/main/cpp/algorithms/datastructures/linkedlist/DoublyLinkedList.h +++ b/src/main/cpp/algorithms/datastructures/linkedlist/DoublyLinkedList.h @@ -194,7 +194,7 @@ class DoublyLinkedList // Add an element at a specified index void addAt(int index, const T &data) { - if (index < 0) { + if (index < 0 || index > size_) { throw ("Illegal Index"); } if (index == 0) { diff --git a/src/main/python/algorithms/datastructures/linkedlist/DoublyLinkedList.py b/src/main/python/algorithms/datastructures/linkedlist/DoublyLinkedList.py index 4435927..78a5748 100644 --- a/src/main/python/algorithms/datastructures/linkedlist/DoublyLinkedList.py +++ b/src/main/python/algorithms/datastructures/linkedlist/DoublyLinkedList.py @@ -110,8 +110,8 @@ def addAt(self, index, data): """ Add an element at a specified index """ - if index < 0: - raise Exception('index should not be negative. The value of index was: {}'.format(index)) + if index < 0 or index > self.llSize: + raise Exception('Illegal index. The value of index was: {}'.format(index)) if index == 0: self.addFirst(data)