1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A C language problem in link list traversal

Discussion in 'C' started by Austin Way, Apr 5, 2009.

  1. Austin Way

    Austin Way New Member

    Joined:
    Apr 5, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi guys,

    I've difficulties in developing a new link list by a loop


    First of all, i let the user enter people's number: Numbger

    And create an empty link list

    And then read data to link list by for loop.
    rec *f; //by using another pointer
    f=p; //f indicates to the node which p indicates to
    printf("OK1\n");//for my test
    for(i=0;i<=Number-1;i++){
    fscanf(fp1,"%s %s %s",&(f->Firstname),&(f->Lastname),&(f->Phonenumber));
    f=f->Link;
    }
    //在嘗試用回圈 寫入number比資料入link list時發生了問題
    //while I 'm using a loop to enter messages to nodes ,
    //errors occur in the statement f=f->Link;
    //But WHY?><"
    //If i can'g use a loop , then what should i do ?
    printf("OK2\n");

    printf("%s\n",f->Firstname);
    printf("%s\n",f->Lastname);
    printf("%s\n",f->Phonenumber);
    printf("%s\n",f->Link->Firstname);
    printf("%s\n",f->Link->Lastname);
    printf("%s\n",f->Link->Phonenumber);//for testing the date be written or not?


    printf("OK3\n");



    Thank you for your help >(_ _)<
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    How is rec defined?
     
  3. Austin Way

    Austin Way New Member

    Joined:
    Apr 5, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    typedef struct people{
    char Firstname[10];
    char Lastname[10];
    char Phonenumber[10];
    int Seq_num;
    //the larger number in Seq_num stands for the smaller number in value.
    struct people *Link;
    } rec;
     
  4. Austin Way

    Austin Way New Member

    Joined:
    Apr 5, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *fp1;
    int main()
    {
    typedef struct people{
    char Firstname[10];
    char Lastname[10];
    char Phonenumber[10];
    int  Seq_num;
    
    struct people *Link;
    } rec;
    
    
    int Number;
    int count;
    int i;
    rec *p;
    
    printf("Please enter the source of the first matrix\n");
    
    scanf("%s",matrix1_name);
    
    fp1=fopen(matrix1_name,"r");
    if(fp1==NULL)
    printf("some error has occured ,but don't worry and you'll work it out");
    else
    printf("writing\n");
      
    printf("Please tell me how much people's information in the file? \n");
    scanf("%d", &Number);
      
    
    p=(rec *)malloc(Number*sizeof(rec));//根據link01.c example知 ()內就是P的宣告
    
      
    
    
    
    rec *f;
    f=p;
    
    
    printf("OK1\n");
    for(i=0;i<=Number-1;i++){
    fscanf(fp1,"%s %s %s",&(f->Firstname),&(f->Lastname),&(f->Phonenumber));
    f=f->Link;
    }
    
    printf("OK2\n");
    
    printf("%s\n",f->Firstname);
    printf("%s\n",f->Lastname);
    printf("%s\n",f->Phonenumber);
    printf("%s\n",f->Link->Firstname);
    printf("%s\n",f->Link->Lastname);
    printf("%s\n",f->Link->Phonenumber);
    
    
    printf("OK3\n");
    /*while(f->Link!=NULL){
    fscanf(fp1,"%s",&(f->Firstname));
    fscanf(fp1,"%s",&(f->Lastname));
    fscanf(fp1,"%s",&(f->Phonenumber));
    
    }
    
    
    
    printf("%s",f->Firstname);
    
    /*count=0;
    while(count!=Number-1){
    f=f->Link;
    count++;
    }
    f->Link=NULL;
    
    
    
    
    fclose(fp1);
    return 0;
    }
     
    Last edited by a moderator: Apr 5, 2009
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You haven't set p to point to memory that contains the rec structures in which to place the values.
     

Share This Page