I am a new to C and currently trying to learn how to build a iterator for C. I set myself this question:
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:
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
This is the program that I built:
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->first = NULL;
q->last = NULL;
q->count = 0L;
}
return q;
}
int Enqueue(Queue* q, Data d){
Node* n = (Node*)malloc(sizeof(Node));
if(!n)
return 0;
n->next = NULL;
n->d = d;
if(q->count++ == 0){
q->first = n;
}
else{
q->last->next = n;
}
q->last = n;
return 1;
}
Data Dequeue(Queue* q){
Node* n;
Data d;
if(q->count == 0)
return d;
n = q->first;
d= n->d;
q->first = n->next;
free(n);
q->count--;
q->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<5; i++){
d = Dequeue(q);
printf("Name:%s, Labs:%d, Classrooms:%d\n",d.name, d.labroom, d.classroom);
}
return 1;
}
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:
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->next = 0L;
it->size = size;
it->elements = elements;
}
return it;
}
int it_hasNext(Iterator* it){
return((it->next<it->size)? 1: 0);
}
void* it_Next(Iterator* it, void* elements){
if(it->next < it->size){
elements = it->elements[it->next++];
}
return elements;
}
void it_Destroy(Iterator* it){
free(it->elements);
free(it);
}
I used this code in main to display the information retrieved.
Data* de;
Iterator* it = Iter_Create(q, q->count);
while(it_hasNext(it) == 1){
de = it_Next(it,q);
printf("Name:%s, Labs:%d, Classrooms:%d\n",de->name, de->labroom, de->classroom);
}
it_Destroy(it);
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:
Name:RP-SIT, Labs:4, Classrooms:20
Name:NUS-Yale, Labs:50, Classrooms:60
Segmentation fault: 11
Sorry for the long question but I wanted to be as clear as I can be. Thanks for the help!