2014-11-21 213 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;'頭部類型爲'node_t **'。 – 2014-11-21 20:42:28

+0

您的打印方法無法初始化「當前」;它擁有垃圾,因此行爲未定義。 – 2014-11-21 20:46:28

+0

@Ieaturaw不要忘記標記我的答案是最好的,因爲我相信你會使用我展示的代碼。 – 2014-11-21 21:10:56

回答

3

在下面的部分代碼:

void print_list(node_t *list){ 
    node_t *current; 
    if (current == 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. 

你只需要編寫函數,將刪除所有分配的內存爲清單。