2013-09-24 3 views
1

파일의 정보를 Car struct "newcar"로 읽는 중입니다. 아래의 "sll_add"함수를 사용하여 "list"라는 Carlist i 유형의 단일 좋아하는 목록에 정보를 추가해야합니다. 나는 그것이 어떻게 시작되는지 이해하는 데 어려움을 겪고 있습니다. 어떤 도움을 주셔서 감사합니다.단일 링크 된 목록에 개체를 추가하는 방법

int main (void)//main function 
{ 

FILE *fp;//file pointer 
fp = fopen("car_inventory.txt", "r");//opens the car inventory data within the program. 
int num=0;//vairable created to keep track of the information. 
int year;//, choice;//variables use for user input. 
char str1[100];//string created to ensure the program reads the information from the original file correctly. 
Carlist list;//creates a string of type "Car" named file to hold up to 100 cars. 
Car newcar;//Car variable to store the info from the file. 

list *last=num; 

if (fp) 
    { 
     {     
     while(!feof(fp))//takes input from the data file. 
      { 
     fgets(str1,50, fp); 
     sscanf(str1,"%d %s %[^\n]s", &newcar.year, newcar.make, newcar.model); 

     fgets(str1,50, fp); 
     sscanf(str1,"%[^\n]s", newcar.style); 

     fgets(str1,50, fp); 
     sscanf(str1,"%[^\n]s", newcar.color); 

     fgets(str1,50, fp); 
     sscanf(str1,"%s", newcar.mileage); 

     fgets(str1,50, fp); 
     sscanf(str1,"%c", &newcar.air_condition); 

     fgets(str1,50, fp); 
     sscanf(str1,"%s", newcar.inventory_num); 

     fgets(str1,50, fp); 
     sscanf(str1,"%lf", &(newcar).price); 

     fgets(str1,50, fp); 
     sscanf(str1,"%[^\n]s", newcar.previous_owner); 

     fgets(str1,50,fp); 


     num++; 
     sll_add(*newcar, &list); 
      } 
     } 

return 0; 


#define MAX_CARS  100 

/* Type definitions */ 

typedef struct car 
{ 
    int year; 
    char make[25]; 
    char model[25]; 
    char style[25]; 
    char color[20]; 
    char mileage[8]; 
    char air_condition; 
    char inventory_num[16]; 
    double price; 
    char previous_owner[30]; 
    struct car *next; 

} Car; 


typedef struct carlist 
{ 
    Car *first; 
    Car *last; 
} Carlist; 


void sll_init(Carlist *l); 
Car *sll_first(Carlist *l); 
Car *sll_end(Carlist *l); 
Car *sll_next(Car *current, Carlist *l); 
Car *sll_retrieve(Car *element, Carlist *l); 
void sll_delete(Car *c, Carlist *l); 
void sll_delete_year(Carlist *l, int year); 
void sll_add(Car *newcar, Carlist *l); 


    #endif 

답변

1

처음부터 시작하십시오. 먼저 Carlist를 초기화해야하므로 sll_init() 기능을 사용해야합니다. 이 작업은 포인터가 null 일 때 firstlast 포인터로 설정해야하기 때문에 매우 쉽습니다. 그것은 다음과 같이 보일 수 있습니다 당신이 sll_add()을 구체화 할 필요가 있으므로

void sll_init(Carlist *l) 
{ 
    l->first = l->last = NULL; 
} 

다음, 당신은, 당신의 목록에 항목을 추가 할 수 있어야합니다. 다음과 같이 표시 될 수 있습니다.

void sll_add(Car *newcar, Carlist *l) 
{ 
    Car *new = malloc(sizeof(*new)); 
    *new = *newcar; 
    new->next = NULL; // just to be safe in case newcar wasn't properly initialized 

    if (l->first == NULL) 
    { 
     // the list is empty, so add the first entry 
     l->first = l->last = new; 
    } 
    else 
    { 
     // the list is not empty, so append a new entry to the end 
     l->last->next = new; 
     l->last = new; 
    } 
} 

이렇게하면 시작할 수 있습니다.

+0

도움 주셔서 감사합니다. – user2812294

관련 문제