Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   A C language problem in link list traversal (http://www.go4expert.com/forums/c-language-link-list-traversal-t16805/)

Austin Way 5Apr2009 15:16

A C language problem in link list traversal
 
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 >(_ _)<

xpi0t0s 5Apr2009 16:23

Re: A C language problem in link list traversal
 
How is rec defined?

Austin Way 5Apr2009 21:26

Re: A C language problem in link list traversal
 
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;

Austin Way 5Apr2009 21:40

Re: A C language problem in link list traversal
 
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;
}


xpi0t0s 5Apr2009 22:58

Re: A C language problem in link list traversal
 
You haven't set p to point to memory that contains the rec structures in which to place the values.


All times are GMT +5.5. The time now is 04:10.