samedi 25 avril 2015

array to create a linked-list


typedef struct num{
    int num;
    int pre;
    struct num* next;
}Num;

Num list[10]=
{{3,4},{2,1},{6,5},{7,2},{4,3},{3,9},{5,6},{1,3},{8,4},{10,0}
};

#include <stdio.h>

int main(){

    int cnt;
    Num *ptr = NULL;

    Num tempTwo;
    for (cnt = 0; cnt < 10; cnt++) {
        tempTwo = list[cnt]; 
        ptr->next = &tempTwo; //Error
        ptr = ptr->next;
    }

    for (cnt = 0; cnt<10; cnt++) {
        printf("num: %d, pre: %d\n",ptr->num,ptr->pre);
        ptr = ptr->next;
    }
}

I want to make linked-list with array 'list' using pointer ptr.
Error: Bad access
What can I do to solve this problem?


Aucun commentaire:

Enregistrer un commentaire