Arraylist implementation in C
<p>My first language is Java, now I am trying to implement Arraylist using C language for all data types, using void*. Here is my code.</p>

<p>The usage.</p>

<pre><code>#include "pk_arraylist_int.h"

#include &lt;stdio.h&gt;

int main() {
    Arraylist myArray = newArraylist();
    list_int_add(&amp;myArray, 0, 10);
    list_int_add(&amp;myArray, 0, 20);
    list_int_add(&amp;myArray, 0, 30);
    int i;
    for (i = 0; i != list_size(&amp;myArray); i++) {
        printf("[%d] -&gt; %d\n", i, list_int_get(&amp;myArray, i));
    }
    return 0;
}

int list_int_get(Arraylist* list, int index) {
    Node* reader = list_get(list, index);
    return (int)(int*)(reader-&gt;value);
}

void list_int_set(Arraylist* list, int index, int value) {
    int* pointer = &amp;value;
    list_set(list, index, pointer);
}

void list_int_add(Arraylist* list, int index, int value) {
    int* pointer = malloc(sizeof(int));
    pointer = &amp;value;
    list_add(list, index, pointer);
}
</code></pre>

<p>and here is the arraylist.c</p>

<pre><code>#include "pk_arraylist.h"

Arraylist newArraylist() {
    Arraylist temp;
    temp.root = NULL;
    return temp;
}

Node* _element(Arraylist* list, int index) {
    Node* reader = list-&gt;root;
    int i;
    for (i = 0; i != index; i++) {
        reader = reader-&gt;next;
    }
    return reader;
}

int list_size(Arraylist* list) {
    int size = 0;
    Node* reader = list-&gt;root;
    while (reader != NULL) {
        reader = reader-&gt;next;
        size++;
    }
    return size;
}

bool list_isEmpty(Arraylist* list) {
    if (list-&gt;root == NULL) {
        return true;
    } else {
        return false;
    }
}

void* list_get(Arraylist* list, int index) {
    Node* reader = _element(list, index);
    return reader-&gt;value;
}

void list_set(Arraylist* list, int index, void* value) {
    Node* reader = _element(list, index);
    reader-&gt;value = value;
}

void list_add(Arraylist* list, int index, void* value) {
    int size = list_size(list);
    Node* newNode = malloc(sizeof(Node));
    newNode-&gt;value = value;
    newNode-&gt;next = NULL;
    if (index &lt; 0) {
        return;
    } else if (size == 0) {
        list-&gt;root = newNode;
    } else if (index == 0) {
        newNode-&gt;next = list-&gt;root;
        list-&gt;root = newNode;
    } else if (index &gt;= size) {
        Node* left = _element(list, index - 1);
        left-&gt;next = newNode;
    } else {
        Node* left = _element(list, index - 1);
        Node* right = _element(list, index + 1);
        left-&gt;next = newNode;
        newNode-&gt;next = right;
    }
}

void list_remove(Arraylist* list, int index) {
    int size = list_size(list);
    if (index == 0) {
        Node* garbage = list-&gt;root;
        Node* right = garbage-&gt;next;
        free(garbage);
        list-&gt;root = right;
    } else if (index == size - 1) {
        Node* left = _element(list, index - 1);
        Node* garbage = left-&gt;next;
        free(garbage);
        left-&gt;next = NULL;
    } else if (0 &lt; index &amp;&amp; index &lt; size - 1) {
        Node* left = _element(list, index - 1);
        Node* garbage = left-&gt;next;
        Node* right = garbage-&gt;next;
        free(garbage);
        left-&gt;next = right;
    }
}

void list_clear(Arraylist* list) {
    while (list_size(list) != 0) {
        list_remove(list, 0);
    }
}
</code></pre>

<p>and also the arraylist.h</p>

<pre><code>#ifndef _PK_ARRAYLIST
#define _PK_ARRAYLIST

#include &lt;stdlib.h&gt;
#include &lt;stdbool.h&gt;

struct Node {
    void* value;
    struct Node* next;
};
typedef struct Node Node;

struct Arraylist {
    Node* root;
};
typedef struct Arraylist Arraylist;
Arraylist newArraylist();

Node* _element(Arraylist* list, int index);

int list_size(Arraylist* list);
bool list_isEmpty(Arraylist* list);

void* list_get(Arraylist* list, int index);
void list_set(Arraylist* list, int index, void* value);
void list_add(Arraylist* list, int index, void* value);

void list_remove(Arraylist* list, int index);
void list_clear(Arraylist* list);

#endif
</code></pre>

<p>I was expecting the array to be 30, 20, 10. However outputting the array list I saw 30, 30, 30.</p>

<p>I think the same memory address is being used each time. I suspect that the three elements of the linked list all store the same address.</p>

<p>Problem is how do I solve it? Is there a way for the program to allocate new memory blocks for new data? instead of overwriting the same memory block every time?</p>

<p>I am new to C, any help appreciated thank you.</p>