简体   繁体   English

链接列表无法正常工作

[英]Linked List isn't working properly

This is a basic linked list that adds nodes and then prints them but for some reason it doesn't work correctly. 这是一个基本的链表,该链表添加节点然后打印它们,但是由于某些原因,它无法正常工作。 From what I've tested it fails after printing the list it gets to the point where it prints the wage where it incorrectly prints the number and then terminates. 根据我的测试,它在打印列表后失败,直到打印工资,然后错误地打印数字,然后终止。

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

In following part of code: 在以下代码部分中:

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

You are comparing uninitialized value of current pointer with null. 您正在将当前指针的未初始化值与null进行比较。 I think you forgot to assign value to it: 我认为您忘记为它分配价值了:

current = list;

Before if instruction. 在if指令之前。

Change functions the following way 通过以下方式更改功能

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

Here is a demonstrative program 这是一个示范节目

#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 output is 输出是

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.

You need only to write function that will delete all allocated memory for the list. 您只需要编写函数即可删除该列表的所有已分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM