链表是不正常 [英] Linked List isn't working properly

查看:167
本文介绍了链表是不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是添加节点的基本链表则打印出来,但由于某种原因,它不能正常工作。从我测试了它打印它得到它打印它不正确地打印数量的工资,然后终止点的地址列表失败后。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;typedef结构node_s {
        焦炭JOB_TITLE [25];
        双hourly_wage;
        结构node_s *接下来的;
} node_t;无效print_list(node_t *清单);
无效add_node(node_t **头,字符*称号,双hwage);诠释的main()
{      node_t *名单;
      名单= NULL;       add_node(安培;列表中,程序员,32.35);
       print_list(名单);
       add_node(安培;列表中,分析师,25.80);
       print_list(名单);
       add_node(安培;列表中,技师,17.50);
       print_list(名单);
       add_node(安培;列表中,店员,12.00);
       print_list(名单);
       add_node(安培;列表中,经理人,53.58);
       print_list(名单);       返回(0);
}无效print_list(node_t *名单){
    node_t *电流;
    如果(当前== NULL){
        的printf(\\ n);
    }其他{
        的printf(作业名为:%S \\ n,电流 - > JOB_TITLE);
        的printf(工作也给予%d个小时\\ n,电流 - > hourly_wage);
        print_list(电流 - >下面);
    }
}无效add_node(node_t **头,字符*称号,双hwage){
    node_t *电流=头;
    node_t * newNode =(node_t *)malloc的(的sizeof(node_t));
    如果(newNode == NULL){
        的printf(malloc的失败\\ n);
        出口(-1);
    }    的strcpy(newNode-> JOB_TITLE,标题);
    newNode-> hourly_wage = hwage;
    newNode->接着= NULL;    而(电流 - >下面){
        电流=电流 - >接着,
    }
    电流 - >接下来= newNode;
}


解决方案

在以下code的一部分:

 无效print_list(node_t *名单){
    node_t *电流;
    如果(当前== NULL){

您在比较与零电流指针未初始化值。我想你忘记值分配给它:

 电流=清单;

在IF指令。

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. I think you forgot to assign value to it:

current = list;

Before if instruction.

这篇关于链表是不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆