链表头不是指向头但是结束

时间:2017-05-16 08:45:01

标签: c linked-list singly-linked-list

我的链表头应该在迭代的每个链表中显示第一个条目。但它总是显示链表中的最后一个元素,我无法弄清楚为什么,我在没有其他链接列表元素的情况下分配到头部,所以它不应该保持这种状态吗?

为什么我的头只显示每个迭代的最后一个链接元素?我怀疑它可能与地址指向有关,但我认为这些东西是这样做的

 typedef struct record_s {
        int id;
        char number[NAMELEN];
        struct record_s *next;
    } Record;

typedef struct person_s {
    int id;
    char name[NAMELEN];
    double expenditure;
    Record *numbers;
} Person;

typedef struct people_s {
    Person data[MAXRECORD];
    int size;
} People;
int main(int argc,char **argv){
    FILE *fp=fopen(argv[1],"r");
    int i,j,z=0;
    int o;
    int x;
    int ztemp=0;
    char str[NAMELEN];
    char a[NAMELEN];
    char b[NAMELEN];
    char numbers[NAMELEN][NAMELEN];
    People people1;           
    Record *current,*ptr,*head=NULL,*test;
    int bosluk=0;
    char *token;
    int satir=satir_sayisi(argv[1]);
    for(i=0;i<satir+1;i++){
        current=(Record*)malloc(sizeof(Record));
        people1.data[i].numbers=current;                                            
        head=people1.data[i].numbers;

        fscanf(fp,"%d%s%s%lf",&people1.data[i].id,a,b,&people1.data[i].expenditure);
        fgets (str, 60, fp);
        token=strtok(str," ");
        ztemp=0;
        while (token != NULL)
        {
            ptr=(Record*)malloc(sizeof(Record));
            strcpy(numbers[z],token);
            strcpy(current->number,numbers[z]);
            current->id=people1.data[i].id;


            token = strtok (NULL, " ,\n");
            current->next=ptr;
            ptr=current;
            z++;
            ztemp++;
        }
        current->next=NULL;
        people1.data[i].numbers=head;
        test=head;
        while(test!=NULL){
            printf("%d is id, %s is supposedly the number\n",test->id,test->number);
            test=test->next;
        }

        strcat(a," ");
        strcat(a,b);
        strcpy(people1.data[i].name,a);


    }
    for(o=0;o<satir+1;o++){
        x=0;
        x=strcspn(numbers[o],"\n");
        if(x!=0)
            numbers[i][o]='\0';
    }
    people1.size=i;
    fclose(fp);
    return 0;
}

0 个答案:

没有答案