2014-11-21 2 views
0

이것은 노드를 추가 한 다음 인쇄하는 기본 연결 목록이지만 어떤 이유로 올바르게 작동하지 않습니다. 필자가 테스트 한 바에 따르면 목록을 인쇄 한 후에 실패하면 잘못 인쇄 된 임금을 인쇄 한 다음 종료됩니다. 코드의 일부를 다음에서연결된 목록이 제대로 작동하지 않습니다.

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

typedef struct node_s {           
     char job_title[25]; 
     double hourly_wage; 
     struct node_s *next; 
} node_t; 

void print_list(node_t *list); 
void add_node(node_t **head, char *title, double hwage); 

int main()             
{ 

     node_t *list; 
     list = NULL; 

     add_node(&list, "Programmer", 32.35);    
     print_list(list); 
     add_node(&list, "Analyst", 25.80);  
     print_list(list);    
     add_node(&list, "Technician", 17.50); 
     print_list(list); 
     add_node(&list, "Clerk", 12.00); 
     print_list(list); 
     add_node(&list, "Manager", 53.58); 
     print_list(list);  

     return(0); 
}               

void print_list(node_t *list){ 
    node_t *current; 
    if (current == NULL) { 
     printf("\n"); 
    }else{ 
     printf("The job is called:%s\n", current->job_title); 
     printf("The job pays %d hourly.\n", current->hourly_wage); 
     print_list(current->next); 
    } 
} 

void add_node(node_t **head, char *title, double hwage){ 
    node_t *current = head; 
    node_t *newNode = (node_t *) malloc(sizeof(node_t)); 
    if (newNode == NULL) { 
     printf("malloc failed\n"); 
     exit(-1); 
    }  

    strcpy(newNode->job_title, title); 
    newNode->hourly_wage = hwage; 
    newNode->next = NULL; 

    while (current->next) { 
     current = current->next; 
    }  
    current->next = newNode; 
} 
+1

'node_t * current = head;'head는'node_t ** '유형입니다. –

+0

인쇄 방법이 "현재"초기화에 실패했습니다. 그것은 쓰레기를 보유하고 있으므로 그 행동은 정의되어 있지 않습니다. –

+0

@Ieaturaw 내가 보여준 코드를 사용할 것이므로 최선을 다하여 내 답변을 표시하는 것을 잊지 마십시오. :) –

답변

3

:

void print_list(node_t *list){ 
    node_t *current; 
    if (current == NULL) { 

당신은 널 (null)와 현재 포인터의 초기화되지 않은 값을 비교한다. 나는 그것에 가치를 두는 것을 잊었다 고 생각합니다 :

current = list; 

지시 사항 이전에.

-4

기능 변경 다음과 같은 방법으로 여기에

void print_list(node_t *list) 
{ 
    if (list == NULL) 
    { 
     printf("\n"); 
    } 
    else 
    { 
     printf("The job is called:%s\n", list->job_title); 
     printf("The job pays %f hourly.\n", list->hourly_wage); 
     print_list(list->next); 
    } 
} 

void add_node(node_t **head, const char *title, double hwage) 
{ 
    node_t *newNode = (node_t *)malloc(sizeof(node_t)); 

    if (newNode == NULL) 
    { 
     printf("malloc failed\n"); 
     exit(-1); 
    }  

    strncpy(newNode->job_title, title, 25); 
    newNode->job_title[24] = '\0'; 
    newNode->hourly_wage = hwage; 
    newNode->next = NULL; 

    while (*head) 
    { 
     head = &(*head)->next; 
    }  

    *head = newNode; 
} 

은 시범 프로그램을이다

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

typedef struct node_s {           
     char job_title[25]; 
     double hourly_wage; 
     struct node_s *next; 
} node_t; 

void print_list(node_t *list) 
{ 
    if (list == NULL) 
    { 
     printf("\n"); 
    } 
    else 
    { 
     printf("The job is called:%s\n", list->job_title); 
     printf("The job pays %f hourly.\n", list->hourly_wage); 
     print_list(list->next); 
    } 
} 

void add_node(node_t **head, const char *title, double hwage) 
{ 
    node_t *newNode = (node_t *)malloc(sizeof(node_t)); 

    if (newNode == NULL) 
    { 
     printf("malloc failed\n"); 
     exit(-1); 
    }  

    strncpy(newNode->job_title, title, 25); 
    newNode->job_title[24] = '\0'; 
    newNode->hourly_wage = hwage; 
    newNode->next = NULL; 

    while (*head) 
    { 
     head = &(*head)->next; 
    }  

    *head = newNode; 
} 


int main(void) 
{ 
    node_t *list; 
     list = NULL; 

     add_node(&list, "Programmer", 32.35);    
     print_list(list); 
     add_node(&list, "Analyst", 25.80);  
     print_list(list);    
     add_node(&list, "Technician", 17.50); 
     print_list(list); 
     add_node(&list, "Clerk", 12.00); 
     print_list(list); 
     add_node(&list, "Manager", 53.58); 
     print_list(list);  

     return 0; 
} 

출력은

The job is called:Programmer 
The job pays 32.350000 hourly. 

The job is called:Programmer 
The job pays 32.350000 hourly. 
The job is called:Analyst 
The job pays 25.800000 hourly. 

The job is called:Programmer 
The job pays 32.350000 hourly. 
The job is called:Analyst 
The job pays 25.800000 hourly. 
The job is called:Technician 
The job pays 17.500000 hourly. 

The job is called:Programmer 
The job pays 32.350000 hourly. 
The job is called:Analyst 
The job pays 25.800000 hourly. 
The job is called:Technician 
The job pays 17.500000 hourly. 
The job is called:Clerk 
The job pays 12.000000 hourly. 

The job is called:Programmer 
The job pays 32.350000 hourly. 
The job is called:Analyst 
The job pays 25.800000 hourly. 
The job is called:Technician 
The job pays 17.500000 hourly. 
The job is called:Clerk 
The job pays 12.000000 hourly. 
The job is called:Manager 
The job pays 53.580000 hourly. 

당신은 모든 할당 된 메모리를 삭제 함수를 작성하기 만하다 목록에.

관련 문제