@@ -18,6 +18,13 @@ class DSDoublyLL{
1818 */
1919 DSDoublyLL (const T* data, const int size);
2020
21+ /* *
22+ * @brief DSDoublyLL - copy constructor
23+ * @param other - reference of list to copy
24+ */
25+ DSDoublyLL (const DSDoublyLL<T>& other);
26+
27+
2128 /* *
2229 * @brief getNumIndexes - Getter: numIndexes
2330 * @return numIndexes
@@ -146,6 +153,17 @@ DSDoublyLL<T>::DSDoublyLL(const T* data, const int size){
146153 }
147154}
148155
156+ /* *
157+ * @brief DSDoublyLL - copy constructor
158+ * @param other - reference to the list to copy
159+ */
160+ template <class T >
161+ DSDoublyLL<T>::DSDoublyLL(const DSDoublyLL& other){
162+ for (DSNode<T>* n = other.head ; n != nullptr ; n = n->next ){
163+ pushBack (n->data );
164+ }
165+ }
166+
149167
150168/* *
151169 * @brief getNumIndexes - Getter: numIndexes
@@ -214,21 +232,22 @@ void DSDoublyLL<T>::insert(int index, T data){
214232
215233 // ToDo: throw an "index out of bounds" error
216234 DSNode<T>* current = head;
217- for (int count = 0 ; count < index - 1 ; current = current-> next ){
218- count++ ;
235+ for (int i = 0 ; i < index - 1 ; i++ ){
236+ current = current-> next ;
219237 }
220238
221- if (current->next == nullptr ){
222- tail = newNode;
223- current->next = newNode;
224- newNode->prev = current;
225- } else {
226- current->next ->prev = newNode;
227- newNode->next = current->next ;
239+ newNode->next = current->next ;
240+ newNode->prev = current;
228241
229- current->next = newNode;
230- newNode->prev = current;
242+ current->next = newNode;
243+
244+ if (newNode->next == nullptr ){
245+ tail = newNode;
246+ }else {
247+ newNode->next ->prev = newNode;
231248 }
249+
250+ numIndexes++;
232251}
233252
234253/* *
0 commit comments