Iterator in C for linked list
<p>I am a new to C and currently trying to learn how to build a iterator for C. I set myself this question:</p>

<blockquote>
  <p>Create a queue program that stores a variable Data in it. Data contains char* name, int classroom, int labroom. Load the following data into the program:</p>
</blockquote>

<pre><code>Name:RP-SIT, Labs:4, Classrooms:20
Name:NP-SIT, Labs:6, Classrooms:30
Name:NYP-SIT, Labs:12, Classrooms:60
Name:SP-SIT, Labs:4, Classrooms:12
Name:NUS-Yale, Labs:50, Classrooms:60
</code></pre>

<p>This is the program that I built:</p>

<pre><code>typedef struct data{
        char* name;
        int classroom;
        int labroom;
    }Data;

    typedef struct queue_element{
        Data d;
        struct queue_element* next;
    }Node;

    typedef struct queue{
        Node* first;
        Node* last;
        int count;
    }Queue;
Queue* Create(){
    Queue* q = (Queue*)malloc(sizeof(Queue));

    if(q){
        q-&gt;first = NULL;
        q-&gt;last = NULL;
        q-&gt;count = 0L;
    }

    return q;
}

int Enqueue(Queue* q, Data d){
    Node* n = (Node*)malloc(sizeof(Node));
    if(!n)
        return 0;
    n-&gt;next = NULL;
    n-&gt;d = d;

    if(q-&gt;count++ == 0){
        q-&gt;first = n;
    }

    else{
        q-&gt;last-&gt;next = n;
    }
    q-&gt;last = n;
    return 1;
}

Data Dequeue(Queue* q){
    Node* n;
    Data d;

    if(q-&gt;count == 0)
        return d;

    n = q-&gt;first;
    d= n-&gt;d;

    q-&gt;first = n-&gt;next;
    free(n);
    q-&gt;count--;
    q-&gt;last = NULL;

    return d;
}

int main(int argc, char* argv[]){
    Queue* q;
    Data d1 = {"RP-SIT", 20, 4},d2 = {"NP-SIT", 30, 6}, d3 = {"NYP-SIT",60,12}, d4={"SP-SIT", 12, 4}, d5 = {"NUS-Yale", 60, 50};


    if((q=Create()) == NULL){
        printf("Error creating Queue");
        exit(1);
    }

    if(!Enqueue(q,d1)){
        printf("Error enqueuing data");
        exit(1);
    }
    if(!Enqueue(q,d2)){
        printf("Error enqueuing data");
        exit(1);
    }
    if(!Enqueue(q,d3)){
        printf("Error enqueuing data");
        exit(1);
    }
    if(!Enqueue(q,d4)){
        printf("Error enqueuing data");
        exit(1);
    }
    if(!Enqueue(q,d5)){
        printf("Error enqueuing data");
        exit(1);
    }
    Data d;
    for(int i = 0; i&lt;5; i++){
        d = Dequeue(q);
        printf("Name:%s, Labs:%d, Classrooms:%d\n",d.name, d.labroom, d.classroom);
    }
    return 1;
}
</code></pre>

<p>I am trying to add in a function that makes use of a Iterator to display the information without popping the information out of the Queue. This is my iterator code: </p>

<pre><code>typedef struct iterator{
    long next;
    long size;
    void* *elements;
}Iterator;

Iterator* Iter_Create(void* *elements, long size){
    Iterator* it = (Iterator*)malloc(sizeof(Iterator));

    if(it){
        it-&gt;next = 0L;
        it-&gt;size = size;
        it-&gt;elements = elements;
    }
    return it;
}

int it_hasNext(Iterator* it){
    return((it-&gt;next&lt;it-&gt;size)? 1: 0);
}

void* it_Next(Iterator* it, void* elements){
    if(it-&gt;next &lt; it-&gt;size){
        elements = it-&gt;elements[it-&gt;next++];
    }

    return elements;
}

void it_Destroy(Iterator* it){
    free(it-&gt;elements);
    free(it);
}
</code></pre>

<p>I used this code in main to display the information retrieved.</p>

<pre><code>Data* de;
Iterator* it = Iter_Create(q, q-&gt;count);

while(it_hasNext(it) == 1){
    de = it_Next(it,q);
    printf("Name:%s, Labs:%d, Classrooms:%d\n",de-&gt;name, de-&gt;labroom, de-&gt;classroom);
}

it_Destroy(it);
</code></pre>

<p>This is where I need all your help. I can't figure out why I am getting this segmentation fault when displaying the data. This is my output with the iterator code added:</p>

<pre><code>Name:RP-SIT, Labs:4, Classrooms:20
Name:NUS-Yale, Labs:50, Classrooms:60
Segmentation fault: 11
</code></pre>

<p>Sorry for the long question but I wanted to be as clear as I can be. Thanks for the help!</p>