2017-11-27 6 views
0

링크 된 목록에 포함 된 연결된 목록의 정보를 추가/업데이트하려고하는 초보자입니다. 내 프로그램은 간단한 연락처 목록입니다. 여기서 사용자는 연락처를 연락처에 추가 한 다음 각 연락처에 정보를 추가 할 수 있습니다 (별도의 정보 목록 사용). 내 addInformation 함수로 세분화 오류가 발생하며 문제가 무엇인지 알 수 없습니다. 사람이 내 코드에서 잘못된 뭘하는지 말해 줄 수 있다면다른 연결된 목록 (C 프로그래밍)에 포함 된 연결된 목록의 정보 추가/업데이트 (C 프로그래밍)

void addInformation(Contact *myContacts, char *contactName, char *infoName, char *infoValue) { 
Contact *ptr = myContacts; 
Info *ptr2 = ptr->information; 

if (ptr == NULL) { 
    printf("Error: No contacts added.\n"); 
    return; 
} 

while (ptr != NULL) { 
    if (ptr->name != contactName) { 
    printf("Error: Contact does not exist\n"); 
    return; 
    } 
    else { 
     ptr2->name = infoName; 
     ptr2->value = infoValue; 
     ptr2->next = NULL; 
     ptr->information = ptr2; 
    } 
    ptr = ptr->next; 
    } 

return; 
} 

가이 많이 주시면 감사하겠습니다 : 내 addInformation 기능이 여기

typedef struct info { 
    char *name; 
    char *value; 
    struct info *next; 
} Info; 

typedef struct contact { 
    char *name; 
    Info *information; 
    struct contact *next; 
} Contact; 

을 그리고 다음은 구조를 정의하는 방법입니다! 감사.

+0

일부 추상화를 목록에 추가하십시오. 부디. –

+0

@Eugene Sh .: 그렇습니다. 기본 아이디어는 연락처 목록이 있다는 것입니다. 각 연락처에는 해당 연락처와 관련된 이름 및 정보 목록이 있습니다. 각 정보에는 정보의 이름과 값 및 해당 연락처에 대한 다음 정보 항목에 대한 포인터가 들어 있습니다. add-at-front 알고리즘을 사용하여 연락처 목록에 연락처를 추가하고 추가 정보를 사용하여 연락처 정보 목록에 정보 (핸드폰 번호, 전자 메일, 트위터 핸들 등)를 추가하려고합니다. at-end 알고리즘. –

+0

간단하고 반복 가능한 (편집 가능한) 예를 제공해주십시오. – schorsch312

답변

0

내 첫 번째 반응은 Contact-> Information의 메모리가 할당되지 않았다는 것입니다. 따라서 ptr2는 NULL이거나 가비지입니다. @yano는 이미 그것을 언급했다.

원래 코드에 주석을 쓰려고했는데 몇 가지 의견이있는 새 버전을 작성하는 것이 더 명확해질 것입니다. 코드를 여러 함수로 분리하는 것이 더 나을 것입니다.

void addInformation(Contact *myContacts, char *contactName, char *infoName, char *infoValue) { 
    Contact *ptr = myContacts; 

    if (ptr == NULL) { 
     printf("Error: bad pointer to contacts storage.\n"); 
     return; 
    } 

    while (ptr != NULL) { 
     if (strcmp(ptr->name,contactName) == 0) { 
      // if it's a contact with the name we are looking for 
      // then allocate and put info structure as a head of the list of info 
      Info *newInfo = malloc(sizeof(Info)); 
      newInfo->name = infoName; 
      newInfo->value = infoValue; 
      newInfo->next = ptr->information; 
      ptr->information = newInfo; 
      return; // get out as we found our contact 
     } else if (ptr->next == NULL) { 
      // there is nothing next, we are at the end of the list 
      // allocate memory for both contact and information, fill it in 
      // add the contact as the last element in the list of contacts. 
      Contact* newContact = malloc(sizeof(Contact)); 
      newContact->name = contactName; 
      newContact->information = malloc(sizeof(Info)); 
      newContact->information->name = infoName; 
      newContact->information->value = infoValue; 
      newContact->information->next = NULL; 
      newContact->next = NULL; 
      ptr->next = newContact; 
      return; // get out as there is no reason to iterate more, it's the last element in the list 
     } else { 
      ptr = ptr->next; 
     } 
    } 


    return; 
} 

int main(int argc, const char * argv[]) { 
    // Has to allocate the first contact. Otherwise the pointer management 
    // will have to be different and addInformation function has to accept 
    // pointer to pointer. 
    Info info = { "nn", "vv", NULL }; 
    Contact myContacts = { "contact 1", &info, NULL }; 

    addInformation(&myContacts, "contact 2", "info 1", "val 2"); 
    addInformation(&myContacts, "contact 2", "info 3", "val 4"); 
    addInformation(&myContacts, "contact 1", "info 5", "val 6"); 

    printf("end"); 
    return 0; 
}