2014-12-12 2 views
0

연결된 목록 항목을 처음 사용하고 방금 연결된 목록을 사용하여 첫 번째 프로그램을 만들었으므로 구조에 데이터를 저장하지 않는 것이 문제입니다. 잘 실행되지만 오류는 없지만 인쇄 할 때 데이터가 표시되지 않습니다. 여기 내 코드가있다.C에서 연결된 목록 사용

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct node { 
    int  nID; 
    char chTitle; 
    struct node* next; 
}; 

void addList(struct node *head); 
void printList(struct node *head); 
int checkID(struct node *head, int t); 

int main(int argc, const char * argv[]) 
{ 
    int nInput; 
    struct node *head = NULL; 
    while (1) 
    { 
     printf("\n\t\t~~MENU~~\n"); 
     printf("1. Add a new book\n"); 
     printf("2. Print all data\n"); 
     printf("3. Exit\n"); 
     printf("Make your selection: "); 
     scanf("%d", &nInput); 

     switch (nInput) 
     { 
      case 1: 
       addList(head); 
       break; 
      case 2: 
       printList(head); 
       break; 
      case 3: 
       printf("\nGoodby!!! Thanks for using the program\n"); 
       exit(1); 
       break; 
      default: 
       printf("\n\t\t~~Invalid Input~~\n"); 
       break; 
     } 
    } 
    return 0; 
} 

void addList(struct node *head) 
{ 
    int bookId; // Used to store the BOOK ISBN so it can be checked if it already exist 
    struct node *temp; 

    temp = (struct node *)malloc(sizeof(struct node)); 


    printf("\n Enter Book Details\n"); 
    printf("Enter book ISBN: "); 
    scanf("%d", &bookId); 
    int bInd = checkID(head, bookId); 
    if (bInd == 0) 
    { 
     printf("Enter title: "); 
     scanf("%s", &temp->chTitle); 
     temp->next = head; 
     head = temp; 
    } 
    else 
    { 
     printf("\nSorry another book using that id!\n"); 
    } 
} 

void printList(struct node* head) 
{ 
    while (head != NULL) 
    { 
     printf("%s", &head->chTitle); 
     head = head->next; 
    } 
} 

int checkID(struct node *head, int t) 
{ 
    head = NULL; 
    while (head != NULL) 
    { 
     if (head->nID == t) 
      return 1; 
     head = head->next; 
    } 
    return 0; 
} 
+1

StackOverflow는 코드에 대한 자유로운 질문에 대답하지 않습니다. 여기서 질문을 할 때 문제를 해결하기 위해 무엇을 시도했는지 설명 할 수 있도록 준비해야합니다. 아직 디버거를 사용해 보셨습니까? – Tom

+0

추천 : ['malloc'의 결과를 캐스팅하지 마세요.] (0120-139904) –

+4

'addList()'는'head'를 로컬로만 변경합니다. 그래서'main'의'head'는 항상 null입니다. –

답변

0

하나의 문제는 바로 여기에 있습니다 :

void addList(struct node *head) 

addList() 헤드 포인터의 사본을 받고있다, 그래서 당신은이 기능에 수정할 때 로컬 복사본 것을 수정된다. 발신자의 버전이 수정되지 않았습니다. 이 문제를 해결하는 한 가지 방법은 이중 포인터를 사용하는 것입니다 :

void addList(struct node **head) 
{ 
    int bookId; // Used to store the BOOK ISBN so it can be checked if it already exist 
    struct node *temp; 

    temp = (struct node *)malloc(sizeof(struct node)); 


    printf("\n Enter Book Details\n"); 
    printf("Enter book ISBN: "); 
    scanf("%d", &bookId); 
    int bInd = checkID(*head, bookId); 
    if (bInd == 0) 
    { 
     printf("Enter title: "); 
     scanf("%s", &temp->chTitle); 
     temp->next = *head; 
     *head = temp; 
    } 
    else 
    { 
     printf("\nSorry another book using that id!\n"); 
    } 
} 

그런 다음 발신자도 변경할 수 있습니다 또한

addList(&head); 

언급 5gon12eder @를 같은 char은 하나의 캐릭터를 보유하고 있습니다.

무효 addList (구조체 노드 * & 헤드)

여기 머리가 참조되고,

struct node { 
    int  nID; 
    char chTitle[100]; /* or how ever long your title can be */ 
    struct node* next; 
}; 
+0

그래! 그리고 어쩌면 원칙을위한'scanf ("% 99s"...)'이 코드는 여전히 dublicate book id의 입력을 허용합니다. 'checkID()'에서'head = NULL;'이 제거되어야합니다. – Christophe

+0

@Christophe : 그래, 여기서 고칠 것이 많아. OP가 작동하기 위해 뭔가를 남겨 둘 필요가 있습니다. 8v) –

0

당신은이처럼 addList 헤더를 변경할 수 있습니다 : 당신은 당신의 제목을 보유 할 char 배열이 필요합니다 노드 유형의 포인터. 따라서 addList 안에 head을 수정하면 원래 목록에 다시 반영됩니다.

관련 문제