Skip to content
Snippets Groups Projects
Commit 2b410b17 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Reorganize List-to-array conversion

parent ca120efc
No related branches found
No related tags found
No related merge requests found
...@@ -41,19 +41,14 @@ template<class T> class List { ...@@ -41,19 +41,14 @@ template<class T> class List {
* *
* b = List<int>(); * b = List<int>();
*/ */
List(int size = 1) { List(int length = 1) {
this->size = size; size = length;
element* p_prev = NULL; first = new element;
T value = (T)0; first->p_prev = NULL;
current = new element; element *current = first;
current->value = value; element *p_prev = first;
current->p_prev = p_prev;
p_prev = current;
first = current;
for (int i = 1; i < size; i++) { for (int i = 1; i < size; i++) {
T value;
current = new element; current = new element;
current->value = value;
current->p_prev = p_prev; current->p_prev = p_prev;
p_prev = current; p_prev = current;
} }
...@@ -107,6 +102,10 @@ template<class T> class List { ...@@ -107,6 +102,10 @@ template<class T> class List {
return current->value; return current->value;
} }
int length() {
return size;
}
/*! \fn set(int, T) /*! \fn set(int, T)
* \brief Set an element by index and value. * \brief Set an element by index and value.
* *
...@@ -141,8 +140,8 @@ template<class T> class List { ...@@ -141,8 +140,8 @@ template<class T> class List {
T* to_array() { T* to_array() {
T *result = new T[size]; T *result = new T[size];
current = last; current = last;
for (int i = size - 1; i > 0; i--) { for (int i = size; i > 0; i--) {
result[i] = current->value; result[i - 1] = current->value;
current = current->p_prev; current = current->p_prev;
} }
return result; return result;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment