链接列表无法正常工作

时间:2014-11-21 20:41:13

标签: c linked-list nodes

这是添加节点然后打印它们的基本链接列表,但由于某种原因它无法正常工作。根据我测试的内容,在打印列表后它会失败,直到它打印工资,错误地打印数字然后终止。

#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;
}

2 个答案:

答案 0 :(得分:3)

以下部分代码:

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

您正在将当前指针的未初始化值与null进行比较。我想你忘了给它赋值:

current = list;

在指示之前。

答案 1 :(得分:-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.

您只需编写将删除列表中所有已分配内存的函数。