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 {
*
* b = List<int>();
*/
List(int size = 1) {
this->size = size;
element* p_prev = NULL;
T value = (T)0;
current = new element;
current->value = value;
current->p_prev = p_prev;
p_prev = current;
first = current;
List(int length = 1) {
size = length;
first = new element;
first->p_prev = NULL;
element *current = first;
element *p_prev = first;
for (int i = 1; i < size; i++) {
T value;
current = new element;
current->value = value;
current->p_prev = p_prev;
p_prev = current;
}
......@@ -107,6 +102,10 @@ template<class T> class List {
return current->value;
}
int length() {
return size;
}
/*! \fn set(int, T)
* \brief Set an element by index and value.
*
......@@ -141,8 +140,8 @@ template<class T> class List {
T* to_array() {
T *result = new T[size];
current = last;
for (int i = size - 1; i > 0; i--) {
result[i] = current->value;
for (int i = size; i > 0; i--) {
result[i - 1] = current->value;
current = current->p_prev;
}
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