I was going to write a simple SDL app so I thought I should implement a simple linked list that I can extend later. Now after some struggling with it I decided to ask for help here.
I know the linked list, I read about it. I saw couple implementations, not so hard, easy to grasp. So I thought I should be able to implement one myself. I am guessing that I am severely overlooking something here. 🙁
First this code does not compile well, segmentation fault. Second I do not think that it is going to produce what I want if I can overcome the basic errors I am doing with the pointers unfortunately.
Can you please show me my mistakes in my code? Where am I doing wrong?
Here is a liveworkspace link for editing
http://liveworkspace.org/code/1qu1RN$1
Here is the error I get on Cygwin gcc
temp [v 0] -> 0.460241 0.000000 0.050356
temp [v 1] -> 0.072079 0.153807 0.864573
temp [v 2] -> 0.683917 0.709301 0.080479
temp [v 3] -> 0.295755 0.264795 0.894696
address of ptemp @ 1629101750
[v 0] -> linked vertex is 7731159828445146316135105405324534341657178000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000 578395405356271.125000 1310047834864691498231654014730274851713450000.000000
Segmentation fault (core dumped)
And here is the code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NEWline printf("\n")
typedef struct position {
double x;
double y;
double z;
} position;
struct datalist {
position v;
struct datalist *pnext;
};
typedef struct datalist datalist;
void initializeList (datalist *head);
void addList_II (datalist *head , double data[3] );
void initializeList (datalist *head){
position pv= {0,0,0};
head = (datalist *) malloc ( sizeof (head) );
if (head == NULL){ printf("cannot allocate memory"); exit(1); }
head->v=pv;
head->pnext=NULL;
}
void addList_II ( datalist *head , double data[3] ) {
datalist *adddata;
adddata = (datalist *)malloc ( sizeof (datalist) );
if (adddata == NULL) { printf("cannot allocate memory for insertion data"); exit(1); }
adddata->v.x = data[0];
adddata->v.y = data[1];
adddata->v.z = data[2];
adddata->pnext = head->pnext;
head=adddata;
}
int main() {
datalist *ptemp;
datalist temp;
int i,c; //counters
datalist *head;
initializeList(head);
for ( i = 0; i < 4; i++ ) {
double t[3];
srand ( time ( NULL )*i+234 );
temp.v.x = ((double ) rand() )/ RAND_MAX;
srand ( time ( NULL )*i*546 );
temp.v.y = ((double ) rand() )/ RAND_MAX;
srand ( time ( NULL )*i*567+345 );
temp.v.z = ((double ) rand() )/ RAND_MAX;
t[0] = temp.v.x; t[1] = temp.v.y; t[2] = temp.v.z;
printf ( "temp [v %d] -> %F %F %F \n",i, temp.v.x, temp.v.y, temp.v.z );
addList_II (head, t );
}
c=0;
ptemp=head;
while (ptemp!=NULL) {
printf("[v %d] -> linked vertex is %F %F %F \n", c, ptemp->v.x,ptemp->v.y, ptemp->v.z);
ptemp=ptemp->pnext;
}
}
In your
addList_IIyou are setting the wrong pointer. You are taking your new node and pointing its next pointer to the head’s next pointer, which is null.Additionally your pointers were going out of scope in your function calls. what you need to do is pass in ** variables. This means your passing in a pointer to a pointer, which points to your block of memory. You modify the original pointer by derefrencing your local *, and upon return you are good.
If you need more explanation, just ask, but heres the solution